EXCEL VBA:使用 vba 打开 XLSM excel

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27834972/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 05:45:12  来源:igfitidea点击:

EXCEL VBA: opening XLSM excel using vba

excelvbaexcel-vba

提问by pretzels04

i cant figure out my error in my codes

我无法弄清楚代码中的错误

If cboUnit.Text = "MARINER" Then
Application.ScreenUpdating = False
Application.Workbooks.Open Filename:=ThisWorkbook.Path & "\UNIT" & "\MARINER"
ThisWorkbook.Activate
Workbooks("TOUKEI DATA REPORT GRAPHING").Close
End If

i just want to open xlsm file which is on the folder unit but i always got an error:

我只想打开文件夹单元上的 xlsm 文件,但我总是遇到错误:

runtime error '1004' the file this getting is 'xlsx' extension

运行时错误 '1004' 得到的文件是 'xlsx' 扩展名

回答by peege

If you use something like this, it should at least make debugging simpler. Then substitute the code I've provided with this line:

如果你使用这样的东西,它至少应该使调试更简单。然后替换我在这一行中提供的代码:

Application.Workbooks.Open Filename:=ThisWorkbook.Path & "\UNIT" & "\MARINER"  

Declare the variables with your other variables. I'm understanding your question to be that "Mariner" is the file name. If it's not, you will need to change the fileNPath.

使用其他变量声明变量。我理解你的问题是“水手”是文件名。如果不是,您将需要更改 fileNPath。

Dim path As String, fileNPath As String

'add these two values above your code
path = ActiveWorkbook.path
fileNPath = path & "\UNIT\MARINER.xlsm"

Application.Workbooks.Open fileName:=fileNPath   'Change this line 

Once you've done this, you can see the values for the file path in debug mode where the version you have won't have a value until the line that isn't working anyway. So this way, you can SEE what is going on before you try to USE it.

完成此操作后,您可以在调试模式下看到文件路径的值,在该模式下,您拥有的版本将没有值,直到无论如何都不起作用的行。因此,通过这种方式,您可以在尝试使用它之前看到发生了什么。

note:To have a macro enabled workbook, it must be .xlsm, not .xlsx

注意:要启用宏的工作簿,它必须是 .xlsm,而不是 .xlsx