檢查此子。這應該適合你
Sub OpenFileIfDataExist()
On Error GoTo HarunErrHandler
Dim strWorkBook As Excel.Workbook
Dim SearRange As Range
Dim FSO As Object
Dim strFileName, strFilePath As String
Dim strID As String
Dim lnCheckID As Long
strFileName = InputBox("Enter your file name.", "File Name")
strFilePath = "C:\Reports\" & strFileName & ".xlsx"
Set FSO = CreateObject("scripting.filesystemobject")
If FSO.FileExists(strFilePath) = True Then
Set strWorkBook = GetObject(strFilePath)
Set SearRange = strWorkBook.Sheets(1).Range("A1:A1000")
strID = InputBox("Enter ID.", "ID Input")
lnCheckID = Application.WorksheetFunction.Match(strID, SearRange, 0)
If lnCheckID > 0 Then
Workbooks.Open (strFilePath)
End If
Else
MsgBox "File Name does not match"
End If
Exit Sub
HarunErrHandler:
MsgBox "File Name and ID does not match.", vbInformation, "Nothing Found"
End Sub
太棒了! harun24hr thx很多! –
你應該接受我的答案,因爲它適合你。選擇綠色我的答案。 – harun24hr