Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 75
Default Dynamically setting Dimensions in a VBA loop

Hey all.

Having some issues with this, and wanted to see if you have any ideas.

Essentially what I am doing is looping through 9 sheets and excecuting some
code to create pivot tables on each sheet.

For my code to work properly I need a few dimensions assigned, 'TbName' and
'RegionSelect'

What I have now is this just reiterated 9 times, with tb name being 9
different names and 9 different sheets being selected

Sheets("Sheet").Select
TbName = "Name of region"
RegionSelect = ActiveSheet.Name
My_Macro


So now I have come up with this code below:

Public TbName As String
Public RegionSelect As String
Dim wSht As Integer

Sheets(1).Select
wSht = ActiveSheet.Index
Do While wSht < 10
If wSht = 1 Then
Sheets(wSht).Select
Else
End If

Sheets(wSht).Select
TbName = ActiveSheet.Name
'Code that does stuff will go here
wSht = wSht + 1

Loop


What I need is a way to assign 'RegionSelect' with the proper region for
each sheet
The only way I can think of doing this is to create 9 If statements like:

If activesheet.name = "sheet 1" Then
Regionselect = "Region1"
End if

Is there a better way to do this?

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 10,593
Default Dynamically setting Dimensions in a VBA loop

Not sure I fully understand but you can create worksheet local names like so

For Each sh In ActiveWorkbook.Worksheets
If sh.Name < "Sheet1" Then
ActiveWorkbook.Names.Add Name:="'" & sh.Name & "'!Region", _
RefersTo:="='" & sh.Name & "'!$A$3:$A$10"
End If
Next sh


change the range to suit

--
HTH

Bob Phillips

(replace somewhere in email address with gmail if mailing direct)

"havocdragon" wrote in message
...
Hey all.

Having some issues with this, and wanted to see if you have any ideas.

Essentially what I am doing is looping through 9 sheets and excecuting

some
code to create pivot tables on each sheet.

For my code to work properly I need a few dimensions assigned, 'TbName'

and
'RegionSelect'

What I have now is this just reiterated 9 times, with tb name being 9
different names and 9 different sheets being selected

Sheets("Sheet").Select
TbName = "Name of region"
RegionSelect = ActiveSheet.Name
My_Macro


So now I have come up with this code below:

Public TbName As String
Public RegionSelect As String
Dim wSht As Integer

Sheets(1).Select
wSht = ActiveSheet.Index
Do While wSht < 10
If wSht = 1 Then
Sheets(wSht).Select
Else
End If

Sheets(wSht).Select
TbName = ActiveSheet.Name
'Code that does stuff will go here
wSht = wSht + 1

Loop


What I need is a way to assign 'RegionSelect' with the proper region for
each sheet
The only way I can think of doing this is to create 9 If statements like:

If activesheet.name = "sheet 1" Then
Regionselect = "Region1"
End if

Is there a better way to do this?



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 75
Default Dynamically setting Dimensions in a VBA loop

You lost me on that one, but let me explain a bit better here.

I have 2 completely different values that need to be determined. One is
based off the name of the sheet, so there is no worries there. So if sheet1
equalled "Jupiter" then my Dim TbName would = Jupiter. However the second
variable is based off that name, so if TbName = Jupiter, then Dim
RegionSelect = "5th Planet".

The problem lies in that I dont know how to set RegionSelect without using
several If statements to figure out what it should be.

"Bob Phillips" wrote:

Not sure I fully understand but you can create worksheet local names like so

For Each sh In ActiveWorkbook.Worksheets
If sh.Name < "Sheet1" Then
ActiveWorkbook.Names.Add Name:="'" & sh.Name & "'!Region", _
RefersTo:="='" & sh.Name & "'!$A$3:$A$10"
End If
Next sh


change the range to suit

--
HTH

Bob Phillips

(replace somewhere in email address with gmail if mailing direct)

"havocdragon" wrote in message
...
Hey all.

Having some issues with this, and wanted to see if you have any ideas.

Essentially what I am doing is looping through 9 sheets and excecuting

some
code to create pivot tables on each sheet.

For my code to work properly I need a few dimensions assigned, 'TbName'

and
'RegionSelect'

What I have now is this just reiterated 9 times, with tb name being 9
different names and 9 different sheets being selected

Sheets("Sheet").Select
TbName = "Name of region"
RegionSelect = ActiveSheet.Name
My_Macro


So now I have come up with this code below:

Public TbName As String
Public RegionSelect As String
Dim wSht As Integer

Sheets(1).Select
wSht = ActiveSheet.Index
Do While wSht < 10
If wSht = 1 Then
Sheets(wSht).Select
Else
End If

Sheets(wSht).Select
TbName = ActiveSheet.Name
'Code that does stuff will go here
wSht = wSht + 1

Loop


What I need is a way to assign 'RegionSelect' with the proper region for
each sheet
The only way I can think of doing this is to create 9 If statements like:

If activesheet.name = "sheet 1" Then
Regionselect = "Region1"
End if

Is there a better way to do this?




  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 10,593
Default Dynamically setting Dimensions in a VBA loop

I think Tom twigged it then using an array.

--
HTH

Bob Phillips

(replace somewhere in email address with gmail if mailing direct)

"havocdragon" wrote in message
...
You lost me on that one, but let me explain a bit better here.

I have 2 completely different values that need to be determined. One is
based off the name of the sheet, so there is no worries there. So if

sheet1
equalled "Jupiter" then my Dim TbName would = Jupiter. However the second
variable is based off that name, so if TbName = Jupiter, then Dim
RegionSelect = "5th Planet".

The problem lies in that I dont know how to set RegionSelect without using
several If statements to figure out what it should be.

"Bob Phillips" wrote:

Not sure I fully understand but you can create worksheet local names

like so

For Each sh In ActiveWorkbook.Worksheets
If sh.Name < "Sheet1" Then
ActiveWorkbook.Names.Add Name:="'" & sh.Name & "'!Region", _
RefersTo:="='" & sh.Name & "'!$A$3:$A$10"
End If
Next sh


change the range to suit

--
HTH

Bob Phillips

(replace somewhere in email address with gmail if mailing direct)

"havocdragon" wrote in message
...
Hey all.

Having some issues with this, and wanted to see if you have any ideas.

Essentially what I am doing is looping through 9 sheets and excecuting

some
code to create pivot tables on each sheet.

For my code to work properly I need a few dimensions assigned,

'TbName'
and
'RegionSelect'

What I have now is this just reiterated 9 times, with tb name being 9
different names and 9 different sheets being selected

Sheets("Sheet").Select
TbName = "Name of region"
RegionSelect = ActiveSheet.Name
My_Macro


So now I have come up with this code below:

Public TbName As String
Public RegionSelect As String
Dim wSht As Integer

Sheets(1).Select
wSht = ActiveSheet.Index
Do While wSht < 10
If wSht = 1 Then
Sheets(wSht).Select
Else
End If

Sheets(wSht).Select
TbName = ActiveSheet.Name
'Code that does stuff will go here
wSht = wSht + 1

Loop


What I need is a way to assign 'RegionSelect' with the proper region

for
each sheet
The only way I can think of doing this is to create 9 If statements

like:

If activesheet.name = "sheet 1" Then
Regionselect = "Region1"
End if

Is there a better way to do this?






  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Dynamically setting Dimensions in a VBA loop

Perhaps using the index value of the sheet.

Dim v as Variant, TbName as String
v = Array("A", "B", "C", "D", "E", "F", "G", "H", "I")

TbName = v(activesheet.Index-1)

--
Regards,
Tom Ogilvy

"havocdragon" wrote in message
...
Hey all.

Having some issues with this, and wanted to see if you have any ideas.

Essentially what I am doing is looping through 9 sheets and excecuting
some
code to create pivot tables on each sheet.

For my code to work properly I need a few dimensions assigned, 'TbName'
and
'RegionSelect'

What I have now is this just reiterated 9 times, with tb name being 9
different names and 9 different sheets being selected

Sheets("Sheet").Select
TbName = "Name of region"
RegionSelect = ActiveSheet.Name
My_Macro


So now I have come up with this code below:

Public TbName As String
Public RegionSelect As String
Dim wSht As Integer

Sheets(1).Select
wSht = ActiveSheet.Index
Do While wSht < 10
If wSht = 1 Then
Sheets(wSht).Select
Else
End If

Sheets(wSht).Select
TbName = ActiveSheet.Name
'Code that does stuff will go here
wSht = wSht + 1

Loop


What I need is a way to assign 'RegionSelect' with the proper region for
each sheet
The only way I can think of doing this is to create 9 If statements like:

If activesheet.name = "sheet 1" Then
Regionselect = "Region1"
End if

Is there a better way to do this?





Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Setting Multiple Variables with a loop jlclyde Excel Discussion (Misc queries) 6 November 11th 09 09:40 PM
Dynamically setting worksheet tab colors John Excel Worksheet Functions 4 January 29th 09 06:55 PM
Setting Range Dynamically DJS Excel Discussion (Misc queries) 5 December 7th 06 09:51 PM
TextToColumns delimiter setting ignored w/in VB loop Amy[_8_] Excel Programming 0 February 15th 05 07:15 PM
Dynamically setting Print Area Kevin Excel Programming 3 September 24th 04 08:11 PM


All times are GMT +1. The time now is 09:46 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"