VBA macro to copy values from column B,C,M in reference sheet to column A,B,C in another sheet

alexcon

New Member
Joined
Dec 29, 2017
Messages
20
Hi, I'd like to create a button macro which copies the values from column B,C,M from the reference sheet into column A,B,C of another sheet called "Pivot Staging".

If possible would be useful if the "Pivot Staging" sheet was cleared every time the button is clicked so fresh data can fill it from the reference sheet.
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
You said:
I'd like to create a button macro which copies the values from column B,C,M from the reference sheet
Is the sheet named "reference"
 
Upvote 0
Try this:
Code:
Sub Selecting()
Sheets("reference").Activate
Sheets("Pivot Staging").Cells.Clear
Sheets("reference").Range("B1,C1,M1").EntireColumn.Copy Sheets("Pivot Staging").Range("A1")
End Sub
 
Upvote 0
Try this:
Code:
Sub Selecting()
Sheets("reference").Activate
Sheets("Pivot Staging").Cells.Clear
Sheets("reference").Range("B1,C1,M1").EntireColumn.Copy Sheets("Pivot Staging").Range("A1")
End Sub
Since you have specified the sheet references directly, there is no need to activate the "reference" worksheet. Also, again, since you are copying entire columns, there is no real need to clear the "Pivot Staging" cells as the data in Columns A:C will be overwritten in their entirety. Given that, your macro can be reduced to this...
Code:
[table="width: 500"]
[tr]
	[td]Sub Selecting()
  Sheets("reference").Range("B1,C1,M1").EntireColumn.Copy Sheets("Pivot Staging").Range("A1")
End Sub[/td]
[/tr]
[/table]
That macro, in turn, can be "simplified" to this...
Code:
Sub Selecting()
  [Reference!B:C,Reference!M:M].Copy ['Pivot Staging'!A1]
End Sub
 
Last edited:
Upvote 0
Since you have specified the sheet references directly, there is no need to activate the "reference" worksheet. Also, again, since you are copying entire columns, there is no real need to clear the "Pivot Staging" cells as the data in Columns A:C will be overwritten in their entirety. Given that, your macro can be reduced to this...
Code:
[TABLE="width: 500"]
<tbody>[TR]
[TD]Sub Selecting()
  Sheets("reference").Range("B1,C1,M1").EntireColumn.Copy Sheets("Pivot Staging").Range("A1")
End Sub[/TD]
[/TR]
</tbody>[/TABLE]
That macro, in turn, can be "simplified" to this...
Code:
Sub Selecting()
  [Reference!B:C,Reference!M:M].Copy ['Pivot Staging'!A1]
End Sub

Hi there, sorry this has taken a while. The stuff we're working with has changed slightly.

Column M from the reference sheet is now hidden, so when it copies to the new sheet, it stays hidden.

Are you able to make it copy and unhide column M when it copies to column C in the new sheet?
 
Upvote 0

Forum statistics

Threads
1,224,827
Messages
6,181,200
Members
453,022
Latest member
RobertV1609

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