使用变量作为模块/数组名称

时间:2015-05-26 20:46:04

标签: arrays excel-vba vba excel

我的最终目标是使用来自不同数组(vba编码)的元素加载一个comboBox。我有8个不同的阵列和6个选项按钮。前两个optionButtons被分组以消除4个数组,其中最后4个分组的optionButtons确定组合框的实际输入。

它的工作原理如下:

'first grouped option buttons
maleOptionButton
femaleOptionButton

'second grouped option buttons
basketballOptionButton
footballOptionButton
soccerOptionButton
hockeyOptionButton

您可以猜到的阵列中充满了体育运动的学生姓名。因此,当用户点击男/女的第一个分组按钮时,点击甚至什么也不做。但是当他们点击体育的第二组选项按钮时,它在具有IF的模块中调用相同的子程序。否则Ifs确定所选按钮的组合。

sub maleInitArray()

'declaration section:
Public maleSoccerArray(1 to 6) as string

'sub section
dim mike as student 'calls the class student
set mike = new student

with mike
    .name = "Michael"
    .age = 14
    .so on and so on = something
end with

maleSoccerArray(1) = mike.name
End Sub

我想做的是:

dim i as integer
dim l as integer
dim gender as string
dim sport as string

if inputForm.soccerOptionButton.value = true then
    if inputForm.maleOptionButton.value = true then
        gender = "male"
        sport = "maleSoccerArray"
        call male.maleInitArray    ' inits the array thats hard coded.
    else
        gender = "female"
        sport = "femaleSoccerArray"
        call female.femaleInitArray
 Else If... 
 ' the list goes on to assign variables depending on the combo boxes.

' doesn't work, but it beats using this every time
l = UBound(sport) ' Doesn't recognize "sport" as an Array  
for i = 1 to l
     ' .AddItem(gender.sport(i)) will not work as well.
     inputForm.studentComboBox.AddItem(gender.sport(i))
next i

好像是UBound(变量)和AddItem(Variable.Variable) 不会起作用......

到目前为止我找到了一些东西,但是它们都没有用过......比如application.run方法并且分配了实际的" male.maleSoccerArray"方法

非常感谢任何帮助..谢谢

0 个答案:

没有答案