Copy and pasting ranges with VBA

JenniferJones93

New Member
Joined
Sep 30, 2019
Messages
2
Hello all!

I don't have any sample data to upload at the moment but the question is fairly straightforward I believe. I put a table below to help visualize what I'm looking for.

I have one worksheet with many ranges of the same size and I need a way to move one range to another range with a drop downs and a button.

So on sheet one I have data like this (The headers are merged so User One in this example would be A1:A2, etc.)

User OneUser TwoUser ThreeUser FourUser FiveUser SixUserSevenUserEight

<tbody>
</tbody>


So on my other worksheet I wanted to select which user's data to move and which spot to move it to.

Example:
Move From: User 8
Move To: User 3

And then by running the macro O2:P10 would be cut or copied to E2:F10.

I could set each as a named range or something but I was having trouble figuring out how to call it from the cell's value to do what I want
 

JenniferJones93

New Member
Joined
Sep 30, 2019
Messages
2
Oops-- looks like I need data in the cells for the table to show.

User 1User 2User 3User 4User 5User 6User 7User 8
DataDataDataDataDataDataDataDataDataDataDataDataDataDataDataData
DataDataDataDataDataDataDataDataDataDataDataDataDataDataDataData
DataDataDataDataDataDataDataDataDataDataDataDataDataDataDataData
DataDataDataDataDataDataDataDataDataDataDataDataDataDataDataData
DataDataDataDataDataDataDataDataDataDataDataDataDataDataDataData
DataDataDataDataDataDataDataDataDataDataDataDataDataDataDataData
DataDataDataDataDataDataDataDataDataDataDataDataDataDataDataData
DataDataDataDataDataDataDataDataDataDataDataDataDataDataDataData
DataDataDataDataDataDataDataDataDataDataDataDataDataDataDataData

<tbody>
</tbody>
 

DanteAmor

Well-known Member
Joined
Dec 3, 2018
Messages
8,974
Office Version
2007
Platform
Windows
Try this.

Run the macro, capture the user From, press OK, capture the user To and press Ok.

Code:
Sub Copy_and_pasting_ranges()
  Dim usr1 As Variant, usr2 As Variant, f As Range, uc As Long
  Dim colFrom As Long, colTo As Long
  usr1 = InputBox("Enter User From", "COPY RANGES")
  If usr1 = "" Then Exit Sub
  usr2 = InputBox("Enter User To", "COPY RANGES")
  If usr2 = "" Then Exit Sub
  Set f = Rows(1).Find(usr1, , xlValues, xlWhole)
  If f Is Nothing Then
    MsgBox usr1 & " User does noes not exists", vbCritical
    Exit Sub
  Else
    colFrom = f.Column
  End If
  Set f = Rows(1).Find(usr2, , xlValues, xlWhole)
  If f Is Nothing Then
    MsgBox usr2 & " User does noes not exists", vbCritical
    Exit Sub
  Else
    colTo = f.Column
  End If
  '
  uc = Cells(1, Columns.Count).End(xlToLeft).Column + 3
  Columns(colFrom).Resize(, 2).Copy Columns(uc)
  Columns(colTo).Resize(, 2).Copy Columns(colFrom)
  Columns(uc).Resize(, 2).Copy Columns(colTo)
  Columns(uc).Resize(, 2).Clear
End Sub
 

Forum statistics

Threads
1,082,601
Messages
5,366,571
Members
400,902
Latest member
fathima

Some videos you may like

This Week's Hot Topics

Top