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,506
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,506
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,506
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,081,530
Messages
5,359,347
Members
400,524
Latest member
Excelbat

Some videos you may like

This Week's Hot Topics

  • VBA (Userform)
    Hi All, I just would like to know why my code isn't working. Here is my VBA code: [CODE=vba]Private Sub OKButton_Click() Dim i As Integer...
  • List box that changes fill color
    Hello, I have gone through so many pages trying to figure this out. I have a 2020 calendar that depending on the day needs to have a certain...
  • Remove duplicates and retain one. Cross-linked cases
    Hi all I ran out of google keywords to use and still couldn't find a reference how to achieve the results of a single count. It would be great if...
  • VBA Copy and Paste With Duplicates
    Hello All, I'm in need of some input. My VBA skills are sub-par at best. I've assembled this code from basic research and it works but is...
  • Macro
    is it possible for a macro to run if the active cell value is different to the value above it
  • IF DATE and TIME
    I currently use this to check if date has passed but i also need to set a time on it too. Is it possible? [CODE=vba]=IF(B:B>TODAY(),"Not...
Top