【问题标题】:Excel VBA Case Statement with Like conditions not working as excpected具有类似条件的 Excel VBA 案例语句未按预期工作
【发布时间】:2014-07-08 16:03:00
【问题描述】:

早上好,

我遇到了类似的问题: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#: 

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

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

【问题讨论】:

  • 您没有为变量CellVal 分配任何内容,因此您马上就会遇到逻辑错误:CellVal 将始终为空/空字符串,除非您对其进行分配。
  • 谢谢,我没有收到错误,这可能意味着它会自动定义它,但我已经这样做了。

标签: excel sql-like case-statement vba


【解决方案1】:

为什么不简单地将您的Case 建立在单元格的.Column 上?无需使用模糊 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 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

从 cmets 更新:类型不匹配

类型不匹配源于您使用+ 作为字符串连接器。在 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

【讨论】:

  • 我仍然收到第 15 列的“类型不匹配”错误,但它适用于所有其他列。 'O' 列通常是一个 9 位整数,为什么会抛出这个错误?更新:NM,我只是放了一个字符串并将其设置为单元格值: Dim v As String v = cell.Value Print #1, "Token#:" + v + " " + cell.Address
  • 干杯。我已经进行了更新以解释您为什么会收到该错误。您找到了一种解决方法,但我向您展示了另外两种解决方法:)
  • 谢谢!我不知道 '&' 是一个字符串连接器。我想我也会这样。现在这一切都运行得很好,真的节省了我们的时间。再次感谢您的帮助!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-10-23
  • 2017-01-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-05
  • 2017-10-27
相关资源
最近更新 更多