我有一本从不同工作簿复制信息的工作簿。要打开这些工作簿,请使用以下代码:
Dim nomearq As String
Dim nomearq2 As String
nomearq = Application.GetOpenFilename
Workbooks.Open Filename:=nomearq
nomearq2 = ActiveWorkbook.Name
因此,如果所选文件不是excel文件,我试图不允许宏运行:
If Not Right(nomearq, 4) = ".xls" Or Right(nomearq, 5) = ".xlsm" Then
MsgBox "Arquivo incompatível"
Exit Sub
但是xlsm文件没有打开。我不知道为什么。
或者,您可以将用户限制为只能选择Excel文件,如下所示:
Sub tgr()
Dim nomearq As Variant
nomearq = Application.GetOpenFilename("Excel Files, *.xl??") 'This specifies that it can ONLY open Excel files
If VarType(nomearq) = vbBoolean Then Exit Sub 'Pressed cancel
With Workbooks.Open(nomearq)
'do stuff with the workbook here
MsgBox "opened " & nomearq
.Close SaveChanges:=False 'Close the opened workbook when you're done with it
End With
End Sub
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句