错误运行时间' 9'在VBA中的拆分功能

时间:2018-01-21 08:11:02

标签: vba excel-vba excel

在我的Excel中,我创建了以下组合框:

Private Sub Workbook_Open()
    With ActiveSheet.ComboBox1
        .AddItem "CHIAV<-->MACC"
        .AddItem "CHIAV<-->TRISC"
        .AddItem "CHIAV<-->PITT"
    End With
End Sub

其变更子是:

Private Sub ComboBox1_Change()
    Dim operatore As String
    Dim Op1 As String
    Dim Op2 As String
    Dim trovato As Integer
    Dim rng As Range
    Set rng = Selection
    operatore = ActiveSheet.OLEObjects("ComboBox1").Object
    Op1 = Split(operatore, "<")(0)
    Op2 = Split(operatore, ">")(1)
    Cells(1, 2) = Op1
    Cells(2, 2) = Op2
    trovato = 0
    If ComboBox1.Value = "SELEZIONA OPERATORI" Then
        Exit Sub
    End If

    If rng Is Nothing Then
        MsgBox "Non hai selezionato nessun range di celle!"
        Exit Sub
    End If

    For Each cell In rng
        If (trovato = 2) Then
            Exit For

        ElseIf StrComp(cell.Value, Op1) = 0 Then
            trovato = trovato + 1
        End If
    Next cell

    If (trovato < 2) Then
        MsgBox "Operatori non trovati nella selezione!"
        cbReset
        Exit Sub
    Else
        Select Case operatore
            Case "CHIAV<-->MACC"
                For Each cell In Selection
                    If cell.Value = "CHIAV" Then
                        cell.Value = "MACC"
                    ElseIf cell.Value = "MACC" Then
                        cell.Value = "CHIAV"
                    End If
                Next cell
                MsgBox "Scambiato CHIAV con MACC"
            Case "CHIAV<-->TRISC"
                For Each cell In Selection
                    If cell.Value = "CHIAV" Then
                        cell.Value = "TRISC"
                    ElseIf cell.Value = "TRISC" Then
                        cell.Value = "CHIAV"
                    End If
                Next cell
                MsgBox "Scambiato CHIAV con TRISC"
            Case "CHIAV<-->PITT"
                For Each cell In Selection
                    If cell.Value = "CHIAV" Then
                        cell.Value = "PITT"
                    ElseIf cell.Value = "PITT" Then
                        cell.Value = "CHIAV"
                    End If
                Next cell
                MsgBox "Scambiato CHIAV con PITT" 
        End Select
    End If
    cbReset
End Sub

和cbReset是宏:

Sub cbReset()
 'Reset ComboBox1 Values
      ActiveSheet.ComboBox1.Value = "SELEZIONA OPERATORI"
      ActiveSheet.Cells(1, 1).Select
End Sub

程序完成了我写的内容,但在选择项目之后,错误出现在语句&#34; Op2 = Split(operatore,&#34;&gt;&#34;)(1)&#34; 。我无法理解我的错误。拆分功能不应该提示错误,因为符号&#34;&gt;&#34;在字符串&#34; operatore&#34;。有人可以帮我理解吗?

1 个答案:

答案 0 :(得分:2)

如果字符串>中没有此类字符,"SELEZIONA OPERATORI"之后尝试查找ComboBox值的部分,则会导致您的问题。

在执行任何操作之前,您应该将测试移动到“默认”(?)值:

Private Sub ComboBox1_Change()
    Dim operatore As String
    Dim Op1 As String
    Dim Op2 As String
    Dim trovato As Integer
    Dim rng As Range

    If ComboBox1.Value = "SELEZIONA OPERATORI" Then
        Exit Sub
    End If

    Set rng = Selection
    operatore = ActiveSheet.OLEObjects("ComboBox1").Object
    Op1 = Split(operatore, "<")(0)
    Op2 = Split(operatore, ">")(1)
    Cells(1, 2) = Op1
    Cells(2, 2) = Op2
    trovato = 0

    '...

或者,如K.Dᴀᴠɪs所述,确保不创建错误的更通用的方法是在尝试访问分隔符后的字符串部分之前测试分隔符是否存在。 (即使分隔符不存在,在分隔符之前获取部分也是安全的。)

Private Sub ComboBox1_Change()
    Dim operatore As String
    Dim Op1 As String
    Dim Op2 As String
    Dim trovato As Integer
    Dim rng As Range

    Set rng = Selection
    operatore = ActiveSheet.OLEObjects("ComboBox1").Object
    'Note: It's usually better to use the full delimiter rather than just a part of it
    Op1 = Split(operatore, "<-->")(0)
    If Instr(operatore, "<-->") > 0 Then
        Op2 = Split(operatore, "<-->")(1)
    Else
        Op2 = ""
    End If
    Cells(1, 2) = Op1
    Cells(2, 2) = Op2
    trovato = 0

    If ComboBox1.Value = "SELEZIONA OPERATORI" Then
        Exit Sub
    End If

    '...