VBA Excel连接两个variables值以形成一个新的variables

我正在尝试编写一个读取多个实体的代码,并对它们进行sorting和sorting。 每个实体都有一个types(A,B,C等),它们应该决定它被放入什么样的表单,并将它们全部放到我的“全部”表单中。 每次我find任何给定types的实体时,我也想增加一个特定于该types的variables。

我想要做什么,如果findtypes,并做两件事情:设置当前工作表的types。 将计数器variables设置为该types。

例:

Dim x As Integer, FindSlot As Integer Dim CurrentSheet As String, CurrentPropertyNumb As String Dim APropertyNumb As String, BPropertyNumb As String Dim CPropertyNumb As String For x = 1 to 2 If x = 1 Then CurrentSheet = "All" Else CurrentSheet = Range("B" & FindSlot) CurrentPropertyNumb = CurrentSheet & PropertyNumb End If Next x 

在else块中,CurrentSheet将被设置为“A”,“B”,“C”或任何types。 然后我想要CurrentPropertyNumb设置为“APropertyNumb”或“BPropertyNumb”等等。显然,我可以用几个If语句做到这一点,但它最终会成为我想避免的12个,我认为这将是很酷的! 🙂

有没有办法做到这一点,还是我对自己的目标太过崇高?

如果你有一系列你想用string值进行索引的值,那么字典是一个很好的select:

 Dim x As Integer, FindSlot As Integer Dim CurrentSheet As String, CurrentPropertyNumb As String Dim PropNums as Object Dim CPropertyNumb As String Set PropNums = CreateObject("scripting.Dictionary") For x = 1 to 2 If x = 1 Then CurrentSheet = "All" Else CurrentSheet = Range("B" & FindSlot) If Not PropNums.Exists(CurrentSheet) Then PropNums.Add CurrentSheet, 1 '? what are the initial values here? Else PropNums(CurrentSheet) = PropNums(CurrentSheet) +1 End If CurrentPropertyNumb = PropNums(CurrentSheet) End If Next x