searchstring并查找相邻列的内容

我有一个名为“指示”的excel工作表在同一个目录中,其中包含我的macros的Excel表“工具”在那里。 excel表格显示有大约400行和20列的数据。

我有一个macros,将采取从用户的stringinput(例如:DUMMY_TEXT),并运行另一个macros。 另一个macros是我被击中的地方。

我必须在D列的Excel表格“指示”中searchstringinputDUMMY_TEXT,并在findstringinput的相应行中查找列Q,R,S和T的内容。 这必须根据用户inputdynamic地发生。

我很惊讶地发现Q,R,S和T列的内容。这就是我现在所拥有的

Dim FoundCell As Excel.Range temppath = ActiveWorkbook.Path Workbooks.Open (temppath + "\indications.xlsx") Set FoundCell = Range("D1:D20000").Find(what:=LSearchValue, lookat:=xlWhole) Workbooks("indications.xlsx").Close 

对于lookat:=xlWhole匹配的单个列,我更喜欢使用MATCHfunction的Excel应用程序对象使用Range.Find方法 。

 Sub trwqoeiryg() Dim findThis As Variant, rw As Variant, wb As Workbook Dim strQ as string, strR as string, strS as string, strT as string findThis = "find This" 'VB/VBA uses an ampersand as the preferred string concatenation operator Set wb = Workbooks.Open(Filename:=temppath & "\Indications.xlsx", ReadOnly:=True) With wb With .Worksheets(1) '<~~set this properly! rw = Application.Match(findThis, .Columns("D"), 0) If Not IsError(rw) Then Debug.Print .Cells(rw, "Q").Value Debug.Print .Cells(rw, "R").Value Debug.Print .Cells(rw, "S").Value Debug.Print .Cells(rw, "T").Value strQ = .Cells(rw, "Q").Value strR = .Cells(rw, "R").Value strS = .Cells(rw, "S").Value strT = .Cells(rw, "T").Value Debug.Print strQ Debug.Print strR Debug.Print strS Debug.Print strT Else Debug.Print findThis & " not found." End If End With .Close SaveChanges:=False End With End Sub 

一旦通过MATCH返回的行数, Range.Cells属性可以很容易地从同一行的任何列中返回值。