打开excel并find值,返回下一列

我想创build一个打开Excel工作表的脚本,在'A'列中find一个值并返回'B'列中的文本! 在GE的iFIX中使用VBA! 当我想声明'MyValue'作为范围时,他给出了一个错误信息。 范围是未知的! 这是我的代码返回相同的列。 有人提供另一种解决scheme?

Private Sub OPEN_MSG_Click() Dim FindString$ Dim MyValue Dim objExcel, objsheet, WB,WS As Object 'Open excel file Set objExcel = CreateObject("Excel.Application") objExcel.Visible = False Set WB = objExcel.Workbooks.Open("C:\Program Files (x86)\Proficy\Proficy iFIX\ProjectBackup\Foutcode_Opgezuiverd.xlsx") WB.ACTIVATE Set WS = WB.Worksheets("Blad1") 'Set objsheet = objExcel.ActiveWorkbook.Worksheets(1) FindString = InputBox("Enter a Search value") With WS.Range("A1:A800") Set MyValue = .Find("FindString", LookIn:=xlValues) If Not MyValue Is Nothing Then MsgBox MyValue.Column MsgBox MyValue.row End If End With ' Save the spreadsheet and close the workbook. objExcel.ActiveWorkbook.Close ' Quit Excel. objExcel.Application.Quit End Sub 

在find方法中,要求它以string的formssearch“FindString”。 既然你想查找FindString中的值,你应该删除“”。

  Set MyValue = .Find(FindString, LookIn:=xlValues) 

您可能还想要将Findstring声明为string以避免find方法的错误。