Excel VBA-循环浏览单元格列并搜索工作簿中的每个单元格值

Excel VBA-循环浏览单元格列并搜索工作簿中的每个单元格值,vba,excel,macros,Vba,Excel,Macros,我需要创建一个宏,在包含字符串的选定单元格列表中循环,并获取每个字符串,然后在整个工作簿中搜索该字符串。如果找到该字符串,则应退出子字符串,并选择找到的字符串,否则将继续查找下一个字符串,直到列表结束 当我运行代码时,在列表中找到一个字符串时,子节点不会退出,也不会进入该单元格 Option Explicit Dim sheetCount As Integer Dim datatoFind Sub Button1_Click() Find_File End Sub Private Su

我需要创建一个宏,在包含字符串的选定单元格列表中循环,并获取每个字符串,然后在整个工作簿中搜索该字符串。如果找到该字符串,则应退出子字符串,并选择找到的字符串,否则将继续查找下一个字符串,直到列表结束

当我运行代码时,在列表中找到一个字符串时,子节点不会退出,也不会进入该单元格

Option Explicit

Dim sheetCount As Integer
Dim datatoFind

Sub Button1_Click()

Find_File

End Sub

Private Sub Find_File()
Dim c As Range
Dim counter As Integer
Dim currentSheet As Integer
Dim notFound As Boolean
notFound = True

For Each c In Selection.Cells
    On Error Resume Next
    currentSheet = ActiveSheet.Index
    datatoFind = StrConv(c.Value, vbLowerCase)
    If datatoFind = "" Then Exit Sub
    sheetCount = ActiveWorkbook.Sheets.Count
    If IsError(CDbl(datatoFind)) = False Then datatoFind = CDbl(datatoFind)
    For counter = 1 To sheetCount
        Sheets(counter).Activate
        Cells.Find(What:=datatoFind, After:=ActiveCell, LookIn:=xlValues, LookAt _
        :=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
        False, SearchFormat:=False).Activate
        If InStr(1, StrConv(ActiveCell.Value, vbLowerCase), datatoFind) Then
            notFound = False
            Sheets(counter).Activate
            Range("datatoFind").Select
            Exit For
        End If
    Next counter
    If notFound = True Then
        MsgBox ("Value not found")
        Sheets(counter).Activate
    Else
        Exit Sub
    End If
Next c
End Sub

非常感谢您的帮助

有很多问题。第一个是您正在搜索所有的工作表。第一个匹配可能在包含您的匹配列的工作表中


当在表格中循环时,排除包含要查找的项目列的表格…………还有其他问题

如果是我的代码,我会改变,但不会改变您的代码

Public Sub Find_File()
Dim c As Range
Dim counter As Integer
Dim currentSheet As Integer
Dim notFound As Boolean
Dim datatoFind As String
Dim sheetCount As Integer
Dim cellFound As Range
Dim cellToFind As Range
Dim originalSheet As Worksheet

notFound = True
Set originalSheet = ActiveSheet
For Each c In Selection.Cells
    On Error Resume Next
    currentSheet = ActiveSheet.Index
    Set cellToFind = c
    datatoFind = StrConv(c.Value, vbLowerCase)
    If datatoFind = "" Then Exit Sub
    sheetCount = ActiveWorkbook.Sheets.Count
    If IsError(CDbl(datatoFind)) = False Then datatoFind = CDbl(datatoFind)
    For counter = 1 To sheetCount
        Sheets(counter).Activate
        Set cellFound = Cells.Find(What:=datatoFind, After:=ActiveCell, LookIn:=xlValues, LookAt _
        :=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
        False, SearchFormat:=False)
        If Not cellFound Is Nothing Then
            If cellFound.Address <> cellToFind.Address And _
               cellFound.Parent.Name <> cellToFind.Parent.Name Then
                notFound = False
                cellFound.Activate
                GoTo WorksEnd
            End If
        End If
    Next counter
Next c
WorksEnd:
    If notFound = True Then
        originalSheet.Activate
        MsgBox ("Value not found")

    End If

End Sub
公共子查找文件()
调光范围
作为整数的Dim计数器
将currentSheet设置为整数
未找到布尔值
Dim datatoFind As字符串
Dim SHEETCUNT为整数
在范围内找到模糊的单元格
暗淡的单元格查找范围
将原始图纸变暗为工作表
notFound=True
设置原始图纸=活动图纸
对于选择单元格中的每个c
出错时继续下一步
currentSheet=ActiveSheet.Index
设置cellToFind=c
datatoFind=StrConv(c.Value,小写)
如果datatoFind=“”,则退出Sub
sheetCount=ActiveWorkbook.Sheets.Count
如果IsError(CDbl(datatoFind))=False,则datatoFind=CDbl(datatoFind)
对于计数器=1的纸张计数
工作表(计数器)。激活
Set cellfund=Cells.Find(What:=datatoFind,After:=ActiveCell,LookIn:=xlValues,LookAt_
:=xlPart,SearchOrder:=xlByRows,SearchDirection:=xlNext,MatchCase:=_
False,SearchFormat:=False)
如果没有,那就什么都不是了
如果cellFound.Address cellToFind.Address和_
cellFound.Parent.Name cellToFind.Parent.Name然后
notFound=False
找到手机,激活
转到工作端
如果结束
如果结束
下一个柜台
下一个c
工作结束:
如果notFound=True,则
原始表单。激活
MsgBox(“未找到值”)
如果结束
端接头

我希望这有助于理解

谢谢,伙计,这真的很有帮助!您帮助我解决了搜索我的列表所在的同一张工作表的问题,并找到了一种更干净的方式到达找到的单元格!