循环遍历 VBA 中的复选框。

huangapple go评论83阅读模式
英文:

Loops through Check Box in VBA

问题

我有20个不同的复选框(命名为Checkbox 30到Checkbox 50),我需要逐个勾选。每个复选框的选择需要转化为4个不同的数组。

我的数组如下:
Graph_1(2, 20)
Graph_2(2, 20)
Graph_3(3, 20)
Graph_4(6, 20)

是否有一种简单的方法来实现这个,目前我正在使用这段代码。这种方法可以工作,但不够简洁。我觉得一定有更快的方法,但想不到更好的方法来实现这个。

If ActiveSheet.Shapes("Check Box 30").ControlFormat.Value = 1 Then ' Team 1
    Graph_1(1, 1) = 1
    Graph_1(2, 1) = 1
    Graph_2(1, 1) = 1
    Graph_2(2, 1) = 1
    Graph_3(1, 1) = 1
    Graph_3(2, 1) = 1
    Graph_3(3, 1) = 1
    Graph_4(1, 1) = 1
    Graph_4(2, 1) = 1
    Graph_4(3, 1) = 1
    Graph_4(4, 1) = 1
    Graph_4(5, 1) = 1
    Graph_4(6, 1) = 1
End If

复选框31对应每个数组的第2行,32对应第3行,以此类推。

感谢任何帮助。

英文:

I have 20 different checkbox (Named Checkbox 30 to Checkbox 50) that I need to check through. Each checkbox selection needs to be translated into 4 different arrays.

My arrays are as follows:
Graph_1(2,20)
Graph_2(2,20)
Graph_3(3,20)
Graph_4(6,20)

Is there an easy way to do this, currently I am using this code. This way will work but it's not pretty nor concise. I figure there must be a quicker way to do it but can't think of a better method to do this.

If ActiveSheet.Shapes("Check Box 30").ControlFormat.Value = 1 Then ' Team 1
    Graph_1(1, 1) = 1
    Graph_1(2, 1) = 1
    Graph_2(1, 1) = 1
    Graph_2(2, 1) = 1
    Graph_3(1, 1) = 1
    Graph_3(2, 1) = 1
    Graph_3(3, 1) = 1
    Graph_4(1, 1) = 1
    Graph_4(2, 1) = 1
    Graph_4(3, 1) = 1
    Graph_4(4, 1) = 1
    Graph_4(5, 1) = 1
    Graph_4(6, 1) = 1
End If

Checkbox 31 refers to row 2 of each array, 32 to row 3 etc.

Thanks for any help.

答案1

得分: 0

以下是您要求的翻译:

"Your question suggests that you would be well served by working through a tutorial on VBA as it appears that you don't understand how to construct looping structures.

You also appear to be confused about which index refers to rows and which to columns. I've assumed that when you refer to rows in your question you are referring to dimension 2 of the array.

One point to be aware of is that if you import an x * y range from Excel, the indexing in VBA will be y * x.

For your specific issue, the code below may be a helpful pointer.

Dim myCB As Long
    
    For myCB = 0 To 20
        Dim myCBStr As String
        myCBStr = VBA.CStr(30 + myCB)
        
        Dim myRow As Long
        myRow = myCB + 1
        
        If ActiveSheet.Shapes("Check Box " & myCBStr).ControlFormat.Value = 1 Then ' Team 1
            Graph_1(1, myRow) = 1
            Graph_1(2, myRow) = 1
            Graph_2(1, myRow) = 1
            Graph_2(2, myRow) = 1
            Graph_3(1, myRow) = 1
            Graph_3(2, myRow) = 1
            Graph_3(3, myRow) = 1
            Graph_4(1, myRow) = 1
            Graph_4(2, myRow) = 1
            Graph_4(3, myRow) = 1
            Graph_4(4, myRow) = 1
            Graph_4(5, myRow) = 1
            Graph_4(6, myRow) = 1
        End If
    Next

"

英文:

Your question suggests that you would be well served by working through a tutorial on VBA as it appears that you don't understand how to construct looping structures.

You also appear to be confused about which index refers to rows and which to columns. I've assumed that when you refer to rows in your question you are referring to dimension 2 of the array.

One point to be aware of is that if you import an x * y range from Excel, the indexing in VBA will be y * x.

For you specific issue the code belw may be a helpful pointer.

Dim myCB As Long
    
    For myCB = 0 To 20
        Dim myCBStr As String
        myCBStr = VBA.CStr(30 + myCB)
        
        Dim myRow As Long
        myRow = myCB + 1
        
        If ActiveSheet.Shapes("Check Box " & myCBStr).ControlFormat.Value = 1 Then ' Team 1
            Graph_1(1, myRow) = 1
            Graph_1(2, myRow) = 1
            Graph_2(1, myRow) = 1
            Graph_2(2, myRow) = 1
            Graph_3(1, myRow) = 1
            Graph_3(2, myRow) = 1
            Graph_3(3, myRow) = 1
            Graph_4(1, myRow) = 1
            Graph_4(2, myRow) = 1
            Graph_4(3, myRow) = 1
            Graph_4(4, myRow) = 1
            Graph_4(5, myRow) = 1
            Graph_4(6, myRow) = 1
        End If
    Next

答案2

得分: 0

请尝试下一种方法。它假定您使用“Form”复选框,并在说“translate”时,需要将1放置在名称以30结尾的复选框中,将2放置在以31结尾的复选框中,依此类推:

