P
Paul Urbanus
Hi,
I'm a VBA newbie - hacking out some code when I need it, but often
having to refer to the help as I don't use VBA enought to know the
various objects and methods off the top of my head.
DEVELOPMENT ENVIRONMENT
=======================
OS = Win2K Workstation
Excel = Excel XP
PROBLEM TO SOLVE
================
Create an associative array (Dictionary object in VBA) in which the
desired data type to store is a user defined type. Ideally, I'd like to
store an array, but VBA help says this isn't allowed.
WHAT I DID
==========
Since this was my first time using a Dictionary object, I did a web
search and cut/pasted/modified some existing code to do a simple test.
After I got the rudimentary test code to work (where string data is
stored in the dictionary), I decided to store one of my user-defined
data items.
THE VBA ERROR
=============
When I try and run the code, 'udtSeatCell' is highlighted in the
following line of code.
d.Add "111", udtSeatCell
Also, I get the following error:
'Compile Error:'
'Only user-defined types in public object modules can be coerced to or
from a variant or passed to late-bound functions'
MY VBA CODE
===========
' Define a physical seat desriptor (cell location in worksheet)
Public Type SeatCell
Row As Integer
Column As Integer
End Type
Dim udtSeatCell As SeatCell
Sub DictTest1()
Dim d 'Create a variable
Set d = CreateObject("Scripting.Dictionary")
Dim KeyToFind
' Initialize the test object
udtSeatCell.Row = 1
udtSeatCell.Column = 1
d.Add "105", "Section 105"
d.Add "120", "Section 120"
d.Add "219", "Section 219"
d.Add "111", udtSeatCell
MsgBox ("Dictionary 'd' has " & d.Count & " key/value pairs")
Do
KeyToFind = InputBox("Section to find?")
If d.exists(KeyToFind) = False Then
MsgBox ("Section " & KeyToFind & "was not found")
Else
MsgBox (d.Item(KeyToFind))
End If
Loop Until (KeyToFind = "")
End Sub
ANY SUGGESTIONS????
Urb
I'm a VBA newbie - hacking out some code when I need it, but often
having to refer to the help as I don't use VBA enought to know the
various objects and methods off the top of my head.
DEVELOPMENT ENVIRONMENT
=======================
OS = Win2K Workstation
Excel = Excel XP
PROBLEM TO SOLVE
================
Create an associative array (Dictionary object in VBA) in which the
desired data type to store is a user defined type. Ideally, I'd like to
store an array, but VBA help says this isn't allowed.
WHAT I DID
==========
Since this was my first time using a Dictionary object, I did a web
search and cut/pasted/modified some existing code to do a simple test.
After I got the rudimentary test code to work (where string data is
stored in the dictionary), I decided to store one of my user-defined
data items.
THE VBA ERROR
=============
When I try and run the code, 'udtSeatCell' is highlighted in the
following line of code.
d.Add "111", udtSeatCell
Also, I get the following error:
'Compile Error:'
'Only user-defined types in public object modules can be coerced to or
from a variant or passed to late-bound functions'
MY VBA CODE
===========
' Define a physical seat desriptor (cell location in worksheet)
Public Type SeatCell
Row As Integer
Column As Integer
End Type
Dim udtSeatCell As SeatCell
Sub DictTest1()
Dim d 'Create a variable
Set d = CreateObject("Scripting.Dictionary")
Dim KeyToFind
' Initialize the test object
udtSeatCell.Row = 1
udtSeatCell.Column = 1
d.Add "105", "Section 105"
d.Add "120", "Section 120"
d.Add "219", "Section 219"
d.Add "111", udtSeatCell
MsgBox ("Dictionary 'd' has " & d.Count & " key/value pairs")
Do
KeyToFind = InputBox("Section to find?")
If d.exists(KeyToFind) = False Then
MsgBox ("Section " & KeyToFind & "was not found")
Else
MsgBox (d.Item(KeyToFind))
End If
Loop Until (KeyToFind = "")
End Sub
ANY SUGGESTIONS????
Urb