search列标题标签值

是否有可能从第一行(标题)search由另一个表格定义的值? 我需要“FName”作为一个列或值的范围,而不是单个单元格。

以下是我迄今能够工作的一个样本:

FName = Workbooks("IntChk.xlsm").Worksheets("Data").Range("B3") Set rngFound = Worksheets("File").Rows(1).Find(What:=FName, LookIn:=xlValues, LookAt:=xlWhole, _ SearchOrder:=xlByColumns, SearchDirection:=xlNext, MatchCase:=False) 

从另一个工作簿中识别search词后,您想要在该工作簿的第1行中find一个或多个匹配项(…?),并logging与匹配对应的列。

 Option Explicit Sub get_em_all() Dim fName As String, addr As String Dim rng As Range, fnd As Range 'get search criteria fName = Workbooks("IntChk.xlsm").Worksheets("Data").Range("B3") With ThisWorkbook '<~~ different from IntChk.xlsm...? With .Worksheets("File").Rows(1) 'perform first search Set fnd = .Rows(1).Find(What:=fName, MatchCase:=False, _ LookIn:=xlValues, LookAt:=xlWhole) 'was anything found If Not fnd Is Nothing Then 'record the first find Set rng = fnd addr = rng.Address 'loop and collect results until we arrive at the first find Do Set rng = Union(rng, fnd) Set fnd = .FindNext(after:=fnd) Loop Until addr = fnd.Address 'expand the found cells from the first row to the columns within the current region With .Parent.Cells(1, 1).CurrentRegion Set rng = Intersect(rng.EntireColumn, .Cells) End With 'report the address(es) of the cell(s) found Debug.Print rng.Address(0, 0) Else Debug.Print 'nothing found" End If End With End With End Sub 

编辑,以纠正一些“优化”错别字

我想你想从“标题”行中select所有单元格的值在另一个范围

如果这是你的目标,你可以尝试以下

 Option Explicit Function GetRange(fnameRng As Range, dataRng As Range) As Range Dim fName As String 'get search criteria fName = GetJoinFromRange(fnameRng) With dataRng .Rows(1).Insert With .Offset(-1).Resize(1) .FormulaR1C1 = "=if(isnumber(search(""-"" & R2C & ""-"" ,""" & fName & """)),1,"""")" .Value = .Value Set GetRange = .SpecialCells(xlCellTypeConstants)).Offset(1) End With .Rows(1).Offset(-1).EntireRow.Delete End With End Function Function GetJoinFromRange(rng As Range) As String If rng.Rows.Count > 1 Then GetJoinFromRange = "-" & Join(Application.Transpose(rng), "-") & "-" Else GetJoinFromRange = "-" & Join(rng, "-") & "-" End If End Function 

可以像下面这样用“主”子来调用

 Option Explicit Sub main() Dim fnameRng As Range, dataRng As Range, rngFound As Range Set fnameRng = Workbooks("IntChk.xlsm").Worksheets("Data").Range("B3:B6") '<== adapt it to your needs Set dataRng = ThisWorkbook.Worksheets("File").Range("B1:I1000") '<== adapt it to your needs Set rngFound = GetRange(fnameRng, dataRng) End Sub 

经过一个星期的试验和错误,我能够创build这个代码。 它运作良好,光。

 sub IntChk Dim i As Integer Lastcol = 5 For i = 1 To 1 For j = 1 To Lastcol MsgBox "Cell Value = " & Cells(j) & vbNewLine & "Column Number = " & j For Each c In Workbooks("IntChk.xlsm").Worksheets("Data").Range("A1:A50") If c.Value = Cells(j) Then MsgBox "Match" Match = "True" End If Next c Next j If Match = "True" Then MsgBox "Yes, True!" Else: MsgBox "not true ;(" End If Next I end sub