Consulting

Results 1 to 11 of 11

Thread: Solved: Problem with cut/paste to last row of sheets

  1. #1
    VBAX Expert CatDaddy's Avatar
    Joined
    Jun 2011
    Posts
    581
    Location

    Solved: Problem with cut/paste to last row of sheets

    [VBA]Sub WorkDivision_SortOthers()
    Dim MB As Workbook
    Set MB = Workbooks("MasterWorkbook.xls")
    MB.Sheets(1).Activate
    For Each cell In Range("F:F")

    Select Case cell.Value

    Case "AAA":
    cell.EntireRow.Cut Destination:=Sheets(8).Cells(Rows.Count, 2).End(xlUp).Row
    Case "BBB":
    cell.EntireRow.Cut Destination:=Sheets(4).Cells(Rows.Count, 2).End(xlUp).Row
    Case "CCC":
    cell.EntireRow.Cut Destination:=Sheets(6).Cells(Rows.Count, 2).End(xlUp).Row

    Case "DDD":
    cell.EntireRow.Cut Destination:=Sheets(2).Cells(Rows.Count, 2).End(xlUp).Row

    Case "EEE":
    cell.EntireRow.Cut Destination:=Sheets(5).Cells(Rows.Count, 2).End(xlUp).Row

    Case "FFF":
    cell.EntireRow.Cut Destination:=Sheets(4).Cells(Rows.Count, 2).End(xlUp).Row


    End Select
    Next cell
    End Sub
    [/VBA]

    I'm trying to get this macro to go through the first worksheets and send entire rows to the end of different worksheets based on the select case, but it doesnt send anything...something wrong with cut
    ------------------------------------------------
    Happy Coding my friends

  2. #2
    VBAX Contributor
    Joined
    May 2010
    Location
    Sydney, NSW, Australia
    Posts
    170
    Location
    This doesn't fix you probem but you probably want to offset that destination by 1 row otherwise you will overwrite the existing last row of data

  3. #3
    VBAX Expert shrivallabha's Avatar
    Joined
    Jan 2010
    Location
    Mumbai
    Posts
    750
    Location
    First guess:
    [VBA]cell.EntireRow.Cut Destination:=Sheets(8).Cells(Rows.Count, 2).End(xlUp).Row [/VBA]
    is incorrect and it should be cell like (as marked by blade hunter):
    [VBA]cell.EntireRow.Cut Destination:=Sheets(8).Cells(Rows.Count, 2).End(xlUp)(2)[/VBA]
    Regards,
    --------------------------------------------------------------------------------------------------------
    Shrivallabha
    --------------------------------------------------------------------------------------------------------
    Using Excel 2016 in Home / 2010 in Office
    --------------------------------------------------------------------------------------------------------

  4. #4
    VBAX Expert CatDaddy's Avatar
    Joined
    Jun 2011
    Posts
    581
    Location
    no...that doesn't work...

    Since I am copying an entire row shouldnt the destination be a row?
    ------------------------------------------------
    Happy Coding my friends

  5. #5
    VBAX Expert shrivallabha's Avatar
    Joined
    Jan 2010
    Location
    Mumbai
    Posts
    750
    Location

    Answer!

    No, thats incorrect. Row returns Row Number and NOT Row Object.

    Following is the correct syntax
    [VBA]Case "AAA":
    Rows(cell.Row).Cut (Sheets(8).Cells(Rows.Count, 2).Offset(, -1).End(xlUp)(2))[/VBA]

    This errors because Excel tries to paste the whole row starting from column 2 and gets spillover.
    Regards,
    --------------------------------------------------------------------------------------------------------
    Shrivallabha
    --------------------------------------------------------------------------------------------------------
    Using Excel 2016 in Home / 2010 in Office
    --------------------------------------------------------------------------------------------------------

  6. #6
    VBAX Expert CatDaddy's Avatar
    Joined
    Jun 2011
    Posts
    581
    Location
    is the (2) at the end what is giving it the error? I dont understand what you are trying to say and it still doesnt work
    ------------------------------------------------
    Happy Coding my friends

  7. #7
    VBAX Expert shrivallabha's Avatar
    Joined
    Jan 2010
    Location
    Mumbai
    Posts
    750
    Location
    Strange, it shouldn't give any error. I've posted it after testing. (2) ensures that the data is written on the Next Line otherwise the data will keep overwriting on the same row!

    Post your workbook then.
    Regards,
    --------------------------------------------------------------------------------------------------------
    Shrivallabha
    --------------------------------------------------------------------------------------------------------
    Using Excel 2016 in Home / 2010 in Office
    --------------------------------------------------------------------------------------------------------

  8. #8
    VBAX Contributor
    Joined
    May 2010
    Location
    Sydney, NSW, Australia
    Posts
    170
    Location
    Can you please paste this:

    [vba]
    ?Sheets(8).Cells(Rows.Count,2).Offset(, -1).End(xlUp)(2).address
    [/vba]

    in the debug window (CTRL-G) and press enter then tell us what the result is.

    Thanks

    Dan

  9. #9
    VBAX Master Aflatoon's Avatar
    Joined
    Sep 2009
    Location
    UK
    Posts
    1,720
    Location
    This:
    Sheets(8).Cells(Rows.Count, 2).Offset(, -1).End(xlUp)(2)
    returns the cell below the last used row in column A on sheet 8. Based on the initial post I suspect you want:
    Sheets(8).Cells(Rows.Count, 2).End(xlUp).offset(1, -1)
    which will find the last used row in column B then go one row down and one column left. It also should not be in parentheses.

    So, if we assume that the target sheet is also in MB (as it was not specified in the original code):
    [vba]Sub WorkDivision_SortOthers()
    Dim MB As Excel.Workbook
    Dim cell As Excel.Range

    Set MB = Workbooks("MasterWorkbook.xls")

    For Each cell In MB.Sheets(1).Range("F:F")

    Select Case cell.Value

    Case "AAA"
    cell.EntireRow.Cut Destination:=MB.Sheets(8).Cells(Rows.Count, 2).End(xlUp).Offset(1, -1)
    Case "BBB"
    cell.EntireRow.Cut Destination:=MB.Sheets(4).Cells(Rows.Count, 2).End(xlUp).Offset(1, -1)
    Case "CCC"
    cell.EntireRow.Cut Destination:=MB.Sheets(6).Cells(Rows.Count, 2).End(xlUp).Offset(1, -1)

    Case "DDD"
    cell.EntireRow.Cut Destination:=MB.Sheets(2).Cells(Rows.Count, 2).End(xlUp).Offset(1, -1)

    Case "EEE"
    cell.EntireRow.Cut Destination:=MB.Sheets(5).Cells(Rows.Count, 2).End(xlUp).Offset(1, -1)

    Case "FFF"
    cell.EntireRow.Cut Destination:=MB.Sheets(4).Cells(Rows.Count, 2).End(xlUp).Offset(1, -1)
    End Select
    Next cell
    End Sub
    [/vba]
    Be as you wish to seem

  10. #10
    VBAX Expert shrivallabha's Avatar
    Joined
    Jan 2010
    Location
    Mumbai
    Posts
    750
    Location
    Thanks Aflatoon. Apologies for my "Lost In Transfer" drivel.

    And Yes, it will work if Column A is also NON-BLANK as Column B but that will be BAD coding anyway!
    Regards,
    --------------------------------------------------------------------------------------------------------
    Shrivallabha
    --------------------------------------------------------------------------------------------------------
    Using Excel 2016 in Home / 2010 in Office
    --------------------------------------------------------------------------------------------------------

  11. #11
    VBAX Expert CatDaddy's Avatar
    Joined
    Jun 2011
    Posts
    581
    Location
    I see...its because it was in the second column that the row wouldnt paste (because there wasnt enough room for the row + another cell huh?)...seems like Aflatoon got it spot on for me! thanks everyone
    Last edited by CatDaddy; 07-28-2011 at 12:25 PM.
    ------------------------------------------------
    Happy Coding my friends

Posting Permissions

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