On Error Resume Next
'假設股票數據在1,2行,用戶輸入股票名稱的單元格為“A4”
Set objRange = Range("A4")
'判斷用戶是否在A4中進行輸入
If Not Intersect(Target, objRange) Is Nothing Then
strInput = objRange.Value
'調用HLookUp對數據進行檢索
With Application.WorksheetFunction
strOutput = .HLookup(strInput, Range("1:2"), 2, 0)
End With
'檢索失敗進行提示
If strOutput = "" Then
MsgBox "無效輸入"
'檢索成功,新建Excel文檔,將用戶輸入數據和查詢結果賦值給指定單元格
Else
Set objExcelApp = CreateObject("Excel.Application")
objExcelApp.Visible = True
Set objWorkBook = objExcelApp.Workbooks.Add
Set objSheet = objWorkBook.Sheets.Item(1)
objSheet.Cells(1, 1).Value = strInput
objSheet.Cells(2, 1).Value = strOutput
objWorkBook.SaveAs "D:\test\Result.xls"
objWorkBook.Close
objExcelApp.Quit
Set objExcelApp = Nothing
End If
End If
End Sub
不知道這樣是否是妳想要的~