Copy/paste VBA code

jrosen12

New Member
Joined
Aug 10, 2018
Messages
7
Hello,

I am a beginner (new learner) of VBA. I am trying to create a macro that does the following:
The user will be on sheet "Values" He/she will type in three values in columns A, B, and C. I would like the macro to copy these values, and paste them into Sheet "GL" in cells E6,F6, G6. Once they have done that, I want it to copy G9 on Sheet "GL" and paste it in column E on "Values" next to its corresponding row.

So, for example:

"Values" A1:C1 has values 1, 2, 3
The Macro Copies 1, 2, 3 and pastes in Sheet "GL" in E6:G6
Then it copies G9 on the same tab
The Macro goes back to "Values" and pastes this value into E1.
Then it loops until it reaches a blank A cell in "Values".
Please let me know if I need to clarify what I need.
 

mumps

Well-known Member
Joined
Apr 11, 2012
Messages
8,425
Using the method you described, the end result will display the data from the last row of "Values" in E6:G6 of "GL" and all the cells in column E of "Values" will have the value of G9 from "GL". Is this what you want?
 

jrosen12

New Member
Joined
Aug 10, 2018
Messages
7
There may be a better way of getting at what I want, but my end goal is that the values in column E on "Values" will have a corresponding value for its row.

G9 on "GL" is a vlookup formula based on values in E6:G6.

So rows 2-on may have a different value depending on what's in A:C in "Values".
 

mumps

Well-known Member
Joined
Apr 11, 2012
Messages
8,425
Do you want to do this automatically as you enter the data in columns A:C in each row? In other words, you enter data in A1, then in B1 and finally in C1. After you enter the data in C1 and exit the cell, all the copying and pasting will be done automatically for row 1. Then you would enter the data in A2:C2, and so on. The other alternative would be to run a macro manually on all the rows at once after you have entered all the data in all the rows. Which method would work best for you?
 

jrosen12

New Member
Joined
Aug 10, 2018
Messages
7
Thank you for your help. I think manually would be best, by clicking a button that has the macro assigned to it.
 

mumps

Well-known Member
Joined
Apr 11, 2012
Messages
8,425
Try:
Code:
Sub CopyRange()
    Application.ScreenUpdating = False
    Dim rng As Range
    Dim LastRow As Long
    LastRow = Sheets("Values").Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    For Each rng In Sheets("Values").Range("A1:A" & LastRow)
        rng.Resize(, 3).Copy Sheets("GL").Range("E6")
        Sheets("Values").Cells(rng.Row, "E").Value = Sheets("GL").Range("G9").Value
    Next rng
    Application.ScreenUpdating = True
End Sub
 

Forum statistics

Threads
1,077,649
Messages
5,335,482
Members
399,018
Latest member
KathyMoUHC

Some videos you may like

This Week's Hot Topics

Top