For each loop applying value of one cell to entire content

canyon

New Member
Joined
Jan 5, 2022
Messages
24
Office Version
  1. 2019
Platform
  1. Windows
I've got this set of code:

VBA Code:
Sub loopthru()

    Dim rng As Range
    Dim mycell As Range
    
    Set rng = Range("A2:A78")
    For Each mycell In rng
        Windows("wb1.xlsx").Activate
        mycell.Copy
        Windows("wb2.xlsx").Activate
        Range("G2").Select
        Range("G2").PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:= _
        xlNone, SkipBlanks:=False, Transpose:=False
        Range("G3").Select
        Range("G3").Copy
        Windows("wb1.xlsx").Activate
        Range("D2:D78").PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:= _
        xlNone, SkipBlanks:=False, Transpose:=False

    Next
End Sub

When I run it the code only takes the last value of column A and applies its output (taken from WB2 cell G3) to the entire contents of column D (Range D2:D78). How can I do this differently so that each of the values is unique to the input of column A?
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
How about
VBA Code:
Sub loopthru()

    Dim rng As Range
    Dim mycell As Range
   
    Set rng = Range("A2:A78")
    For Each mycell In rng
        Windows("wb1.xlsx").Activate
        mycell.Copy
        Windows("wb2.xlsx").Activate
        Range("G2").Select
        Range("G2").PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:= _
        xlNone, SkipBlanks:=False, Transpose:=False
        Range("G3").Select
        Range("G3").Copy
        Windows("wb1.xlsx").Activate
        mycell.Offset(, 3).PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:= _
        xlNone, SkipBlanks:=False, Transpose:=False

    Next
End Sub
 
Upvote 0
Solution
How about
VBA Code:
Sub loopthru()

    Dim rng As Range
    Dim mycell As Range
  
    Set rng = Range("A2:A78")
    For Each mycell In rng
        Windows("wb1.xlsx").Activate
        mycell.Copy
        Windows("wb2.xlsx").Activate
        Range("G2").Select
        Range("G2").PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:= _
        xlNone, SkipBlanks:=False, Transpose:=False
        Range("G3").Select
        Range("G3").Copy
        Windows("wb1.xlsx").Activate
        mycell.Offset(, 3).PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:= _
        xlNone, SkipBlanks:=False, Transpose:=False

    Next
End Sub
It works! Thank you!
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,215,833
Messages
6,127,161
Members
449,367
Latest member
w88mp

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