Dictionary to hold column numbers

bklabel1

Board Regular
Joined
Feb 24, 2015
Messages
134
I want to store each column number into a dictionary. So I can say on sheet "Music" the cell with value "trumpet" is 7. If I ask the dictionary what column is "trumpet" it would return 7 so I know that trumpet is in the seventh column from the left.

Since I don't know of another way to do this I was trying with a Dictionary object. Please let me know if there is an easier way to do this. I will place my code here so that I find out what I did wrong. Even if there is a better way I will learn about using the data dictionary object in VBA.

I declare the object at the top before any sub or function. I do this to make it public. I don't know if it is a good idea to make them public but I don't know how to pass the dictionary into between sub/function

VBA Code:
Dim dicStorageB As Object
___________________________________________________

VBA Code:
Function keyValueCreateStorageB()
  Set dicStorageB = CreateObject("scripting.dictionary")
  For col = 1 To Sheets("StorageB").UsedRange.Columns.Count
    thisName = Sheets("StorageB").Cells(1,col).Value
    dicStorageB.Item(thisName) = col
  Next
End Function

I use the immediate window and ?dicStorageB.item(thisName) shows that the dictionary contains the value

This is called with
VBA Code:
KeyValueCreateStorageB()

After leaving the function the immediate window shows that the dictionary object value is empty.

The dictionary is declared as public because it is declared before the first sub/function. (i don't know if this is correct). I am not returning a variable or object so maybe I can use a sub instead of a function.

The dictionary looses its contents when it leaves the function and I do not know why.
 
Domenic,
Appreciated about the location of CompareMode.

Why does:
Sub keyValueCreateStorageB(ByVal dic As Object)
work?

I was expecting it to be ByRef. I have to be wrong because it works.

My thinking was that if the dictionary object is taken in to the sub that any changes to the dictionary object would be lost once the sub is done with its work. It does not work like that so what is wrong with my understanding?

Thanks,

Kevin
 
Upvote 0

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
Domenic,

I just woke up and looked at the link this morning. I have to come back to this. It's information overload right now. I quickly noticed that there is a section for passing objects. It says that they are always passed ByRef and I see examples of using ByVal. I need to go back to see why they have ByVal if it is always passed ByRef.

I appreciate the link.
 
Upvote 0

Forum statistics

Threads
1,214,520
Messages
6,120,017
Members
448,937
Latest member
BeerMan23

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top