打开 excel 并查找值,返回下一列



我想创建一个打开 excel 作品的脚本,在"A"列中找到一个值并返回"B"列中的文本! 在 GE 的 iFIX 中使用 VBA! 当我想将"MyValue"声明为范围时,他给出了一条错误消息。 范围未知! 这是我返回同一列的代码。有人给另一个解决方案吗?

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)ProficyProficy iFIXProjectBackupFoutcode_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 方法中,您要求它搜索"FindString"作为字符串。由于您想在 FindString 中查找值,因此您应该删除 "。

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

您可能还希望将 Findstring 声明为字符串,以避免 find 方法出错。

最新更新