Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

selecting Excel worksheet with a variable 1

Status
Not open for further replies.

JaxTim999

MIS
Oct 23, 2008
1
US
Good morning all. Newby coder here so, hopefully, this is a basic question

I have a sub that will open a file and pull monthly data from a specified year. The pages in the file are named "2001", "2002", etc.

The line in my sub is:
Sheets("2008").Select

What I need it to be is something like:
Sheets("20XX").Select
where XX is a variable determined by the user.

How is this accomplished?

Thank you for the assistance.
 
Something like this

Code:
dim uservar as integer
uservar = InputBox("Enter a number between 1 and 3", , "1")
Sheets("Sheet" & uservar).Select



Hope this helps.

Matt
[rockband]
 
myYear = "2008"
Sheets(myYear).Select

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 




JaxTim999,

You may be boxed in by some existing system, but what you have to work with is a REALLY BAD design, "The pages in the file are named "2001", "2002", etc."

Having similar data chopped up into different sheets or workbooks, makes it really difficult to access and analyze data and its not the way a professional would design a system.

As I originally said, you may be stuck with something not of your choosing, and if you have occasion to design a new system for a different situation, give serious thought to your "database" design, and don't shoot yourself in the foot.

Skip,
[sub]
[glasses]Just traded in my old subtlety...
for a NUANCE![tongue][/sub]
 
Code:
Dim myYear As Integer
For myYear = 2001 To 2008 Step 1
   Sheets(CStr(myYear)).Select
   ' Other code goes here
Next myYear
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top