Excel中的macros(VBA)在同事PC上失败,但在同事login时工作
我正在开发VBA工具来自动执行一系列冗长的pipe理任务,在以下情况下代码运行良好。
- 当我login到我的电脑
- 当我的同事login到我的电脑
- 当我login到我的同事电脑
然而,当我的同事在她的个人电脑上运行它时,它没有正确完成。
它正在失败的具体领域是:
'creates 2 dims for location of the two files that need opening based on the critera set on the home page Dim newdata As String newdata = Range("f11").Value Dim olddata As String olddata = Range("f12").Value Dim fileextension As String fileextension = Range("f14").Value Dim fulllocationolddata As String fulllocationolddata = Range("f13") & olddata & fileextension Dim fulllocationnewdata As String fulllocationnewdata = Range("f13") & newdata & fileextension 'open file containing OLDDATA c&p previous days data to the conversion tool 'then shuts the old data workbook Workbooks.Open Filename:=fulllocationolddata Workbooks(olddata).Activate Worksheets("sheet1").Select Range("A1").CurrentRegion.Copy Workbooks("Stockfile Conversion Tool.xlsm").Activate Sheets("OLD STOCK").Activate Range("A3").Select Selection.PasteSpecial Workbooks(olddata).Activate Worksheets("sheet1").Select Workbooks(olddata).Close SaveChanges:=False
最后一行(Workbooks(olddata).Close SaveChanges:= False)不会closures工作簿,然后在macros中打开另一个同名的工作簿,但已经打开它只是激活窗口和代码的其余部分分崩离析。
如果有人有任何想法,我会出错,将不胜感激。
提前感谢你的帮助
Plan303
在这里给我一个答案的评论:
Workbooks("Name of the Workbook")
是否正常工作取决于系统控制 – 文件夹选项 – 查看 – [x]中的设置隐藏已知文件types的扩展名 。
如果这设置,那么Excel的文件扩展名.xlsx
, .xlsm
,…在资源pipe理器或其他文件列表中不可见。 只有Excel文件的文件名是可见的。 如果是这样,那么Workbooks("Name of the Workbook")
将起作用。
如果未设置Hide extensions for known file types
的扩展名,则Excel的文件扩展名.xlsx
, .xlsm
,…在资源pipe理器或其他文件列表中可见。 如果是这样,那么Workbooks("Name of the Workbook")
将不起作用。 然后只有Workbooks("Name of the Workbook.xlsx")
,给出的名称和扩展名,将工作。
但是Workbooks("Name of the Workbook.xlsx")
如果设置Hide extensions for known file types
Workbooks("Name of the Workbook.xlsx")
也可以工作。 所以使用全名包含扩展名应该是首选。
所以对于具体的问题:
如果olddata
只包含工作簿的名称,而不包含文件扩展名,那么Workbooks(olddata)
只有在Folder Options
设置Hide extensions for known file types
时才有效。 如果没有设置该选项并且文件扩展名在资源pipe理器中可见,则会失败。 但Workbooks("Stockfile Conversion Tool.xlsm")
将始终工作,无论是否设置Hide extensions for known file types
。 因此,如果olddata
仅包含工作簿的名称,并且fileextension
包含.xlsx
, Workbooks(olddata & fileextension)
也应该始终有效。