0

I'm writing a script to record the various characteristics associated with a ream of material (supplier number, material certificates, etc). A nice way to do this I though would be to create a class object, named by the ream's serial number, with all these associated parameters.

The serial number is a string housed in an array. I'm struggling to find out whether there is some data type that can be assigned the serial number value and used to name the class object:

Dim Obj As String
Obj = Arr_IncomingCann(i, 2)
Dim Obj As New cCannister

I see why this can't work (I'm simply redimensioning "Obj"), but can someone give some advice as to whether what I'm attempting is possible?

1
  • 1
    Variable names in VBA are static (means they cannot be changed dynamically). You might want to have a look at Dictionaries see Excel VBA Dictionary – A Complete Guide. You can add a new item to the dictionary then and assign a new instance of your class to that item. Commented Feb 10, 2020 at 10:07

1 Answer 1

3

Variable names in VBA are static (means they cannot be changed dynamically). You might want to have a look at Dictionaries see Excel VBA Dictionary – A Complete Guide.

You can add a new item to the dictionary then and assign a new instance of your class to that item.

Example:

Dim dict As Object
Set dict = CreateObject("Scripting.Dictionary")

Dim NewClass As New clsMyClass
NewClass.Test = "ABC" 'do something with your class object

dict.Add Key:="your serial", Item:=NewClass
Set NewClass = Nothing 'you can destroy the variable the class object is now in the dictionary.

Then you can access it with

Debug.Print dict("your serial").Test 'will return ABC from your class object.
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.