Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default scope of public variables

Using Excel 2003

I have a bunch of public variables declared as

Public Grand_IncHours As Double
Public Grand_BudgetLabor As Double
Public Grand_IncLabor As Double
Public Grand_BudgetMat As Double
and about 25 others

I made them public because they are outputted to a worksheet in a
different procedure in another module. Everything was working fine;
but, something has happened that keeps them from resetting when my
code finishes.

The lines of code at the end of the component that calculates the
Public variables is:

Call Output_to_CSSR(r4, items, sub_clins, prorateflag)
Call Format_CSSR(r4)

Application.Calculation = xlCalculationAutomatic
ActiveSheet.PageSetup.PrintArea = r4.Cells(1, 1).CurrentRegion.Address
Application.ScreenUpdating = True

End Sub

These 5 lines of code all execute fine, and the out worksheet looks
fine, the screen is updated, and the print range is correct the first
time the code is run. If I run the code a second time the Public
variables are all doubled; a third time they are tripled etc. This
continues to happen until I reset the code in the VBA editor, or close
and reopen the file. When I look in the locals window during code
execution the variables appear to be well behaved, and they are no
longer available when the code finishes; but, they pick up where they
left off the next time the code runs. I can set them to zero at the
end of the code and everything seems to work fine. I have never had
to do that before on other projects, and this one was working fine
without doing that until this afternoon.

Can anyone shed any light on this behavior?

Thanks

Ken
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7,247
Default scope of public variables

Module-scope or project-scope variables retain their values even when
all VBA code has completed. If you need to reset them, you should do
that as the first task that is carried out by your code.


Cordially,
Chip Pearson
Microsoft Most Valuable Professional
Excel Product Group
Pearson Software Consulting, LLC
www.cpearson.com
(email on web site)

On Mon, 27 Oct 2008 16:32:16 -0700 (PDT), wrote:

Using Excel 2003

I have a bunch of public variables declared as

Public Grand_IncHours As Double
Public Grand_BudgetLabor As Double
Public Grand_IncLabor As Double
Public Grand_BudgetMat As Double
and about 25 others

I made them public because they are outputted to a worksheet in a
different procedure in another module. Everything was working fine;
but, something has happened that keeps them from resetting when my
code finishes.

The lines of code at the end of the component that calculates the
Public variables is:

Call Output_to_CSSR(r4, items, sub_clins, prorateflag)
Call Format_CSSR(r4)

Application.Calculation = xlCalculationAutomatic
ActiveSheet.PageSetup.PrintArea = r4.Cells(1, 1).CurrentRegion.Address
Application.ScreenUpdating = True

End Sub

These 5 lines of code all execute fine, and the out worksheet looks
fine, the screen is updated, and the print range is correct the first
time the code is run. If I run the code a second time the Public
variables are all doubled; a third time they are tripled etc. This
continues to happen until I reset the code in the VBA editor, or close
and reopen the file. When I look in the locals window during code
execution the variables appear to be well behaved, and they are no
longer available when the code finishes; but, they pick up where they
left off the next time the code runs. I can set them to zero at the
end of the code and everything seems to work fine. I have never had
to do that before on other projects, and this one was working fine
without doing that until this afternoon.

Can anyone shed any light on this behavior?

Thanks

Ken

  #3   Report Post  
Posted to microsoft.public.excel.programming
Ken Ken is offline
external usenet poster
 
Posts: 590
Default scope of public variables

I will do that. Thanks for your time this evening, and thanks for a great
site.
Ken

"Chip Pearson" wrote:

Module-scope or project-scope variables retain their values even when
all VBA code has completed. If you need to reset them, you should do
that as the first task that is carried out by your code.


Cordially,
Chip Pearson
Microsoft Most Valuable Professional
Excel Product Group
Pearson Software Consulting, LLC
www.cpearson.com
(email on web site)

On Mon, 27 Oct 2008 16:32:16 -0700 (PDT), wrote:

Using Excel 2003

I have a bunch of public variables declared as

Public Grand_IncHours As Double
Public Grand_BudgetLabor As Double
Public Grand_IncLabor As Double
Public Grand_BudgetMat As Double
and about 25 others

I made them public because they are outputted to a worksheet in a
different procedure in another module. Everything was working fine;
but, something has happened that keeps them from resetting when my
code finishes.

The lines of code at the end of the component that calculates the
Public variables is:

Call Output_to_CSSR(r4, items, sub_clins, prorateflag)
Call Format_CSSR(r4)

Application.Calculation = xlCalculationAutomatic
ActiveSheet.PageSetup.PrintArea = r4.Cells(1, 1).CurrentRegion.Address
Application.ScreenUpdating = True

End Sub

These 5 lines of code all execute fine, and the out worksheet looks
fine, the screen is updated, and the print range is correct the first
time the code is run. If I run the code a second time the Public
variables are all doubled; a third time they are tripled etc. This
continues to happen until I reset the code in the VBA editor, or close
and reopen the file. When I look in the locals window during code
execution the variables appear to be well behaved, and they are no
longer available when the code finishes; but, they pick up where they
left off the next time the code runs. I can set them to zero at the
end of the code and everything seems to work fine. I have never had
to do that before on other projects, and this one was working fine
without doing that until this afternoon.

Can anyone shed any light on this behavior?

Thanks

Ken


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
Scope, Public Vars, Referenced workbooks Neal Zimm Excel Programming 5 October 10th 06 10:55 PM
scope of public variables Janis Excel Programming 6 September 15th 06 09:05 AM
Public variables and scope Dennis Benjamin Excel Programming 3 December 2nd 05 04:41 AM
Scope of a public variable Jos Vens Excel Programming 0 November 24th 03 10:08 AM
Scope of Public Variable Dkline[_2_] Excel Programming 9 October 22nd 03 04:53 PM


All times are GMT +1. The time now is 02:08 AM.

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

About Us

"It's about Microsoft Excel"