【问题标题】:Excel Macro VBA Not InstrExcel 宏 VBA 未指示
【发布时间】:2013-03-28 17:13:11
【问题描述】:

我正在尝试修改我找到的代码。它是一个 VBA 函数,用于搜索单元格值的所有实例,然后将每个实例的单元格值返回到一个单元格中。我试图只返回尚未找到的值,因此最终得到一个不包含重复项的单元格。

原代码:

Function Lookup_concat(Search_string As String, _
Search_in_col As Range, Return_val_col As Range)
Dim i As Long
Dim result As String
For i = 1 To Search_in_col.Count
If Search_in_col.Cells(i, 1) = Search_string Then
result = result & " " & Return_val_col.Cells(i, 1).Value
End If
Next
Lookup_concat = Trim(result)
End Function

我已将代码修改为这样,我将编辑缩进而不是将其保留在同一行以使其更易于阅读

Function Lookup_concat(Search_string As String, _
Search_in_col As Range, Return_val_col As Range)
Dim i As Long
Dim result As String
For i = 1 To Search_in_col.Count
If Search_in_col.Cells(i, 1) = Search_string 
      And Not (InStr(1, result, Return_val_col.Cells(i, 1).Value)) Then
result = result & " " & Return_val_col.Cells(i, 1).Value
End If
Next
Lookup_concat = Trim(result)
End Function

这个版本最接近 PHP !strstr 函数(我确实理解),也许尝试将 PHP 技术应用于 VBA 是我出错的地方。我的想法是结果字符串实际上是在我的Instr 命令之后填充的,这就是它不起作用的原因。

【问题讨论】:

    标签: excel vba


    【解决方案1】:

    我相信您正在尝试使用两个Ifs。您只想根据字符串在一个范围内添加,并且仅在尚未添加字符串的情况下添加。

    试试这个:

    Function Lookup_concat(Search_string As String, _
    Search_in_col As Range, Return_val_col As Range)
       Dim i As Long
       Dim result As String
    
       For i = 1 To Search_in_col.Count
    
          If (Instr(1, Search_in_col.Cells(i, 1), Search_string) > 0 )
              And ( InStr(1, result, Return_val_col.Cells(i, 1).Value) = 0 ) Then
    
             result = result & " " & Return_val_col.Cells(i, 1).Value
    
          End If
    
       Next
    
       Lookup_concat = Trim(result)
    
    End Function
    

    【讨论】:

    • 完美,谢谢!抱歉回复慢,我从上周开始没上班
    【解决方案2】:

    这是你要找的吗?

    Function Lookup_concat(Search_string As String, Search_in_col As Range, Return_val_col As Range)
        Dim i As Long
        Dim result As String
        For i = 1 To Search_in_col.Count
            If InStr(1, Search_in_col.Cells(i, 1).Value, Search_string, vbTextCompare) > 0 Then
                result = result & " " & Return_val_col.Cells(i, 1).Value
            End If
        Next
        Lookup_concat = Trim(result)
    End Function
    

    【讨论】:

      【解决方案3】:

      不确定你在用Search_in_colReturn_val_col 做什么,但你肯定需要使用If Instr() > 0 测试。

      如果结果 > 1,那么您可能不需要执行任何操作。如果结果为 0,那么您将需要进行连接。这就是我不确定你为什么将search_in_col.cells(i,1).Value 作为搜索参数传递,然后与Return_val_col.Cells(i,1).Value 连接的部分,所以你实际上并没有连接你用作搜索参数的值......

      'if the cell's value exists in the search_string
      If InStr(1, Search_in_col.Cells(i, 1).Value, search_string, vbBinaryCompare) > 0 Then
          'probably you want to do nothing here, since it's already existing
      Else:
          'the cell's value does not exist in the search_string, so concatenate it
          result = result & " " & "whatever value you want to append to the result"
      End If
      

      【讨论】:

      • 很好的解释,谢谢。我已经接受了 previsou 的答案,但这肯定很有帮助
      猜你喜欢
      • 2011-01-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-10
      • 2015-06-13
      相关资源
      最近更新 更多