查找单元格的VBA代码包含某个字符串(在.csv列表中)

时间:2016-07-20 11:20:22

标签: excel vba

找到了大量基于其包含的值查找单元格的示例,但是当单元格只包含一个值时,它们都是示例。我有这些列,其中包含deDE,esES等语言环境列表。如果我尝试根据其中一个语言环境找到一个单元格,我的代码不起作用,我不知道为什么。

Sub Test()


Dim strConcatList As String
Dim cell As Range

    strConcatList = "*" & "esES" & "*"

    'For each used cell in Column A of sheet1, check whether the value in that cell
    'is contained within the concatenated string
    For Each cell In Intersect(Sheets("Requests").Range("G:G"), Sheets("Requests").UsedRange)
        If InStr(strConcatList, cell.Value) > 0 Then       'InStr returns 0 if the string isn't found
        matchRow = cell.Row
        Rows(matchRow & ":" & matchRow).Select
        Selection.Copy
        Sheets.Add.Name = "esES"

        Sheets("Sheet2").Select
        ActiveSheet.Rows(matchRow).Select
        ActiveSheet.Paste
        Sheets("Requests").Select
        End If
    Next cell
End Sub

例如,此代码段应该找到存在“esES”的单元格,创建一个名为esES的新工作表并将该行粘贴到其中。我试过“esES”,没用。我希望我现有的形式可以工作 - 我对Regax有一点经验,虽然不在VBA中,它应该读取任何东西+ esES +任何东西。

不知道发生了什么,我已经被困了几个小时。我不介意在这里给你一点帮助。

编辑:

此代码正常运行,感谢您的帮助:

Sub Test()


Dim strConcatList As String
Dim cell As Range

    Sheets.Add.Name = "esES"
    strConcatList = "*esES*"

    'For each used cell in Column G of sheet1, check whether the value in that cell
    'is contained within the concatenated string
    For Each cell In Intersect(Sheets("Requests").Range("G:G"), Sheets("Requests").UsedRange)
        If cell.Value Like strConcatList Then
        matchRow = cell.Row
        Rows(matchRow & ":" & matchRow).Select
        Selection.Copy

        Sheets("esES").Select
        ActiveSheet.Rows(matchRow).Select
        ActiveSheet.Paste
        Sheets("Requests").Select
        End If
    Next cell
End Sub

1 个答案:

答案 0 :(得分:0)

此代码正常运行:

Sub Test()


Dim strConcatList As String
Dim cell As Range

    Sheets.Add.Name = "esES"
    strConcatList = "*esES*"


    For Each cell In Intersect(Sheets("Requests").Range("G:G"), Sheets("Requests").UsedRange)
        If cell.Value Like strConcatList Then
        matchRow = cell.Row
        Rows(matchRow & ":" & matchRow).Select
        Selection.Copy

        Sheets("esES").Select
        ActiveSheet.Rows(matchRow).Select
        ActiveSheet.Paste
        Sheets("Requests").Select
        End If
    Next cell
End Sub

非常感谢你的帮助!