从多个单元格列中找出一个值,并在下一个空列中返回“X”

我试图把一个“X”或什么在下一个空列,我以后可以使用INDEXINDERECT (因为工作表被命名为与我的主表中列A的范围相同)查找我的主要表格 需要在每个find该值的工作表中添加“X”。

在表格中,我需要find的数字总是在列A中。在我的主表中,值从B2:B23列出。 范围在每张纸上(从400行到5000行)不等。

有没有一个聪明的方法来做到这一点,我还没有find?

atm有80张纸和一张主纸

码:

 Sub Mark_cells_in_column() Dim FirstAddress As String Dim MyArr As Variant Dim Rng As Range Dim I As Long With Application .ScreenUpdating = False .EnableEvents = False End With 'Search for a Value Or Values in a range MyArr = Array("34-2472", "36-437", "36-4351", "36-4879", "36-4982", "36-4981" _ , "36-5715", "36-4983", "36-4984", "36-5125", "36-5126", "36-5257", "36-6139" _ , "38-7079-1", "38-7079-2", "44-1276", "31-8589", "31-8589-1", "31-8647", "36-6149" _ , "36-5770", "31-8590") 'Search Column or range With Sheets("3").Range("A:A") 'cant get my head around how to get this to apply so it loops through every sheet except main sheet 'clear the cells in the column to the right .Offset(0, 13).ClearContents For I = LBound(MyArr) To UBound(MyArr) 'If you want to find a part of the rng.value then use xlPart 'if you use LookIn:=xlValues it will also work with a 'formula cell that evaluates to "values listed" Set Rng = .Find(What:=MyArr(I), _ After:=.Cells(.Cells.Count), _ LookIn:=xlValues, _ LookAt:=xlWhole, _ SearchOrder:=xlByRows, _ SearchDirection:=xlNext, _ MatchCase:=False) If Not Rng Is Nothing Then FirstAddress = Rng.Address Do 'mark the cell in the column to the right if "Values listed" is found Rng.Offset(0, 13).Value = "X" Set Rng = .FindNext(Rng) Loop While Not Rng Is Nothing And Rng.Address <> FirstAddress End If Next I End With With Application .ScreenUpdating = True .EnableEvents = True End With End Sub 

干得好:

 Sub Mark_cells_in_column() Dim FirstAddress As String Dim MyArr As Variant Dim Rng As Range Dim I As Long Dim mainWS As Worksheet, ws As Worksheet With Application .ScreenUpdating = False .EnableEvents = False End With Set mainWS = Sheets("Main") ' Change this to whatever the name of your Main WS is, that you DON'T want to run the macro on 'Search for a Value Or Values in a range MyArr = Array("34-2472", "36-437", "36-4351", "36-4879", "36-4982", "36-4981" _ , "36-5715", "36-4983", "36-4984", "36-5125", "36-5126", "36-5257", "36-6139" _ , "38-7079-1", "38-7079-2", "44-1276", "31-8589", "31-8589-1", "31-8647", "36-6149" _ , "36-5770", "31-8590") ' Loop through Sheets For Each ws In Worksheets If ws.Name <> mainWS.Name Then With ws 'Search Column or range With .Range("A:A") 'clear the cells in the column to the right 13 columns (aka column N) .Offset(0, 13).ClearContents For I = LBound(MyArr) To UBound(MyArr) 'If you want to find a part of the rng.value then use xlPart 'if you use LookIn:=xlValues it will also work with a 'formula cell that evaluates to "values listed" Set Rng = .Cells.Find(What:=MyArr(I), _ After:=.Cells(1, 1), _ LookIn:=xlValues, _ LookAt:=xlWhole, _ SearchOrder:=xlByRows, _ SearchDirection:=xlNext, _ MatchCase:=False) If Not Rng Is Nothing Then FirstAddress = Rng.Address Do 'mark the cell in the column to the right if "Values listed" is found Rng.Offset(0, 13).Value = "X" ' This marks it in 13 columns to the right where the value is found Set Rng = .Columns("A:A").FindNext(Rng) Loop While Not Rng Is Nothing And Rng.Address <> FirstAddress End If Next I End With ' Ends the .Range("A:A") End With ' ends the `with WS` End If Next ws With Application .ScreenUpdating = True .EnableEvents = True End With End Sub 

主要的东西似乎是你正在使用SearchDirection:=xlNext最后一个单元格( After:=.Cells(.Cells.Count) )。 没有下一个细胞,如果你在最后! 所以,我把它改成After:=.Cells(1,1)

其次,我添加了一个循环来检查工作表,如果它是“主”,跳过它。 根据需要编辑。