Very easy Excel VBA Question - How do you position the cursor?

RalphTheCow

Senior member
Sep 14, 2000
964
380
136
I'm a very casual user of VBA in Excel, and it always gives me trouble.

Is there a way to have it leave the cursor (ActiveCell?) where it is? The macro always takes over and leaves it somewhere else, which is a real pain.
 

TSDible

Golden Member
Nov 4, 1999
1,697
0
76
Open the VBA editor (Alt + F11)
Go to your modules and find your macro.
Put the following line at the end of the macro

Sheets("Sheet 1").Range("A1").Select

where Sheet 1 is the name of the sheet that you want the cusor to be, and A1 is the cell.

After reading your question again, I see that may not be what you want and that the cell should be the last cell you were in. In that case, something like this should work....

Sub RangeTest()

Dim rng As Range
Set rng = Application.ActiveCell
Range("A1").Select
Selection.Copy
Range("F1").Select
ActiveSheet.Paste
rng.Select

End Sub

The highlighted areas are the lines you would need to add to your macro.
 

RalphTheCow

Senior member
Sep 14, 2000
964
380
136
Thanks TSDible, that looks good.

I think your last example is just what I was looking for, a macro to operate on the active cell only, so I guess it should work if I just stuck whatever operation I wanted to do on the active cell between the highlighted rows in your example. I'll have to try it at work, no Excel at home.