PDA

View Full Version : Using variables to reference a range



Seanconn520
05-30-2013, 01:45 PM
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).

Dim counter1 as integer
Dim counter2 as integer

Counter1 = 10
Counter2 = 15

Worksheets("sheet1").rows("counter1, counter2").copy
Worksheets("sheet2").range("a1").paste

p45cal
05-30-2013, 02:29 PM
either:With Worksheets("sheet1")
Union(.Rows(counter1), .Rows(counter2)).Copy
End Withor:Worksheets("sheet1").Range(counter1 & ":" & counter1 & "," & counter2 & ":" & counter2).Copy

Kenneth Hobs
05-30-2013, 02:29 PM
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

Seanconn520
05-31-2013, 07:01 AM
either:With Worksheets("sheet1")
Union(.Rows(counter1), .Rows(counter2)).Copy
End Withor:Worksheets("sheet1").Range(counter1 & ":" & counter1 & "," & counter2 & ":" & counter2).Copy



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

Kenneth Hobs
05-31-2013, 07:16 AM
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

Seanconn520
05-31-2013, 11:53 AM
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

Thank you, that worked perfectly I was missing the pastespecial