Macro copy paste destination cell

timjohnny

New Member
Joined
Aug 23, 2021
Messages
17
Office Version
  1. 2019
Platform
  1. Windows
Hi all,

with some great help from Fluff I was able to copy paste from one sheet to another using the following macro:

VBA Code:
Sub timjohnny()
   Dim i As Long
   
   With Sheets("Rewards Structure")
      For i = 2 To 800
         .Range("A" & i).Resize(, 3).Copy Sheets("Index").Cells(4, i * 3 - 1)
      Next i
   End With
End Sub

Now I want to basically repeat this, but from a different origin sheet (but to the same destination sheet. But I need to specify the destination cell in the destination sheet as the cell in row 4 and the column two down from that into which the above code pastes. So basically, I want to repeat the above macro with a different origin sheet but make sure that I'm pasting into column c+2, where c=last column the above code pastes into.

Could anyone help me with that?

Thanks!
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
I haven't tested this, but I think it does what you asked for, just change the sheet names accordingly if need be:

VBA Code:
Sub timjohnnyV2()
'
    Dim i           As Long
    Dim Destination As Worksheet
    Dim Source      As Worksheet
'
    Set Destination = Sheets("Index")                                               ' <--- Set this to the sheet name that you want to copy the data to
    Set Source = Sheets("Rewards Structure")                                        ' <--- Set this to the sheet name that you want to copy the data from
'
    NextColumnNumberInRowToWriteTo = Destination.Rows(4).Find("*", , xlFormulas, , xlByColumns, xlPrevious).Column + 2 ' Returns a Column Number
'
    With Source
        For i = 2 To 800
            .Range("A" & i).Resize(, 3).Copy Destination.Cells(4, i * 3 - 1 + NextColumnNumberInRowToWriteTo)
        Next i
    End With
End Sub
 
Upvote 0
Solution
Thanks a lot johnnyL! After loooking at your code and seeing that and where I need to add the column number
VBA Code:
.Range("A" & i).Resize(, 3).Copy Destination.Cells(4, i * 3 - 1 + NextColumnNumberInRowToWriteTo)
I ended up just finding out the relevant column number using =columnn() in the destination sheet. This works in this case because I don't have a dynamic range (because in Fluff's formula i = 2 To 800), but I will keep your code on file for when I have a dynamic range!

Thanks again and all the best!
 
Upvote 0

Forum statistics

Threads
1,214,943
Messages
6,122,380
Members
449,080
Latest member
Armadillos

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