ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Initializing a dictionary and then modifying it. (https://www.excelbanter.com/excel-programming/365956-initializing-dictionary-then-modifying.html)

Abe[_4_]

Initializing a dictionary and then modifying it.
 
I have a dictionary object which I need to:

1) Create and modify upon opening the workbook (it draws data from the
workbook for its keys and objects)

2) Read and write to in userform and module level code

I haven't used dictionaries before. Can someone point out where I have
gone wrong? It is giving me a Error: Variable not defined at the Module
level code.

What follows isn't my actual code, but it should give you an idea of
what I am trying to do.

<This Workbook
'Declarations
Option Explicit
Public dictCornerCellPics

Private Sub Workbook_Open()
Set dictCornerCellPics = CreateObject("scripting.dictionary")
dictCornerCellPics.add "a", worksheets(4).cells(1,2)
dictCornerCellPics.add "b", worksheets(4).cells(2,2)
...etc....
End Sub
</This Workbook

<module code

Public Sub MakeCorners (strng as String) as Boolean
dictCornerCellPics.add strng, worksheets(3).cells(1,1)
...etc....
MakeCorners = dictCornerCellPics.exists("a")
End Sub
</module code

Thanks in advance, (someday, in a galaxy far far away, I'll be able to
end a post with HTH...)

-Abe


Bob Phillips

Initializing a dictionary and then modifying it.
 
Three things

The Public declaration of the dictionary object should be in the standard
code module not Thisworkbook (otherwise you need to qualify it).

If you want a procedure to return a value, it should be a function not a
sub.

Why are you adding to the dictionary in the function? Shouldn't you just
return the value?

--
HTH

Bob Phillips

(replace somewhere in email address with gmail if mailing direct)

"Abe" wrote in message
ps.com...
I have a dictionary object which I need to:

1) Create and modify upon opening the workbook (it draws data from the
workbook for its keys and objects)

2) Read and write to in userform and module level code

I haven't used dictionaries before. Can someone point out where I have
gone wrong? It is giving me a Error: Variable not defined at the Module
level code.

What follows isn't my actual code, but it should give you an idea of
what I am trying to do.

<This Workbook
'Declarations
Option Explicit
Public dictCornerCellPics

Private Sub Workbook_Open()
Set dictCornerCellPics = CreateObject("scripting.dictionary")
dictCornerCellPics.add "a", worksheets(4).cells(1,2)
dictCornerCellPics.add "b", worksheets(4).cells(2,2)
...etc....
End Sub
</This Workbook

<module code

Public Sub MakeCorners (strng as String) as Boolean
dictCornerCellPics.add strng, worksheets(3).cells(1,1)
...etc....
MakeCorners = dictCornerCellPics.exists("a")
End Sub
</module code

Thanks in advance, (someday, in a galaxy far far away, I'll be able to
end a post with HTH...)

-Abe





All times are GMT +1. The time now is 11:20 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com