具有类似条件的Excel VBA案例声明未按预期工作

时间:2014-07-08 16:03:00

标签: excel-vba sql-like case-statement vba excel

早上好,

我遇到类似的问题:Case Like doesn't work in VBA

基本上,当我们使用LIKE运算符时,我们会得到不同的结果,我们认为应该这样做。她似乎只是跳到最后没有结果,但是我使用第一个案例,只有第一个案例,而忽略了其余的,即使它们不匹配。

我想要的效果是我想选择一系列单元格,并且对于该范围内的每个单元格,循环将值导出到具有不同前缀的文本文件,具体取决于单元格列:

    Sub test()
    Dim r As Excel.Range, cell As Excel.Range
    On Error Resume Next
    Set r = Application.InputBox("Select Range", "Select Range", Type:=8)
    On Error GoTo 0
    If r Is Nothing Then Exit Sub

    Open "C:\Users\User_Name\Documents\Macro Results\text.txt" For Output As #1
    For Each cell In r
        Select Case CellVal
            Case cell.Address Like "$A$#"
                Print #1, "Last Name: " + cell.Value + " " + cell.Address
            Case cell.Address Like "$B$#"
                Print #1, "First Name: " + cell.Value + " " + cell.Address
    Case cell.Address Like "$C$#"
                'Do Nothing Print #1, "First Name: " + cell.Value + " " + cell.Address
    Case cell.Address Like "$F$#"
                Print #1, "Email: " + cell.Value + " " + cell.Address
    Case cell.Address Like "$G$#"
                Print #1, "Phone#: " + cell.Value + " " + cell.Address
    Case cell.Address Like "$H$#"
                'Do Nothing 
    Case cell.Address Like "$I$#"
                'Do Nothing 
    Case cell.Address Like "$J$#"
                'Do Nothing 
    Case cell.Address Like "$K$#"
                'Do Nothing 
    Case cell.Address Like "$L$#"
                'Do Nothing 
    Case cell.Address Like "$M$#"
                'Do Nothing 
    Case cell.Address Like "$N$#"
                Print #1, "Token Type: " + cell.Value + " " + cell.Address
    Case cell.Address Like "$O$#"
                Print #1, "Token#:" + cell.Value + " " + cell.Address
            Case Else
        End Select
    Next
    Close
End Sub

我希望输出看起来像:

Last Name: 
First Name: 
Email: 
Phone#: 
Token Type: 
Token#: 

我不想为类似的语句添加每个单独的单元格列,但在测试宏中,我能够使用此技术成功区分A和B列。它在最终的电子表格中不起作用。

任何帮助或指导将不胜感激。谢谢!

1 个答案:

答案 0 :(得分:1)

为什么不简单地将Case基于单元格.Column?无需使用模糊运算符/ etc。

Sub test()
    Dim r As Excel.Range, cell As Excel.Range
    On Error Resume Next
    Set r = Application.InputBox("Select Range", "Select Range", Type:=8)
    On Error GoTo 0
    If r Is Nothing Then Exit Sub

    Open "C:\Users\User_Name\Documents\Macro Results\text.txt" For Output As #1
    For Each cell In r
        Select Case cell.Column
            Case 1
                Print #1, "Last Name: " + cell.Value + " " + cell.Address
            Case 2
                Print #1, "First Name: " + cell.Value + " " + cell.Address
            Case 6
                Print #1, "Email: " + cell.Value + " " + cell.Address
            Case 7
                Print #1, "Phone#: " + cell.Value + " " + cell.Address
            Case 14
                Print #1, "Token Type: " + cell.Value + " " + cell.Address
            Case 15
                Print #1, "Token#:" + cell.Value + " " + cell.Address
            Case Else
        End Select
    Next
    Close
End Sub

从评论更新re:输入错误

类型不匹配源于您使用+作为字符串连词。在VBA中,您可以使用+&。问题是,如果在处理混合数据类型(整数/长整数,字符串等)时尝试使用+,那么它可能会尝试将其用作数学运算符(加号)。以不同的方式避免这种情况:

我的偏好是始终使用&作为字符串连接符:

 Print #1, "Token#:" & cell.Value & " " + cell.Address

或者将值转换为字符串变量(您已经想到了)。

 Dim v as String
 v = cell.value
 Print #1, "Token#:" + v + " " + cell.Address

另一个替代直接转换为字符串:

 Print #1, "Token#:" + CStr(cell.Value) + " " + cell.Address