【问题标题】:Adding an array to a for loop将数组添加到 for 循环
【发布时间】:2020-10-22 08:14:41
【问题描述】:

https://i.stack.imgur.com/RYIGs.png
目前,DDi 和 DDj,如果满足条件将显示消息“请为 Device1、2 或 3 选择一个设备通道。”、“请为 Device1、2 或 3 选择不同的设备通道”。我想让它说通道的实际名称,而不是 Device1、Device2 等。我不知道如何将数组放入我现有的结构中。我从下面开始,但在 for 开始的地方有语法错误。

If (HTSelection.DeviceDropDown1.List(0)) <> Empty Then
        
    Else
        DeviceDropDown1.AddItem "Device A: HT 1"
        DeviceDropDown1.AddItem "Device A: HT 2"
        DeviceDropDown1.AddItem "Device A: HT 3"
        DeviceDropDown1.AddItem "Device A: HT 4"
        DeviceDropDown1.AddItem "Device A: HT 5"
        DeviceDropDown1.AddItem "Device A: HT 6"
        DeviceDropDown1.AddItem "Device A: HT 7"
        DeviceDropDown1.AddItem "Device A: HT 8"
        DeviceDropDown1.AddItem "Device B: HT 1"
        DeviceDropDown1.AddItem "Device B: HT 2"
        DeviceDropDown1.AddItem "Device B: HT 3"
        DeviceDropDown1.AddItem "Device B: HT 4"
        DeviceDropDown1.AddItem "Device B: HT 5"
        DeviceDropDown1.AddItem "Device B: HT 6"
        DeviceDropDown1.AddItem "Device B: HT 7"
        DeviceDropDown1.AddItem "Device B: HT 8"
        DeviceDropDown1.AddItem "Channel_Not_Available"
    End If
    End Sub

Private Sub HTNextButton_Click()
    
    Dim DDi(1 To 3) As String
    DDi(1) = "Temperature"
    DDi(2) = "Adapter"
    DDi(3) = "USB"
    Dim i As Integer

    Dim DDj(1 To 3) As String
    DDj(1) = "Temperature"
    DDj(2) = "Adapter"
    DDj(3) = "USB"
    Dim ii As Integer

    'DDi = 1
    'DDj = 1
    Numberflag = 0
    DeviceFlagA = 0
    DeviceFlagB = 0
          'For DDi = 1 To 3
    For i = 1 To 3
        Device = Me.Controls.Item("DeviceDropDown" & DDi)
        If Device = "Channel_Not_Available" Then
        ElseIf Device = "Select Device" Then
            MsgBox "Please select a number channel for" & DDi, vbCritical, "Error"
            Exit For
        Else
            If InStr(1, Device, "Device A") Then
                DeviceFlagA = 1
            End If
            If InStr(1, Device, "Device B") Then
                DeviceFlagB = 1
            End If
               
              'For DDj = 1 To 3
            For ii = 1 To 3
            
                If DDi <> DDj Then
                    Device1 = Me.Controls.Item("DeviceDropDown" & DDj)
                    If Device1 = "Channel_Not_Available" Then
                    Else
                        If Device = Device1 Then
                            MsgBox "Please select different number channel for" & DDj, vbCritical, "Error"
                            Numberflag = Numberflag + 1
                        Exit For
                    End If
                End If
    End If
            Next
            If Numberflag >= 1 Then
                Exit For
            End If
        End If
        
    Next

    End If
End Sub

【问题讨论】:

  • 去掉那个On Error Resume Next...它隐藏了潜在的错误。
  • 删除 on error resume next 只会让事情复杂化,这里有一大堆问题都被该行隐藏了。 DDiDDj 是字符串而不是整数,for 行需要为 i 分配一个起始值。 Device = Me.Controls.Item("DeviceDropDown" &amp; DDi) 也有问题。
  • 将删除On error Resume Nexti should be 1 的起始值。我是新手,不知道如何将数组与我的 for 集成。 Device = Me.Controls.Item 在我不尝试使用数组时工作。
  • 我假设 Device = Me.Controls.Item 应该是 1、2 或 3,所以如果你需要数组中包含的字符串,你可以使用 i,你需要用 DDi(i) 索引数组
  • Device 在你的循环中不会改变,因为它引用了DDi,所以可能应该引用DDi(i)

标签: arrays excel vba for-loop


【解决方案1】:

你的意思是这样的吗??

Private Sub HTNextButton_Click()
    Dim Device As String
    Dim DDi(1 To 3) As String
    DDi(1) = "Temperature"
    DDi(2) = "Adapter"
    DDi(3) = "USB"

    For i = 1 To 3
        Device = Me.Controls.Item("DeviceDropDown" & i).Text
        If Device = "Channel_Not_Available" Or Device = "Select Device" Or Device = "" Then
            MsgBox "Please select a number channel for " & DDi(i), vbCritical, "Error"
            Me.Controls.Item("DeviceDropDown" & i).SetFocus
            Exit Sub
        End If
    Next
    '
    '
    '
End Sub

【讨论】:

  • 是的!确切地说,我也为 DDj 复制了它,并在我的第二部分中实施。完美运行。
猜你喜欢
  • 2021-07-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-02
  • 2019-02-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多