Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7532217
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T05:23:13+00:00 2026-05-30T05:23:13+00:00

new to vba, and wonder how to address elements within a range. I add

  • 0

new to vba, and wonder how to address elements within a range.
I add range of cells (rows) indexed with an id to a dictionary

 Set spRange = import.Range("A2:" & spRange.End(xlDown).Address)
    For Each cell In spRange
        dict.Add cell.Offset(0, 2).Text, cell.Row

Next cell

later on, I retrieve the row, and need to access value of first element.

For Each x In dict
        Set spRange = dict.Item(x)
        'how to get value of first element of spRange 
Next

It could be very easy for u, but i am not familiar with api 🙂

thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T05:23:15+00:00Added an answer on May 30, 2026 at 5:23 am

    bsreekanth

    The Dictionary object, if I am not wrong first came out way back in 1996 as part of VB Script 2 and was later added to the VB Scripting run-time library (scrrun.dll). To work with the Dictionary object, you have to add a reference to Microsoft Scripting Runtime.

    The syntax of adding item to dictionary is

    DictObject.Add **<Unique key>**,Value
    

    The key has to be unique else you will get an error. Let’s cover different scenarios to understand how it works.

    Let’s take an example

    We store the row (and not the cell text) and then retrieve the row number

    Sub Sample()
        Dim spRange As Range
        Dim Dict As Dictionary
        Dim j as long
    
        Set spRange = Range("A1:A10")
        Set Dict = New Dictionary
    
        j = 1
    
        For Each cell In spRange
            Dict.Add j, cell.Row
            j = j + 1
        Next cell
    
        Dim x As Variant
    
        For Each x In Dict
            Debug.Print Dict(x)
        Next
    End Sub
    

    If you noticed that I am using the variable “j” to create unique keys and then storing the row values.

    To retrieve the stored row values, I am then looping through the Dictionary Objects.

    Now back to your question.

    later on, I retrieve the row, and need to access value of first element.

    This is the part where I am kind of confused. Reason being, if you wanted just to get the value of a particular row in range spRange then why are you using a dictionary object?

    You can directly get the value using this code

    Debug.print spRange.Cells(1, 1).Value
    

    If you still would like to use a dictionary object then you can use the below code

    Sub Sample()
        Dim spRange As Range
        Dim Dict As Dictionary
    
        Set spRange = Range("A1:A3")
        Set Dict = New Dictionary
    
        j = 1
    
        For Each cell In spRange
            Dict.Add j, cell.Row
            j = j + 1
        Next cell
    
        Dim x As Variant
    
        For Each x In Dict
            Debug.Print spRange.Cells(Dict(x), 1).Value
        Next
    End Sub
    

    And If your intention is to store the range values in the dictionary and then retrieve the value based on a particular key (Row Number) then you can use this code

    Sub Sample()
        Dim spRange As Range
        Dim Dict As Dictionary
    
        Set spRange = Range("A1:A3")
        Set Dict = New Dictionary
    
        For Each cell In spRange
            '~~> See how I reversed it?
            Dict.Add cell.Row, cell.Text
        Next cell
    
        Dim x As Variant
    
        For Each x In Dict
            Debug.Print Dict(x)
        Next
    
        'OR
    
        'Debug.Print Dict(2)
    End Sub
    

    Now one last point. If you are not going to loop through the Dictionary object to retrieve the values but are planning to use something like “Debug.Print Dict(2)” then I would suggest using an extra piece of code which first checks if the element is present or not and then shows it. For example

    If Dict.Exists(2) Then Debug.Print Dict(2)
    

    HTH

    Let me know if you have any questions.

    Sid

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using dictionaries in Excel VBA via dict As New Dictionary (and adding
I'm new to Access VBA development and being asked to debug and add features
I am trying to create a new instance of Excel using VBA using: Set
I am new to VBA and want to add two values. The code I
I am new to Active Directory. I have a VBA Excel Add-In that should
I am new to VBA in Excel. I'm setting up a simple macro Option
I'm totally new to Excel VBA. I am using Microsoft 2003 excel. What my
I have problem creating new instance of excel 2007 using VBA (from Access 2002).
I am using Excel VBA 2003. Is it possible to create new recordset by
Possible Duplicate: Check if access table exists I'm new to vba macros. Any idea

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.