Excel VBA中如何偏移索引匹配结果

如果在Sheet1上的string中findSheet2!A2的值!A:A,则Sheet1!“B2”= Offset(Sheet2!“A2”,0,1)即,Sheet2上的一系列值属于在Sheet1范围内的值。 如果在Sheet1上的值的范围中findSheet2上的值,那么我希望我的结果成为Sheet1上的相邻值,作为Sheet2的相邻值。 这在VBA中如何最好地完成? 例

这是我需要将公式合并到代码代码现在function

Sub ListAllFiles() Dim objShell As Object Dim objFolder As Object Dim objFile As Object Dim ws As Worksheet Set ws = Sheets("Sheet1") Dim myExt1 As String Dim myExt2 As String myExt1 = "pptx" myExt2 = "pdf" Set ws = Sheets("Sheet1") Set objShell = CreateObject("Shell.Application") 'Get the folder object associated with the directory Set objFolder = objShell.Namespace("\\sitefs\www\htdocs\c13\comm\IMS\Schedules\") Dim objFolderItems As FolderItems Dim objFolderItem As FolderItem Set objFolderItems = objFolder.Items() 'Loop through the Files collection Dim fOwner As String Dim fAuthor As String Dim dlm As String ws.UsedRange.Clear ws.Cells(1, 1).Value = "The current files found in " & objFolder & " are:" ws.Cells(1, 2).Value = "Date Last Modified" ws.Cells(1, 3).Value = "Owner" ws.Cells(1, 5).Value = "Report Date:" ws.Cells(1, 6).Value = Now Range("A1").EntireRow.Font.Bold = True For Each objFile In objFolder.Items dlm = objFolder.GetDetailsOf(objFile, 3) If InStr(objFile.Name, "FINAL") Then GoTo 50 If Year(CDate(dlm)) < Year(Now) Then GoTo 50 If InStr(objFile.Name, myExt1) Or InStr(objFile.Name, myExt2) Then Dim x As Integer x = ws.UsedRange.Rows.Count + 1 ws.Cells(x, 1).Value = objFile.Name 'Link cell to file Cells(x, 1).Select Selection.Hyperlinks.Add Anchor:=Selection, Address:=objFile.Path ws.Cells(x + 0, 2).Value = dlm End If 50 Next 'Added this and it's now working Dim oRng As Range Dim cRng As Range x = ws.UsedRange.Rows.Count Set oRng = Range("C2") Set cRng = Range("C2:C" & x) oRng.FormulaArray = _ "=OFFSET(INDEX(Sheet2!$A$2:$A$19,MATCH(TRUE,INDEX((ISNUMBER(SEARCH(Sheet2!$A$2:$A$19,a2))),),0)),,2)" cRng.FillDown Columns("A:B").Sort key1:=Range("B2"), _ order1:=xlDescending, Header:=xlYes Columns.AutoFit 'Clean up! Set objFolder = Nothing Set objFile = Nothing Set objFSO = Nothing Set oRng = Nothing Set cRng = Nothing End Sub 

此公式将在工作表1中的值中find工作表2中的值,并返回相应的值:

 =INDEX(Sheet2!$B$2:$B$9,MATCH(TRUE,INDEX((ISNUMBER(SEARCH(Sheet2!$A$2:$A$9,A2))),),0)) 

在这里输入图像说明

发生什么事:

内部INDEX正在创build一个TRUE / FALSE数组,在这种情况下是8个对象。

因为它迭代Sheet2上的值,它会返回一个数字或一个错误。

ISNUMBER根据该结果返回TRUE / FALSE。

然后,MATCH遍历数组,直到findTRUE,并将该索引返回到外部索引公式。

然后返回该数组中位于MATCH索引处的值。

你的主要想法是,如果你有“德克萨斯州”在表1,列。 A,你想要“加德纳”在上校。 B'

您可以在Sheet1 B2单元格中使用此公式:

=INDEX(Sheet2!$B$2:$B$4,MATCH(TRIM(LEFT(A2,SEARCH(" ",A2))),Sheet2!$A$2:$A$4,0))

(根据需要扩大范围)