Hi,
I am prepping a spreadsheet using a macro that I have recorded. The macro does all general stuff like deleting columns and making specific words red.
There are two separate macro's as one piece of data is downloaded manually and the other is sent via email therefore, the deleted columns are in a different place etc. The spreadsheets appear different at the start but the end result will leave me with the data in exactly the same place for each version (downloaded/emailed)
I then run a second macro that makes all the red amounts in a certain column (D) a minus amount, finally I move the amounts that are black in the same column (D) 1 cell to the right and up 2.
The problem that I have encountered is when I perform the cell move macro on the downloaded data version.
It should act exactly the same as the email version but for some reason it fails telling me "Application-defined or object defined error"
The highlighted code it refers to is the ".Range("D" & i).Copy .Range("D" & i).Offset(-2, 1)" line
Code is below:
Any help would be greatly appreciated.Sub MoveCells() ' Dim i As Long Dim LR As Long Dim ws As Worksheet Set ws = Sheets("Sheet1") With ws LR = .Range("D" & .Rows.Count).End(xlUp).Row For i = 2 To LR If .Range("D" & i).Font.ColorIndex = 1 Then .Range("D" & i).Copy .Range("D" & i).Offset(-2, 1) End If Next i End With End Sub
Thank you
Last edited by jsho1; 04-19-2011 at 08:36 AM.
hi jsho1
you could remove the offset
.Range("D" & i).Copy Destination:=.Range("b" & i + 1)
regards pike
If the solution helped please donate here to the RSPCA
Sites worth visiting;
J&R Solutions - royUK
AJP Excel Information - Andy Pope
Spreadsheet Toolbox
VBA for smarties - snb
Hi Pike,
Thank you for your response. This seems to have moved the data one cell down and two to the left. I am looking to move the data one cell right and two cells up.
It also seems to have deleted the data that I had in cell B also.
oops
.Range("D" & i).Copy Destination:=.Range("E" & i - 2)
regards pike
If the solution helped please donate here to the RSPCA
Sites worth visiting;
J&R Solutions - royUK
AJP Excel Information - Andy Pope
Spreadsheet Toolbox
VBA for smarties - snb
Thank you,
Just tried it and I am now getting the original error message once again, not sure why it keeps failing although it runs perfectly when the data is in exactly the same place on another recorded macro
you will get an error if the data is in the second row as two rows up is off the sheet
Last edited by pike; 04-19-2011 at 06:34 AM.
regards pike
If the solution helped please donate here to the RSPCA
Sites worth visiting;
J&R Solutions - royUK
AJP Excel Information - Andy Pope
Spreadsheet Toolbox
VBA for smarties - snb
The first part of data for one version occurs in cell D4 and works fine. The data in the second version occurs in cell D11 but doesn't appear to work
whats the code? or a sample workbook
regards pike
If the solution helped please donate here to the RSPCA
Sites worth visiting;
J&R Solutions - royUK
AJP Excel Information - Andy Pope
Spreadsheet Toolbox
VBA for smarties - snb
New amended code appears as this
Sub MoveCells() ' Dim i As Long Dim LR As Long Dim ws As Worksheet Set ws = Sheets("Sheet1") With ws LR = .Range("D" & .Rows.Count).End(xlUp).Row For i = 2 To LR If .Range("D" & i).Font.ColorIndex = 1 Then .Range("D" & i).Copy Destination:=.Range("E" & i - 2) End If Next i End With End Sub
try..
For i = 3 To LR
regards pike
If the solution helped please donate here to the RSPCA
Sites worth visiting;
J&R Solutions - royUK
AJP Excel Information - Andy Pope
Spreadsheet Toolbox
VBA for smarties - snb
hmm. doesnt like the blank cells
so
For i = 2 To LR If .Range("D" & i).Font.ColorIndex = 1 And .Range("D" & i).Value <> "" Then
regards pike
If the solution helped please donate here to the RSPCA
Sites worth visiting;
J&R Solutions - royUK
AJP Excel Information - Andy Pope
Spreadsheet Toolbox
VBA for smarties - snb
It appears to now be working,
I deleted and re-did the recorded macro to mirror exactly the first recorded macro but this time i inserted rows so that there would be no difference from the original.
Fingers crossed and thank you for your help
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks