PC Review


Reply
Thread Tools Rate Thread

Array Values lost between functions

 
 
Tim879
Guest
Posts: n/a
 
      17th Oct 2007
Hi. I am having an issue with an array that I created. I attached the
code below and can't understand why it does not work. I can add values
to the array and verify that they exist, however once the function
ends, the array's values are lost. What am I missing and any
suggestions on how to correct it?

Note: What I am trying to do is create arrays of sheets to print based
on a tab in the file. The Assign_Sheet_Type Function is where I am
trying to initialize the array. This will be called once the worksheet
opens. The Print MPR Function is where I want to print the array of
sheet.

'Module variables
Private Input_Sheets() As Variant ' Contains all input sheets
Private Supporting_Sheets() As Variant ' Contains all plan, RF and
prior year sheets
Private MPR_Sheets() As Variant ' contains all MPR report sheets
Private Board_Sheets() As Variant ' contains all board pack sheets


Sub Assign_Sheet_Type()

'This macro loops through the Worksheet Type tab and adds each sheet
name to 1 of 4 arrays of sheets
'These arrays are then used to show / hide various sheets in the work
book


Dim Input_Sheets_Count, Supporting_Sheets_Count, MPR_Sheets_Count,
Board_Sheets_Count As Integer
Dim MaxNumSheets, Row As Integer

'go to Worksheet Type tab
Sheets("Worksheet Type").Select

' set the max # of sheets in each array
MaxNumSheets = 100

'assign initial sizes to each array
ReDim Input_Sheets(MaxNumSheets)
ReDim Supporting_Sheets(MaxNumSheets)
ReDim MPR_Sheets(MaxNumSheets)
ReDim Board_Sheets(MaxNumSheets)

'initialize the array indexes
Input_Sheets_Count = 0
Supporting_Sheets_Count = 0
MPR_Sheets_Count = 0
Board_Sheets_Count = 0

'Assign the sheet names to the various arrays based on the data
entered on the spreadsheet
For Row = 1 To 100
If Range("B" & Row).Value = "Input" Then
Input_Sheets(Input_Sheets_Count) = Range("A" & Row).Value
Input_Sheets_Count = Input_Sheets_Count + 1
End If

If Range("C" & Row).Value = "Board" Then
Board_Sheets(Board_Sheets_Count) = Range("A" & Row).Value
Board_Sheets_Count = Board_Sheets_Count + 1
End If

If Range("D" & Row).Value = "MPR" Then
MPR_Sheets(MPR_Sheets_Count) = Range("A" & Row).Value
MPR_Sheets_Count = MPR_Sheets_Count + 1
End If

If Range("C" & Row).Value = "Supporting" Then
Supporting_Sheets(Supporting_Sheets_Count) = Range("A" &
Row).Value
Supporting_Sheets_Count = Supporting_Sheets_Count + 1
End If

Next

'redim the arrays to get rid of the blank values
ReDim Preserve Input_Sheets(Input_Sheets_Count - 1)
ReDim Preserve Board_Sheets(Board_Sheets_Count - 1)
ReDim Preserve MPR_Sheets(MPR_Sheets_Count - 1)
ReDim Preserve Supporting_Sheets(Supporting_Sheets_Count - 1)

End Sub

'**************************************************************************************
Sub Print_MPR()
' Print_MPR Macro
' Macro recorded 9/11/2007 by TB
'
'
On Error GoTo Exit_Print_MPR

Application.ScreenUpdating = False

'remember current sheet
strCurrentSheet = ActiveSheet.Name

Sheets(MPR_Sheets)).Select

ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True

Exit_Print_MPR:
Sheets(strCurrentSheet).Select
Application.ScreenUpdating = True
End Sub

 
Reply With Quote
 
 
 
 
=?Utf-8?B?SkxHV2hpeg==?=
Guest
Posts: n/a
 
      17th Oct 2007
Try putting a macro call to your Print routine in the array sub as shown below:

'redim the arrays to get rid of the blank values
ReDim Preserve Input_Sheets(Input_Sheets_Count - 1)
ReDim Preserve Board_Sheets(Board_Sheets_Count - 1)
ReDim Preserve MPR_Sheets(MPR_Sheets_Count - 1)
ReDim Preserve Supporting_Sheets(Supporting_Sheets_Count - 1)
Print_MPR
End Sub

Your array procedure is not written as a function and will not hold the
array values after it has completed.
Even the functions lose their values after they run, but they are applied
withing another procedure.



Once the array
"Tim879" wrote:

