Consulting

Results 1 to 6 of 6

Thread: Paste with offset from a different sheet VBA

  1. #1
    VBAX Newbie
    Joined
    Feb 2016
    Posts
    1
    Location

    Question Paste with offset from a different sheet VBA

    Hi everyone, what offset modification do I need to do to the code so it pastes the data starting from column D?

    Sub copypaste()
    Dim Source As Worksheet, Destination As Worksheet, lr As Long, rng As Range
    Set Source = Sheets("Query")
    Set Destination = Sheets("1. Detail")
    lr = Source.Cells(Rows.Count, 1).End(xlUp).Row
    Set rng = Source.Range("A4:A" & lr).EntireRow
    rng.EntireRow.Copy
    Destination.Cells(Rows.Count, 1).End(xlUp)(2).PasteSpecial xlPasteValues
    End Sub

    Thanks.

  2. #2
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,876
    one of:
    Destination.Cells(Rows.Count, 1).End(xlUp)(2,4).paste…
    Destination.Cells(Rows.Count, 4).End(xlUp)(2).paste…
    depending on whether you want the destination row to be determined from column A or D respectively.
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  3. #3
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    Sub M_snb()
       with Sheets("Query").cells(1).currentregion.columns(1).offset(3)
           Sheets("1. Detail").Cells(Rows.Count, 1).End(xlUp).offset(2).resize(.rows.count)=.value
       end with
    End Sub

  4. #4
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    @ p45cal

    Is this a new feature, or an undocumented trick
    Dim d3 As Range
    Set d3 = Range("A1")(2, 4)
    It is not OOPs style notation
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  5. #5
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,876
    Quote Originally Posted by SamT View Post
    Is this a new feature, or an undocumented trick[CODE]
    Neither. The default property of the range object is .Item and it "Returns a Range object that represents a range at an offset to the specified range." So you don't need to explicitly use .Item.
    See https://msdn.microsoft.com/en-us/library/office/ff196273.aspx which tells you .item is the default property
    and https://msdn.microsoft.com/en-us/library/office/ff197454.aspx showing how you can use it.
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  6. #6
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    Thanks.
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

Posting Permissions

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