Copy/paste VBA code
Results 1 to 8 of 8

Thread: Copy/paste VBA code
Thanks Thanks: 0 Likes Likes: 0

  1. #1
    New Member
    Join Date
    Aug 2018
    Posts
    7
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Copy/paste VBA code

    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.

  2. #2
    Board Regular mumps's Avatar
    Join Date
    Apr 2012
    Location
    Toronto, Canada
    Posts
    8,166
    Post Thanks / Like
    Mentioned
    87 Post(s)
    Tagged
    5 Thread(s)

    Default Re: Copy/paste VBA code

    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?
    Practice makes perfect. I'm very far from perfect so I'm still practising.

  3. #3
    New Member
    Join Date
    Aug 2018
    Posts
    7
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Copy/paste VBA code

    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".

  4. #4
    Board Regular mumps's Avatar
    Join Date
    Apr 2012
    Location
    Toronto, Canada
    Posts
    8,166
    Post Thanks / Like
    Mentioned
    87 Post(s)
    Tagged
    5 Thread(s)

    Default Re: Copy/paste VBA code

    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?
    Practice makes perfect. I'm very far from perfect so I'm still practising.

  5. #5
    New Member
    Join Date
    Aug 2018
    Posts
    7
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Copy/paste VBA code

    Thank you for your help. I think manually would be best, by clicking a button that has the macro assigned to it.

  6. #6
    Board Regular mumps's Avatar
    Join Date
    Apr 2012
    Location
    Toronto, Canada
    Posts
    8,166
    Post Thanks / Like
    Mentioned
    87 Post(s)
    Tagged
    5 Thread(s)

    Default Re: Copy/paste VBA code

    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
    Practice makes perfect. I'm very far from perfect so I'm still practising.

  7. #7
    New Member
    Join Date
    Aug 2018
    Posts
    7
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Copy/paste VBA code

    Worked perfectly! Thank you

  8. #8
    Board Regular mumps's Avatar
    Join Date
    Apr 2012
    Location
    Toronto, Canada
    Posts
    8,166
    Post Thanks / Like
    Mentioned
    87 Post(s)
    Tagged
    5 Thread(s)

    Default Re: Copy/paste VBA code

    You are very welcome.
    Practice makes perfect. I'm very far from perfect so I'm still practising.

Some videos you may like

User Tag List

Tags for this Thread

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •