Selecting column D automatically when you leave the sheet

T

Tonso

I have an xl2003 workbook with several worksheets. Some sheet contain
tables, or instructions, etc. Most of the sheets are identical
templates i create as i need them. On these templates sheets, which
are named 1, 2, 3, etc., I would like to, when i leave the sheet, I
would like for the activecell.column to be column D. That is, when i
leave the sheet, column D of the current row is selected. I have tried
several ways to accomploish this, with no success.

Thanks,

Tosno
 
J

James Ravenswood

You can use an Event macro to Select column D in the worksheet when
the sheet gets deactivated:

Private Sub Worksheet_Deactivate()
application.EnableEvents = False
Dim sh As Worksheet
Set sh = ActiveSheet
Me.Select
Columns("D:D").Select
sh.Select
application.EnableEvents = True
End Sub


Because it is worksheet code, it is very easy to install and automatic
to use:

1. right-click the tab name near the bottom of the Excel window
2. select View Code - this brings up a VBE window
3. paste the stuff in and close the VBE window

If you have any concerns, first try it on a trial worksheet.

If you save the workbook, the macro will be saved with it.


To remove the macro:

1. bring up the VBE windows as above
2. clear the code out
3. close the VBE window

To learn more about macros in general, see:

http://www.mvps.org/dmcritchie/excel/getstarted.htm

To learn more about Event Macros (worksheet code), see:

http://www.mvps.org/dmcritchie/excel/event.htm
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top