Consulting

Results 1 to 6 of 6

Thread: Using variables to reference a range

  1. #1

    Using variables to reference a range

    I want to copy from one row to another row using predefined variables (copying the entire row). In my code the references are determined by cell values but for simplicity sake I'm going to use counters (if the code below can be modified in a way that works I'll be able to figure out my code).

    [VBA]Dim counter1 as integer
    Dim counter2 as integer

    Counter1 = 10
    Counter2 = 15

    Worksheets("sheet1").rows("counter1, counter2").copy
    Worksheets("sheet2").range("a1").paste[/VBA]

  2. #2
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,876
    either:[VBA]With Worksheets("sheet1")
    Union(.Rows(counter1), .Rows(counter2)).Copy
    End With[/VBA]or:[VBA]Worksheets("sheet1").Range(counter1 & ":" & counter1 & "," & counter2 & ":" & counter2).Copy
    [/VBA]
    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
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    [VBA]Sub ken()
    Dim counter1 As Integer
    Dim counter2 As Integer

    counter1 = 10
    counter2 = 15

    'Worksheets("Sheet1").Rows(counter1 & ":" & counter1).Copy
    Worksheets("Sheet1").Rows(counter1).Copy
    Worksheets("Sheet2").Range("A1").PasteSpecial

    Application.CutCopyMode = False
    End Sub
    [/VBA]

  4. #4
    Quote Originally Posted by p45cal
    either:[VBA]With Worksheets("sheet1")
    Union(.Rows(counter1), .Rows(counter2)).Copy
    End With[/VBA]or:[VBA]Worksheets("sheet1").Range(counter1 & ":" & counter1 & "," & counter2 & ":" & counter2).Copy
    [/VBA]

    First and foremost I just want to say thank you this is the closest I've been to solving this code. Your code works the only thing is I want it to also copy and paste the rows in between counter 1 and 2 not just those rows. Any idea how to do this? Thanks again

  5. #5
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    [VBA]Sub ken()
    Dim counter1 As Integer
    Dim counter2 As Integer

    counter1 = 10
    counter2 = 15

    Worksheets("Sheet1").Range(counter1 & ":" & counter2).Copy
    Worksheets("Sheet2").Range("A1").PasteSpecial

    Application.CutCopyMode = False
    End Sub[/VBA]

  6. #6
    Quote Originally Posted by Kenneth Hobs
    [VBA]Sub ken()
    Dim counter1 As Integer
    Dim counter2 As Integer

    counter1 = 10
    counter2 = 15

    Worksheets("Sheet1").Range(counter1 & ":" & counter2).Copy
    Worksheets("Sheet2").Range("A1").PasteSpecial

    Application.CutCopyMode = False
    End Sub[/VBA]
    Thank you, that worked perfectly I was missing the pastespecial

Posting Permissions

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