+ Reply to Thread
Results 1 to 12 of 12

Thread: Cell move?

  1. #1
    Registered User
    Join Date
    04-18-2011
    Location
    London, England
    MS-Off Ver
    Excel 2003
    Posts
    11

    Cell move?

    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:


    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
    Any help would be greatly appreciated.

    Thank you
    Last edited by jsho1; 04-19-2011 at 08:36 AM.

  2. #2
    Forum Guru pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2010
    Posts
    5,155

    Re: Cell move?

    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

  3. #3
    Registered User
    Join Date
    04-18-2011
    Location
    London, England
    MS-Off Ver
    Excel 2003
    Posts
    11

    Re: Cell move?

    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.

  4. #4
    Forum Guru pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2010
    Posts
    5,155

    Re: Cell move?

    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

  5. #5
    Registered User
    Join Date
    04-18-2011
    Location
    London, England
    MS-Off Ver
    Excel 2003
    Posts
    11

    Re: Cell move?

    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

  6. #6
    Forum Guru pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2010
    Posts
    5,155

    Re: Cell move?

    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

  7. #7
    Registered User
    Join Date
    04-18-2011
    Location
    London, England
    MS-Off Ver
    Excel 2003
    Posts
    11

    Re: Cell move?

    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

  8. #8
    Forum Guru pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2010
    Posts
    5,155

    Re: Cell move?

    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

  9. #9
    Registered User
    Join Date
    04-18-2011
    Location
    London, England
    MS-Off Ver
    Excel 2003
    Posts
    11

    Re: Cell move?

    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

  10. #10
    Forum Guru pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2010
    Posts
    5,155

    Re: Cell move?

    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

  11. #11
    Forum Guru pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2010
    Posts
    5,155

    Re: Cell move?

    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

  12. #12
    Registered User
    Join Date
    04-18-2011
    Location
    London, England
    MS-Off Ver
    Excel 2003
    Posts
    11

    Re: Cell move?

    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

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.2.0