Consulting

Results 1 to 5 of 5

Thread: Solved: Modify Copy to spreadsheet

  1. #1
    VBAX Expert
    Joined
    Apr 2007
    Location
    Orlando, FL
    Posts
    751
    Location

    Solved: Modify Copy to spreadsheet

    I have the following code.

    [vba]Sub TotalsReport()
    Dim w As Worksheet
    Dim Ash As Worksheet
    Dim Ws2 As Worksheet
    Dim Dest As Range
    Dim LastRow As Integer

    Set Ash = ActiveSheet

    Set Ws2 = Sheets("Totals")

    For Each w In ThisWorkbook.Worksheets

    If w.Name <> Ws2.Name Then

    w.Range("Y2").Resize(w.Range("Y2").End(xlDown).Row - 1, 2).Copy

    Set Dest = Ws2.Range("a" & Rows.Count).End(xlUp).Offset(1)
    Dest.PasteSpecial Paste:=xlPasteValues
    End If
    Next w

    Ash.Select

    Application.CutCopyMode = False

    Set w = Nothing

    End Sub
    [/vba]

    It works fine, but I am getting a run-time error '1004', I believe it happens when there is no values beyond Y2 and when there is no values at all in Y2 or beyond.

    How can i modify this code so in the case if Y2 is has a value but no other values in Y to just copy Y2. Also if Y2 has no value to skip that sheet and continue with code.

  2. #2
    VBAX Expert shrivallabha's Avatar
    Joined
    Jan 2010
    Location
    Mumbai
    Posts
    750
    Location
    Hi,

    Checking downwards is little unsafe instead you can try the opposite:
    [VBA]Dim lLastRow as Long
    lLastRow = Range("Y" & Rows.Count).End(xlUp).Row
    If lLastRow >= 2 then
    'Write Copy Code Here
    End if[/VBA]
    Regards,
    --------------------------------------------------------------------------------------------------------
    Shrivallabha
    --------------------------------------------------------------------------------------------------------
    Using Excel 2016 in Home / 2010 in Office
    --------------------------------------------------------------------------------------------------------

  3. #3
    VBAX Expert
    Joined
    Apr 2007
    Location
    Orlando, FL
    Posts
    751
    Location
    Ok, but my copy code
    [vba] w.Range("Y2").Resize(w.Range("Y2").End(xlDown).Row - 1, 2).Copy[/vba]

    Would still give an error correct?
    I think I would need new copy code.

  4. #4
    VBAX Expert shrivallabha's Avatar
    Joined
    Jan 2010
    Location
    Mumbai
    Posts
    750
    Location
    Untested:
    [VBA]Sub TotalsReport()
    Dim w As Worksheet
    Dim Ash As Worksheet
    Dim Ws2 As Worksheet
    Dim Dest As Range
    Dim LastRow As Integer

    Set Ash = ActiveSheet
    Set Ws2 = Sheets("Totals")

    For Each w In ThisWorkbook.Worksheets
    If w.Name <> Ws2.Name Then
    LastRow = w.Range("Y" & Rows.Count).End(xlUp).Row
    If LastRow >= 2 Then
    w.Range("Y2").Resize(LastRow - 1, 2).Copy
    Set Dest = Ws2.Range("a" & Rows.Count).End(xlUp).Offset(1)
    Dest.PasteSpecial Paste:=xlPasteValues
    End If
    End If
    Next w

    Ash.Select
    Application.CutCopyMode = False
    Set w = Nothing
    Set Ash = Nothing
    Set Dest = Nothing
    End Sub[/VBA]
    Regards,
    --------------------------------------------------------------------------------------------------------
    Shrivallabha
    --------------------------------------------------------------------------------------------------------
    Using Excel 2016 in Home / 2010 in Office
    --------------------------------------------------------------------------------------------------------

  5. #5
    VBAX Expert
    Joined
    Apr 2007
    Location
    Orlando, FL
    Posts
    751
    Location
    That seemed to work.

    Thanks again Shrivallabha.

Posting Permissions

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