VBA: Looping to copy and paste information between two sheets

canyon

New Member
Joined
Jan 5, 2022
Messages
24
Office Version
  1. 2019
Platform
  1. Windows
Hello everyone,

I have two excel Workbooks (WB1 & WB2), WB1 one has an input tab (G2) where I paste the values from WB2 (Range: B2:B78) to get an output (G3) which I want to have pasted back into WB2 ( in Column D2:D78). I have about 78 rows to go through and thought this could be a good application to try out looping however, I can't seem to get it to loop through all the rows.

VBA Code:
Sub loopthru()

Dim rng As Range, cell As Range

Set rng = Range("B2:B78")



For Each cell In rng

Range("B2").Select
Selection.Copy
Windows("WB1.xlsx").Activate
Range("G2").PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:= _
        xlNone, SkipBlanks:=False, Transpose:=False
Range("G3").Select
Windows("wb2.xlsx").Activate
Range("D2").PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:= _
        xlNone, SkipBlanks:=False, Transpose:=False

Next cell


End Sub
 
Last edited:

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
VBA Code:
Range("B2").Select
Selection.Copy

Can be
VBA Code:
Range("B2:B78").Selection.Copy

No need for
VBA Code:
For Each cell In rng
 
Upvote 0
VBA Code:
Range("B2").Select
Selection.Copy

Can be
VBA Code:
Range("B2:B78").Selection.Copy

No need for
VBA Code:
For Each cell In rng

Hi there,

unfortunately that doesn't work as it just copies and pastes the range from B2:B78.

I need the value in B2 from Workbook 2 to be copied and pasted into cell G2 in Workbook 1.

There is a formula in G3 which provides an output in cell G3 and paste it into D2 in WB2.

Rinse and repeat until B78 has an accompanying value in D78.
 
Upvote 0
Try
VBA Code:
Sub loopthru()

    Dim rng As Range, cell As Range
    Set rng = Range("B2:B78")
    For Each Mycell In rng
        Mycell.Copy
        Windows("WB1.xlsx").Activate
        Range("G2").PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:= _
                                 xlNone, SkipBlanks:=False, Transpose:=False
        Range("G3").Select
        Windows("wb2.xlsx").Activate
        Range("D2").PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:= _
                                 xlNone, SkipBlanks:=False, Transpose:=False

    Next
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,214,975
Messages
6,122,538
Members
449,088
Latest member
RandomExceller01

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