Hello all,
I am using Excel 2010. I am attempting to run a macro as a 'Before Save' event in a workbook named "FirstWorkbook.xlsm." Among other things, the macro opens up a separate workbook ("SecondWorkbook.xlsm"), copies a range of data from the first workbook, pastes the data into the second workbook, and then closes the second workbook. Here is the segment of code in question:
[TABLE="class: outer_border, width: 500"]
<tbody>[TR]
[TD] x = Dir("C:\DocumentLog\SecondWorkbook.xlsm")
Set Srcfile = ActiveWorkbook
Workbooks.Open "C:\DocumentLog\" & x
Set DstFile = ActiveWorkbook
Set Dstsht = DstFile.Worksheets("SubLog")
With Srcfile.Sheets("Register")
.Range("A1:G300").Copy Dstsht.Range("A1:G300")
End With
DstFile.Save
DstFile.Close
[/TD]
[/TR]
</tbody>[/TABLE]
The macro works fine when I run it as a 'Before Save' event by clicking the standard Excel save button. It also works fine when I run it directly from a command button (i.e. a button that is assigned to a macro containing the above-cited code). However, when I try to run it from a command button assigned to a macro that only contains the code "ThisWorkbook.Save"), it gives me a Run Time Error 9 (Subscript Out of Range). When I go to Debug, the following line of code is highlighted:
[TABLE="class: outer_border, width: 500"]
<tbody>[TR]
[TD] Set Dstsht = DstFile.Worksheets("SubLog")
[/TD]
[/TR]
</tbody>[/TABLE]
I have searched this website and others to find an answer to this problem, with no luck. I cannot understand why it will work when I run it from the Excel save button but not when I run it from a 'ThisWorkbook.save' command. I'm not expert on VBA and am trying to piece things together a simply as possible, so any help would be appreciated. Please let me know if I need to provide more information to assist you in understanding the problem.
Thanks!
Jake
I am using Excel 2010. I am attempting to run a macro as a 'Before Save' event in a workbook named "FirstWorkbook.xlsm." Among other things, the macro opens up a separate workbook ("SecondWorkbook.xlsm"), copies a range of data from the first workbook, pastes the data into the second workbook, and then closes the second workbook. Here is the segment of code in question:
[TABLE="class: outer_border, width: 500"]
<tbody>[TR]
[TD] x = Dir("C:\DocumentLog\SecondWorkbook.xlsm")
Set Srcfile = ActiveWorkbook
Workbooks.Open "C:\DocumentLog\" & x
Set DstFile = ActiveWorkbook
Set Dstsht = DstFile.Worksheets("SubLog")
With Srcfile.Sheets("Register")
.Range("A1:G300").Copy Dstsht.Range("A1:G300")
End With
DstFile.Save
DstFile.Close
[/TD]
[/TR]
</tbody>[/TABLE]
The macro works fine when I run it as a 'Before Save' event by clicking the standard Excel save button. It also works fine when I run it directly from a command button (i.e. a button that is assigned to a macro containing the above-cited code). However, when I try to run it from a command button assigned to a macro that only contains the code "ThisWorkbook.Save"), it gives me a Run Time Error 9 (Subscript Out of Range). When I go to Debug, the following line of code is highlighted:
[TABLE="class: outer_border, width: 500"]
<tbody>[TR]
[TD] Set Dstsht = DstFile.Worksheets("SubLog")
[/TD]
[/TR]
</tbody>[/TABLE]
I have searched this website and others to find an answer to this problem, with no luck. I cannot understand why it will work when I run it from the Excel save button but not when I run it from a 'ThisWorkbook.save' command. I'm not expert on VBA and am trying to piece things together a simply as possible, so any help would be appreciated. Please let me know if I need to provide more information to assist you in understanding the problem.
Thanks!
Jake