Hi folks,
Each month end, I create a copy of the last month's end file. as part of this process, all Report sheets (those that are sent out to the business) have the new month added to their tables. this is done by copying the previous month's (eg October) column, and inserting the copied cells in a new column adjacent to it. the previous month's formulas are then converted to values before the new month's raw data is imported.
I currently use this button fired routine to copy the whole column:
On a separate sheet with a different layout, I want to do the same sort of thing but with only a range, rather than the whole column.
This routine errors out at the insertion point. That is, at:
"Run time error 1004: Application defined or object defined error"
what syntax should i be using to get the current month range to copy into the adjacent range?
Each month end, I create a copy of the last month's end file. as part of this process, all Report sheets (those that are sent out to the business) have the new month added to their tables. this is done by copying the previous month's (eg October) column, and inserting the copied cells in a new column adjacent to it. the previous month's formulas are then converted to values before the new month's raw data is imported.
I currently use this button fired routine to copy the whole column:
Code:
Private Sub CommandButton1_Click()
Dim R As Range, BeforeR As Long
'Find 'Totals' in row 5 of active sheet
Set R = Rows(5).Find(what:="Totals", lookat:=xlWhole)
'identify the column to copy (last month)
BeforeR = R.Column - 1
If R Is Nothing Then
MsgBox ("The word 'Totals' was not found in Row 5 - macro terminated!")
Exit Sub
ElseIf Not R Is Nothing Then
'copy last month's column
Columns(BeforeR).Copy
'insert copied cells before the Totals column
Columns(R.Column).Insert Shift:=xlRight
'change last month's column to values
With Columns(BeforeR)
.Value = .Value
End With
Application.CutCopyMode = False
End If
End Sub
On a separate sheet with a different layout, I want to do the same sort of thing but with only a range, rather than the whole column.
Code:
Private Sub CommandButton6_Click()
Dim P As Range, BeforeP As Long, LstMth As Range
'Find 'Totals' in row 5 of active sheet
Set P = Rows(34).Find(what:="Totals", lookat:=xlWhole)
'MsgBox P.Address 'H34
'identify the column to copy (last month)
BeforeP = P.Column - 1
Set LstMth = ActiveSheet.Range(Cells(31, BeforeP), Cells(500, BeforeP)) '''copy G31:G500
'MsgBox BeforeP
If P Is Nothing Then
MsgBox ("The word 'Totals' was not found in Row 5 - macro terminated!")
Exit Sub
ElseIf Not P Is Nothing Then
'copy last month's column
LstMth.Copy
'insert copied cells before the Totals column
ActiveSheet.Cells(31, P.Column).Insert Shift:=xlRight '''insert H31
'change last month's column to values
With LstMth
.Value = .Value
End With
Application.CutCopyMode = False
End If
End Sub
This routine errors out at the insertion point. That is, at:
Code:
'insert copied cells before the Totals column
ActiveSheet.Cells(31, P.Column).Insert Shift:=xlRight
"Run time error 1004: Application defined or object defined error"
what syntax should i be using to get the current month range to copy into the adjacent range?
Last edited: