code to select first sheet

G

Guest

I export several sheets from Access to an Excel workbook and run a macro that
formats each of the sheets (bolds and centers titles, sets column widths,
etc.). When the macro is done, it leaves the focus on the last sheet of the
workbook. I want the focus to be on the first sheet. All of the sheets have
names (not just Sheet1, Sheet2), but I don't want to hard-code the sheet name
because it could change.

Sheets("Sheet1").Select does not work. Is there a generic way to refer to
Sheet1 (that has been renamed)?

Thank you,
Judy
 
D

Dave Peterson

sheets(1).select

Maybe...

Judy said:
I export several sheets from Access to an Excel workbook and run a macro that
formats each of the sheets (bolds and centers titles, sets column widths,
etc.). When the macro is done, it leaves the focus on the last sheet of the
workbook. I want the focus to be on the first sheet. All of the sheets have
names (not just Sheet1, Sheet2), but I don't want to hard-code the sheet name
because it could change.

Sheets("Sheet1").Select does not work. Is there a generic way to refer to
Sheet1 (that has been renamed)?

Thank you,
Judy
 
G

Guest

I don't know what your loop for formatting the sheets looks like. Ahead of
the loop if you are always on the first sheet when you start the macro put in
these lines -
define a variable such as -
Dim SH as string
SH = ActiveSheet.Name

after the loop has finished -
Sheets(SH).select

You could also try Sheet1.Select if that is always the case for the first
sheet. That is independent of the name. You can see that information in the
VBAProject window. ie. Sheet1 (sheetname)
 

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