Typecast string (cell value) to a user defined type?
I'm totally new to VBA.
I have created a user defined type called QualVal with two member variables. Then I've created several variables as Qualval and initialized its values.
Now I want to read a cell value, which is a string and match to the variable names and typecast it.
Type QualVal
ul As Integer 'upper limit
ll As Integer 'lower limit
End Type
开发者_开发百科
My variables with type QualVal:
g1.ll = 94
g1.ul = 96
n.ll = 90
n.ul = 93
Now I want to do something like this:
Dim group As QualVal
group = Cell(1, 2).Value 'would like to cast it to either g1 or n according to the cell value which also can only contain g1 or n
I hope it's clear what I want to do. I could use a select case statement but I have more variables than mentioned and this solution doesn't look nice.
Maybe someone can help me.
Thank you in advance and best regards!
I don't think you can do that, but I'm not sure.
Anyhow the best solution that I can see is to store all possible variable names and their object in a Collection. Something like this:
Dim vars As New Collection
vars.Add "g1", g1
vars.Add "n", n
Dim group As QualVal
group = vars.Item(Cell(1, 2).Value)
This is also much safer because you have control of what variables that is exposed.
精彩评论