> Hi. I am having an issue with an array that I created. I attached the
> code below and can't understand why it does not work. I can add values
> to the array and verify that they exist, however once the function
> ends, the array's values are lost. What am I missing and any
> suggestions on how to correct it?
>
> Note: What I am trying to do is create arrays of sheets to print based
> on a tab in the file. The Assign_Sheet_Type Function is where I am
> trying to initialize the array. This will be called once the worksheet
> opens. The Print MPR Function is where I want to print the array of
> sheet.
>
> 'Module variables
> Private Input_Sheets() As Variant ' Contains all input sheets
> Private Supporting_Sheets() As Variant ' Contains all plan, RF and
> prior year sheets
> Private MPR_Sheets() As Variant ' contains all MPR report sheets
> Private Board_Sheets() As Variant ' contains all board pack sheets
>
>
> Sub Assign_Sheet_Type()
>
> 'This macro loops through the Worksheet Type tab and adds each sheet
> name to 1 of 4 arrays of sheets
> 'These arrays are then used to show / hide various sheets in the work
> book
>
>
> Dim Input_Sheets_Count, Supporting_Sheets_Count, MPR_Sheets_Count,
> Board_Sheets_Count As Integer
> Dim MaxNumSheets, Row As Integer
>
> 'go to Worksheet Type tab
> Sheets("Worksheet Type").Select
>
> ' set the max # of sheets in each array
> MaxNumSheets = 100
>
> 'assign initial sizes to each array
> ReDim Input_Sheets(MaxNumSheets)
> ReDim Supporting_Sheets(MaxNumSheets)
> ReDim MPR_Sheets(MaxNumSheets)
> ReDim Board_Sheets(MaxNumSheets)
>
> 'initialize the array indexes
> Input_Sheets_Count = 0
> Supporting_Sheets_Count = 0
> MPR_Sheets_Count = 0
> Board_Sheets_Count = 0
>
> 'Assign the sheet names to the various arrays based on the data
> entered on the spreadsheet
> For Row = 1 To 100
> If Range("B" & Row).Value = "Input" Then
> Input_Sheets(Input_Sheets_Count) = Range("A" & Row).Value
> Input_Sheets_Count = Input_Sheets_Count + 1
> End If
>
> If Range("C" & Row).Value = "Board" Then
> Board_Sheets(Board_Sheets_Count) = Range("A" & Row).Value
> Board_Sheets_Count = Board_Sheets_Count + 1
> End If
>
> If Range("D" & Row).Value = "MPR" Then
> MPR_Sheets(MPR_Sheets_Count) = Range("A" & Row).Value
> MPR_Sheets_Count = MPR_Sheets_Count + 1
> End If
>
> If Range("C" & Row).Value = "Supporting" Then
> Supporting_Sheets(Supporting_Sheets_Count) = Range("A" &
> Row).Value
> Supporting_Sheets_Count = Supporting_Sheets_Count + 1
> End If
>
> Next
>
> 'redim the arrays to get rid of the blank values
> ReDim Preserve Input_Sheets(Input_Sheets_Count - 1)
> ReDim Preserve Board_Sheets(Board_Sheets_Count - 1)
> ReDim Preserve MPR_Sheets(MPR_Sheets_Count - 1)
> ReDim Preserve Supporting_Sheets(Supporting_Sheets_Count - 1)
>
> End Sub
>
> '**************************************************************************************
> Sub Print_MPR()
> ' Print_MPR Macro
> ' Macro recorded 9/11/2007 by TB
> '
> '
> On Error GoTo Exit_Print_MPR
>
> Application.ScreenUpdating = False
>
> 'remember current sheet
> strCurrentSheet = ActiveSheet.Name
>
> Sheets(MPR_Sheets)).Select
>
> ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True
>
> Exit_Print_MPR:
> Sheets(strCurrentSheet).Select
> Application.ScreenUpdating = True
> End Sub
>
>

 
Reply With Quote
 
Chip Pearson
Guest
Posts: n/a
 
      19th Oct 2007
Just as a matter of style, I would strongly recommend that you include both
the lower and upper bounds when you declare an array. The reason is that
the lower bound of an array is determined by the Option Base statement (if
any) at the top of the module. For example, depending on the Option Base
statement, the following array has either 10 or 11 elements:

Dim Arr(10)

If you have any inclination to build up a library of reusable code, as any
good programmer should, you may encounter problems when moving code between
modules with different Option Base statements. Do yourself a favor and
declare your array as

Dim Arr(0 To 9)
' or
Dim Arr(1 to 10)

It will save you debugging time in the future.

--
Cordially,
Chip Pearson
Microsoft MVP - Excel, 10 Years
Pearson Software Consulting
www.cpearson.com
(email on the web site)


