Shift range on a macro

starfoxx

New Member
Joined
Jul 5, 2011
Messages
3
Hello, here is the problem at hand.

I have 2 worksheets and I want to copy and transpose certain cells from Sheet 1 to Sheet 2.

This is a very repetitive process (up to 3000 cells) so I want to create a macro to do it, so far this is what I have come up with

Code:
Sub Macro()
    
        Dim ws1 As Worksheet
        Dim ws2 As Worksheet
        Dim r1 As Range
        Dim r2 As Range
        Set ws1 = ThisWorkbook.Worksheets(1)
        Set ws2 = ThisWorkbook.Worksheets(2)
        Set r1 = ws1.Range("C2,G2,K2")
        Set r2 = ws2.Range("D2")
        
        
        
        For cuenta = 1 To 3
        
            r1.Copy
            r2.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
                :=False, Transpose:=True
            
             r1.Offset(1, 0).Activate
            
            
            
            'r2.Offset(90, 0).Select
        Next cuenta
            
                        

End Sub
As you can see, I am trying to offset range 1 (on the 1st ws) by 1 row for each iteration of the for loop and range 2 by 90 rows for each iteration of the loop, however I do not know how to do this, can you please help me

Thanks a bunch
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
Welcome to the board...

Try changing

r1.Offset(1, 0).Activate
r2.Offset(90, 0).Select
to

Set r1 = r1.Offset(1, 0)
Set r2 = r2.Offset(90, 0)


Hope that helps.
 
Upvote 0
Does this help?
Code:
Sub Macro()
    
    Dim ws1 As Worksheet
    Dim ws2 As Worksheet
    Dim r1 As Range
    Dim r2 As Range
 
    Set ws1 = ThisWorkbook.Worksheets(1)
    Set ws2 = ThisWorkbook.Worksheets(2)
    Set r1 = ws1.Range("C2,G2,K2")
    Set r2 = ws2.Range("D2")
 
    For cuenta = 1 To 3
        r1.Copy
        r2.PasteSpecial Paste:=xlPasteValues
        Set r1 = r1.Offset(1, 0)
        Set r2 = r2.Offset(90, 0)
    Next cuenta
 
End Sub
 
Upvote 0
Glad to help....

Perhaps even this to eliminate 2 un needed lines of code...

Rich (BB code):
Sub Macro()
    
Dim ws1 As Worksheet
Dim ws2 As Worksheet
Dim r1 As Range
Dim r2 As Range
Dim cuenta As Long
Set ws1 = ThisWorkbook.Worksheets(1)
Set ws2 = ThisWorkbook.Worksheets(2)
Set r1 = ws1.Range("C2,G2,K2")
Set r2 = ws2.Range("D2")
For cuenta = 0 To 2
    r1.Offset(cuenta, 0).Copy
    r2.Offset(cuenta * 90, 0).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=True
Next cuenta
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,613
Messages
6,179,894
Members
452,948
Latest member
Dupuhini

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top