当我在ThisWorkbook模块中工作时,如何更新外部工作簿?

时间:2017-01-10 21:12:13

标签: excel vba save

我正在尝试创建一个宏,它将在保存当前工作簿时更新状态跟踪工作簿。当我将宏附加到按钮时,抓取数据,打开状态跟踪器,粘贴数据,然后关闭跟踪器的功能,当我将代码导入ThisWorkbook Excel对象,以便宏将在保存时运行当我试图选择其他工作簿时,它似乎打破了。

我的代码可以在下面找到。破解代码的错误可在第10行“Windows(”StatusTracker.xlsm“)中找到。表格(”数据“)。选择”带回运行时错误'9':下标超出范围。请注意,第二个工作簿确实打开,但由于错误没有任何反应。是的,我评论了很多。

代码在这里:

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
'Select the Summary tab for capturing data
Sheets("Summary Tab").Select
'Select data
ActiveSheet.Range("M12:M14").Select
'Selection.Copy
ActiveSheet.Range("A1").Select
'Open the status tracker and data tab
Workbooks.Open "C:\Users\USER\Desktop\FOLDER\StatusTracker.xlsm"
Windows("StatusTracker.xlsm").Sheets("Data").Select
'Select where the info is to be pasted and paste transposed
ActiveSheet.Range("G2").PasteSpecial Paste:=xlPasteValues
ActiveSheet.Range("G2:G4").Select
Selection.Copy
'Edit the location here to coincide with the destination location
ActiveSheet.Range("B4:D4").PasteSpecial Transpose:=True
'Format location to percentage
ActiveSheet.Range("B4:D4").NumberFormat = "0%"
'Select the original paste of the data and delete it
ActiveSheet.Range("G2:G4").Select
Selection.Delete
ActiveSheet.Range("A1").Select
'Save and close the Status Tracker
Workbooks("StatusTracker.xlsm").Close SaveChanges:=True
'Kill copy and paste command
Application.CutCopyMode = False
End Sub

我担心有人会告诉我,ThisWorkbook模块中调用的函数只会影响所选的工作簿,而你无法在它之外工作。如果是这种情况,有没有办法解决这个问题,以便我可以做到这一点?

0 个答案:

没有答案