"JLGWhiz" <(E-Mail Removed)> wrote in message
news:0AE910D2-C2F3-4B1C-A67D-(E-Mail Removed)...
> Try putting a macro call to your Print routine in the array sub as shown
> below:
>
> 'redim the arrays to get rid of the blank values
> ReDim Preserve Input_Sheets(Input_Sheets_Count - 1)
> ReDim Preserve Board_Sheets(Board_Sheets_Count - 1)
> ReDim Preserve MPR_Sheets(MPR_Sheets_Count - 1)
> ReDim Preserve Supporting_Sheets(Supporting_Sheets_Count - 1)
> Print_MPR
> End Sub
>
> Your array procedure is not written as a function and will not hold the
> array values after it has completed.
> Even the functions lose their values after they run, but they are applied
> withing another procedure.
>
>
>
> Once the array
> "Tim879" wrote:
>
>> Hi. I am having an issue with an array that I created. I attached the
>> code below and can't understand why it does not work. I can add values
>> to the array and verify that they exist, however once the function
>> ends, the array's values are lost. What am I missing and any
>> suggestions on how to correct it?
>>
>> Note: What I am trying to do is create arrays of sheets to print based
>> on a tab in the file. The Assign_Sheet_Type Function is where I am
>> trying to initialize the array. This will be called once the worksheet
>> opens. The Print MPR Function is where I want to print the array of
>> sheet.
>>
>> 'Module variables
>> Private Input_Sheets() As Variant ' Contains all input sheets
>> Private Supporting_Sheets() As Variant ' Contains all plan, RF and
>> prior year sheets
>> Private MPR_Sheets() As Variant ' contains all MPR report sheets
>> Private Board_Sheets() As Variant ' contains all board pack sheets
>>
>>
>> Sub Assign_Sheet_Type()
>>
>> 'This macro loops through the Worksheet Type tab and adds each sheet
>> name to 1 of 4 arrays of sheets
>> 'These arrays are then used to show / hide various sheets in the work
>> book
>>
>>
>> Dim Input_Sheets_Count, Supporting_Sheets_Count, MPR_Sheets_Count,
>> Board_Sheets_Count As Integer
>> Dim MaxNumSheets, Row As Integer
>>
>> 'go to Worksheet Type tab
>> Sheets("Worksheet Type").Select
>>
>> ' set the max # of sheets in each array
>> MaxNumSheets = 100
>>
>> 'assign initial sizes to each array
>> ReDim Input_Sheets(MaxNumSheets)
>> ReDim Supporting_Sheets(MaxNumSheets)
>> ReDim MPR_Sheets(MaxNumSheets)
>> ReDim Board_Sheets(MaxNumSheets)
>>
>> 'initialize the array indexes
>> Input_Sheets_Count = 0
>> Supporting_Sheets_Count = 0
>> MPR_Sheets_Count = 0
>> Board_Sheets_Count = 0
>>
>> 'Assign the sheet names to the various arrays based on the data
>> entered on the spreadsheet
>> For Row = 1 To 100
>> If Range("B" & Row).Value = "Input" Then
>> Input_Sheets(Input_Sheets_Count) = Range("A" & Row).Value
>> Input_Sheets_Count = Input_Sheets_Count + 1
>> End If
>>
>> If Range("C" & Row).Value = "Board" Then
>> Board_Sheets(Board_Sheets_Count) = Range("A" & Row).Value
>> Board_Sheets_Count = Board_Sheets_Count + 1
>> End If
>>
>> If Range("D" & Row).Value = "MPR" Then
>> MPR_Sheets(MPR_Sheets_Count) = Range("A" & Row).Value
>> MPR_Sheets_Count = MPR_Sheets_Count + 1
>> End If
>>
>> If Range("C" & Row).Value = "Supporting" Then
>> Supporting_Sheets(Supporting_Sheets_Count) = Range("A" &
>> Row).Value
>> Supporting_Sheets_Count = Supporting_Sheets_Count + 1
>> End If
>>
>> Next
>>
>> 'redim the arrays to get rid of the blank values
>> ReDim Preserve Input_Sheets(Input_Sheets_Count - 1)
>> ReDim Preserve Board_Sheets(Board_Sheets_Count - 1)
>> ReDim Preserve MPR_Sheets(MPR_Sheets_Count - 1)
>> ReDim Preserve Supporting_Sheets(Supporting_Sheets_Count - 1)
>>
>> End Sub
>>
>> '**************************************************************************************
>> Sub Print_MPR()
>> ' Print_MPR Macro
>> ' Macro recorded 9/11/2007 by TB
>> '
>> '
>> On Error GoTo Exit_Print_MPR
>>
>> Application.ScreenUpdating = False
>>
>> 'remember current sheet
>> strCurrentSheet = ActiveSheet.Name
>>
>> Sheets(MPR_Sheets)).Select
>>
>> ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True
>>
>> Exit_Print_MPR:
>> Sheets(strCurrentSheet).Select
>> Application.ScreenUpdating = True
>> End Sub
>>
>>


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
User defined functions that are array functions Richard Microsoft Excel Programming 1 22nd Oct 2009 06:56 PM
Array: Counting multiple values within array Trilux_nogo Microsoft Excel Worksheet Functions 4 16th Apr 2007 03:12 AM
search an array for values contained in another array =?Utf-8?B?Q2hlZXItUGhpbC1seQ==?= Microsoft Excel Programming 0 12th Apr 2007 09:44 PM
efficiency: database functions vs. math functions vs. array formula nickname Microsoft Excel Misc 2 14th Jul 2006 04:26 AM
array functions and complex element values =?Utf-8?B?ZnJ1c3RyYXRlZA==?= Microsoft Excel Worksheet Functions 0 13th Mar 2006 11:44 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 04:40 PM.