Macro by Reference

lecxE

Board Regular
Joined
Apr 9, 2002
Messages
121
As an example of my dilemma, I simply want a Macro that when executed, copies the contents from the cell immediately above the active cell and moves one cell to the right. I record my macro in cell b2 and I get:

Range("B1").Select
Selection.Copy
Range("B2").Select
ActiveSheet.Paste
Range("C2").Select

Obviously, each time I execute the macro, I copy the same exact data from B1 to B2.

I know I can edit the macro to use ActiveCell.offset, but can I get it to work by reference while recording with keystrokes?

Thanks for any help you may offer.
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
I'm pretty certain that you cannot do this by recording. Rather only by writing the code.
If you need help with the code, gi's a shout.
 
Upvote 0
This should work.

Sub CopyActiveCellAbove()

'Select Cell 1 row above active cell
'which will now be the active cell
ActiveCell.Offset(-1, 0).Select

'Copy Selected Cell
Selection.Copy

'Value of the cell 1 row below new active cell = new active cell
Selection.Offset(1, 0).Value = Selection

'Select cell 1 row down and 1 column to the right of new active cell
ActiveCell.Offset(1, 1).Select

End Sub

EDIT: Added comments to macro so that you can follow what I did.
This message was edited by cosmos75 on 2002-04-11 10:13
 
Upvote 0

Forum statistics

Threads
1,213,533
Messages
6,114,179
Members
448,554
Latest member
Gleisner2

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