Sub CheckBoxesLoopHandling()
  Dim sh As Worksheet, chkB As CheckBox, ext As Long
  Dim Graph_1(1 To 2, 1 To 1), Graph_2(1 To 2, 1 To 1)
  Dim Graph_3(1 To 3, 1 To 1), Graph_4(1 To 6, 1 To 1) 'array with a column...
  
  Set sh = ActiveSheet
  
  For Each chkB In sh.CheckBoxes
        ext = Split(chkB.name)(2)
        If ext >= 30 And ext <= 50 And chkB.Value = 1 Then
            Graph_1(1, 1) = ext - 29
            Graph_1(2, 1) = ext - 29
            Graph_2(1, 1) = ext - 29
            Graph_2(2, 1) = ext - 29
            Graph_3(1, 1) = ext - 29
            Graph_3(2, 1) = ext - 29
            Graph_3(3, 1) = ext - 29
            Graph_4(1, 1) = ext - 29
            Graph_4(2, 1) = ext - 29
            Graph_4(3, 1) = ext - 29
            Graph_4(4, 1) = ext - 29
            Graph_4(5, 1) = ext - 29
            Graph_4(6, 1) = ext - 29
      End If
  Next
End Sub

或者

Sub CheckBoxesLoopHandling()
  Dim sh As Worksheet, chkB As CheckBox, ext As Long
  Dim Graph_1(1 To 2, 1 To 21), Graph_2(1 To 2, 1 To 21)
  Dim Graph_3(1 To 3, 1 To 21), Graph_4(1 To 6, 1 To 21)
  
  Set sh = ActiveSheet
  
  For Each chkB In sh.CheckBoxes
        ext = Split(chkB.name)(2)
        If ext >= 30 And ext <= 50 And chkB.Value = 1 Then
            Graph_1(1, ext - 29) = 1
            Graph_1(2, ext - 29) = 1
            Graph_2(1, ext - 29) = 1
            Graph_2(2, ext - 29) = 1
            Graph_3(1, ext - 29) = 1
            Graph_3(2, ext - 29) = 1
            Graph_3(3, ext - 29) = 1
            Graph_4(1, ext - 29) = 1
            Graph_4(2, ext - 29) = 1
            Graph_4(3, ext - 29) = 1
            Graph_4(4, ext - 29) = 1
            Graph_4(5, ext - 29) = 1
            Graph_4(6, ext - 29) = 1
      End If
  Next
End Sub

将1放置在相应的数组列中...

关于“translate”的含义,根据您的提问,但您没有澄清它...

英文:

Please, try the next way. It assumes that you use Form check boxes and saying "translate" you need to place 1 for the CheckBox name ending in 30, 2 for the one ending in 31 and so on:

Sub CheckBoxesLoopHandling()
  Dim sh As Worksheet, chkB As CheckBox, ext As Long
  Dim Graph_1(1 To 2, 1 To 1), Graph_2(1 To 2, 1 To 1)
  Dim Graph_3(1 To 3, 1 To 1), Graph_4(1 To 6, 1 To 1) &#39;array with a column...
  
  Set sh = ActiveSheet
  
  For Each chkB In sh.CheckBoxes
        ext = Split(chkB.name)(2)
        If ext &gt;= 30 And ext &lt;= 50 And chkB.Value = 1 Then
            Graph_1(1, 1) = ext - 29
            Graph_1(2, 1) = ext - 29
            Graph_2(1, 1) = ext - 29
            Graph_2(2, 1) = ext - 29
            Graph_3(1, 1) = ext - 29
            Graph_3(2, 1) = ext - 29
            Graph_3(3, 1) = ext - 29
            Graph_4(1, 1) = ext - 29
            Graph_4(2, 1) = ext - 29
            Graph_4(3, 1) = ext - 29
            Graph_4(4, 1) = ext - 29
            Graph_4(5, 1) = ext - 29
            Graph_4(6, 1) = ext - 29
      End If
  Next
End Sub

or

Sub CheckBoxesLoopHandling()
  Dim sh As Worksheet, chkB As CheckBox, ext As Long
  Dim Graph_1(1 To 2, 1 To 21), Graph_2(1 To 2, 1 To 21)
  Dim Graph_3(1 To 3, 1 To 21), Graph_4(1 To 6, 1 To 21)
  
  Set sh = ActiveSheet
  
  For Each chkB In sh.CheckBoxes
        ext = Split(chkB.name)(2)
        If ext &gt;= 30 And ext &lt;= 50 And chkB.Value = 1 Then
            Graph_1(1, ext - 29) = 1
            Graph_1(2, ext - 29) = 1
            Graph_2(1, ext - 29) = 1
            Graph_2(2, ext - 29) = 1
            Graph_3(1, ext - 29) = 1
            Graph_3(2, ext - 29) = 1
            Graph_3(3, ext - 29) = 1
            Graph_4(1, ext - 29) = 1
            Graph_4(2, ext - 29) = 1
            Graph_4(3, ext - 29) = 1
            Graph_4(4, ext - 29) = 1
            Graph_4(5, ext - 29) = 1
            Graph_4(6, ext - 29) = 1
      End If
  Next
End Sub

to place 1 in the corresponding array columns...

Depending on the "translate" meaning, about I asked but you did not clarify it...

huangapple
  • 本文由 发表于 2023年3月31日 17:59:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/75897182.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定