目前正在一个项目中,我需要查找选择的文件是否正确。所以我使用 if 条件进行部分匹配。下面是代码供参考。代码给出运行时错误“424”对象需要。
`
Sub Fetch_Files()
Dim OB1 As Workbook, FTO As Variant,strSheetName as string
Application.ScreenUpdating = False
FTO= Application.GetOpenFilename(Title:="Browse For your File & Import Range", FileFilter:="Excel Files (*.xlsx*),*xlsx*")
If FTO <> False then
Set OB1 = Application.Workbooks.Open(FTO)
OB1.Worksheets(1).Activate
strSheetName = ActiveSheet.Name
If strSheetName.value = "*abc*" Then
'Call abc
else
MsgBox ("Please abc file")
End If
Else
MsgBox ("Please Select Files")
Exit Sub
End If
Application.ScreenUpdating = True
End Sub
`
已找到解决方案。我使用过 OB1.Worksheet(1).Name 使用like关键字。
If FTO <> False then
Set OB1 = Application.Workbooks.Open(FTO)
If OB1.Worksheets(1).Name like "*abc*" Then ' improve code
'Call abc
else
MsgBox ("Please abc file")
End If
Else
MsgBox ("Please Select Files")
Exit Sub
End If
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句