I have been trying to get a macro to work that decants data from one sheets of an excel workbook to the other sheets.
My macro worked for filtering the "master" sheet to show only the data to be decanted to each relevant tab. and my code to copy the filtered rows worked :-)
However, whenever I switched sheets to select cells in order to clear in the receiving sheet, the code crashed.
I went back to basics and setup a simple workbook with 2 sheets and simple data. I then recorded a macro to clear sheet1 then copy manually filtered data from sheet2 to sheet1. Success it worked :-)
I then setup a command button on Sheet2 and pasted the recorded code into its Commandbutton click sub.
When I try to run the code by clicking the command button, it fails at the first cells.select command! :-(
I haven't worked out yet how to upload the simple test workbook.
I went back to basics and setup a simple workbook with 2 sheets and simple data. I then recorded a macro to clear sheet1 then copy manually filtered data from sheet2 to sheet1. Success it worked :-)
I then setup a command button on Sheet2 and pasted the recorded code into its Commandbutton click sub.
When I try to run the code by clicking the command button, it fails at the first cells.select command! :-(
This is the recorded macro which works:
Sub testcopy()
'
' testcopy Macro
' test copy between sheets
'
'
Sheets("Sheet1").Select
Cells.Select
Selection.ClearContents
Sheets("Sheet2").Select
Range("A1").Select
Range(Selection, Selection.End(xlDown)).Select
Range("A1:D500").Select
Selection.Copy
Sheets("Sheet1").Select
Range("A1").Select
ActiveSheet.Paste
End Sub
This is the same code pasted into CommandButton1_Click which fails at the first Cells.Select command
Sub CommandButton1_Click()
'
' testcopy Macro
' test copy between sheets
'
'
Sheets("Sheet1").Select
Cells.Select
Selection.ClearContents
Sheets("Sheet2").Select
Range("A1").Select
Range(Selection, Selection.End(xlDown)).Select
Range("A1:D500").Select
Selection.Copy
Sheets("Sheet1").Select
Range("A1").Select
ActiveSheet.Paste
End Sub
Error message on crashing is :
Runtime error '1004':
Application-defined or object-defined error
And the debugger highlights the cells.select
row