如何select具有不同文件结尾名称的打开的Excel文件
我有一个ExcelmacrosJobs in Lab.xlsx
查找特定的文件名Jobs in Lab.xlsx
并复制到其他macros文件。
但是,在某些情况下,文件命名可能会更改为Jobs in Lab (0 - 195).xlsx
。
如何将Excel左语法实现到下面的VBA中?
Sub Test() Windows("Jobs in Lab").Activate End Sub
您可以使用LIKE
运算符。
Sub Sample() Dim wb As Workbook For Each wb In Application.Workbooks If wb.Name Like "Jobs in Lab*" Then wb.Activate Exit Sub End If Next wb End Sub
该代码假定文件在同一个Excel实例中打开。
编辑 :
但是,如果你仍然想使用LEFT
那么你可以像这样使用它。 注意使用Ucase
。 它将文本转换为大写,然后进行比较。 你也可以使用LCASE
。
Sub Sample() Dim wb As Workbook For Each wb In Application.Workbooks If UCase(Left(wb.Name, 11)) = "JOBS IN LAB" Then wb.Activate Exit Sub End If Next wb End Sub