Hello,
I'm very new to VBA and decided to try and create a macro that loops through a selection of columns (in sheet "Data") and pastes them, one at a time, into a column in another sheet "CalcSheet". Once I'm successful I plan to do some work on the column while it's in sheet "CalcSheet".
My challenge is that I can step through the macro once (but not twice), however it won't run at all from the spreadsheet.
My code is as follows:
Sub CopyCols()
Dim aa As Range
Dim cc As Range
Set cc = Columns("p:s")
For Each aa In cc
Sheets("Data").Select
aa.Select
Application.CutCopyMode = False
Selection.Copy
Sheets("CalcSheet").Select
Columns("d:d").Select
ActiveSheet.Paste
Next
End Sub
When it fails it fails at the line 'aa.select' with an error message 'Select Method of Range Class Failed'.
When it steps through the first time everything works fine for the four columns (p to s). If I continue stepping it fails as mentioned above. It will run once with F5 but fails on a second attempt. The worse part is that it fails immediately if I try and run it from the spreadsheet.
Is it my For Each loop? Or is it some other programming error I've created.
Many thanks, Anthony
I'm very new to VBA and decided to try and create a macro that loops through a selection of columns (in sheet "Data") and pastes them, one at a time, into a column in another sheet "CalcSheet". Once I'm successful I plan to do some work on the column while it's in sheet "CalcSheet".
My challenge is that I can step through the macro once (but not twice), however it won't run at all from the spreadsheet.
My code is as follows:
Sub CopyCols()
Dim aa As Range
Dim cc As Range
Set cc = Columns("p:s")
For Each aa In cc
Sheets("Data").Select
aa.Select
Application.CutCopyMode = False
Selection.Copy
Sheets("CalcSheet").Select
Columns("d:d").Select
ActiveSheet.Paste
Next
End Sub
When it fails it fails at the line 'aa.select' with an error message 'Select Method of Range Class Failed'.
When it steps through the first time everything works fine for the four columns (p to s). If I continue stepping it fails as mentioned above. It will run once with F5 but fails on a second attempt. The worse part is that it fails immediately if I try and run it from the spreadsheet.
Is it my For Each loop? Or is it some other programming error I've created.
Many thanks, Anthony