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
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
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>
 
Upvote 0
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
 
Upvote 0

Forum statistics

Threads
1,213,560
Messages
6,114,304
Members
448,564
Latest member
ED38

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