Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

How do I Copy and paste using offset and a loop?

0

I need to copy multiple cells and paste it into another sheet using VBA

So I need to copy a cell put it in another cell then go back to the other column go down a row and paste it into that same cell as before. 

I need to do it in a loop because once it is pasted into the other cell it refreshes into a URL and changes an output table.

I know how to get the URL changed and refreshed just can't figure out how to copy with an offset.

Answer
Discuss

Answers

0

Hi Macemmie and welcome to the Forum

If you always want to copy to cell G4 (within the same sheet) then use this (commented) code and the attached file - click button "Loop":

Sub CopyToCell()

' get last row in column B
LastB = Range("B" & Rows.Count).End(xlUp).Row

'Loop through rows in B and copy to G4
For n = 4 To LastB
    Cells(4, "G").Value = Cells(n, "B").Value
Next n
' delete this
MsgBox "G4 changed " & n - 4 & " times"

End Sub
If you're copying the value to or from another sheet then the Cells references will need to be preceded with the sheet identity.

Hope this helps.

Discuss

Discussion

Did that work for you? 
John_Ru (rep: 6142) Dec 13, '21 at 4:48 am
Add to Discussion


Answer the Question

You must create an account to use the forum. Create an Account or Login