![]() |
![]() |
|
|||||||
| Excel Questions All Excel/VBA questions - formulas, macros, pivot tables, general help, etc. Please post to this forum in English only. |
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Board Regular
Join Date: Feb 2002
Location: Stockton, California
Posts: 281
|
Is it possible to make my macros a BIT shorter by defining "Sheets("sheetname")" as a variable in my macro? I know i can use:
variable=activeworksheet.name then : worksheets(variable).select but i cant do it like this?: Sub test() Dim DataSheet As Worksheet Dim CurrFile CurrFile = ActiveWorkbook.Name DataSheet = Worksheets("DataSheet") BackUpRange = DataSheet.Range("a1:a5") end sub Just wondering, thanks |
|
|
|
|
|
#2 |
|
MrExcel MVP
Join Date: Feb 2002
Location: Monterrey, Mexico
Posts: 1,433
|
Why do you want to do this?
You are trying to set an object variable to a string variable. I don't think you can do that. Wait, is this what you want to do? Code:
Dim DataSheet As Worksheet
Dim BackUpRange As Range
Set DataSheet = Worksheets("Sheet1")
Set BackUpRange = DataSheet.Range("a1:a5")
|
|
|
|
|
|
#3 |
|
Board Regular
Join Date: Feb 2002
Posts: 3,064
|
file names seem to be comfusing in VBA suggest add time to end of whatever file name them covers any reason poss date also,, whats more varialbe kinda off,
__________________
Free Excel based Web Toolbar available here. Jack in the UK J & R Excel Solutions "making Excel work for you" |
|
|
|
|
|
#4 |
|
Board Regular
Join Date: Feb 2002
Location: Stockton, California
Posts: 281
|
Al, thats exactly what i was looking for, thanks much
|
|
|
|
|
|
#5 |
|
MrExcel MVP
Join Date: Feb 2002
Location: Monterrey, Mexico
Posts: 1,433
|
If you go into VBA and look at the project explorer it will show you a list of all the worksheets. For example:
Sheet1 (DataSheet) Sheet2 (Sheet2) When you change the sheet tab name you just change the name inside () You can change the name outside the () by clicking on it and then hitting F4 (to bring up the properties window). Change (Name) to DataSheet (for example) and now you can do the following: DataSheet.Range("a1:a5") instead of Worksheets("DataRange").Range("a1:a5")
__________________
Kind regards, Al Chara |
|
|
|
![]() |
| Bookmarks |
| Thread Tools | |
| Display Modes | |
|
|