Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 02-28-2020, 10:17 PM
Jennifer Murphy's Avatar
Jennifer Murphy Jennifer Murphy is offline Best way to make variables in one sub available to a called sub Windows XP Best way to make variables in one sub available to a called sub Office 2007
Competent Performer
Best way to make variables in one sub available to a called sub
 
Join Date: Aug 2011
Location: Silicon Valley
Posts: 234
Jennifer Murphy is on a distinguished road
Default Best way to make variables in one sub available to a called sub

I am working on a complicated Sub. The main For loop is now over two screens long. It's difficult tell how the nested If statements and their Else and End If statements line up. And some of the code is replicated.



So I'd like to move chunks of the code to a sub-Sub. One of these Subs will have to deal with 7-8 variables and modify some of them. I know I can pass these variables to the Sub using the ByRef option, which will allow the Sub to modify them. This is probably the recommended way, but it leads to a fairly long Call statement and I have to make sure that I get them in the right order in both the Call and the Sub statement.

I'd like to play with making them some kind of "public" or "global" variables. I tried changing Dim to Public, but this got an error. I then moved the Dim statements outside the main Sub. That worked, but then all of my variable declarations are not together.

Is there a better way?

Is there a good web page with a clear explanation of variable declaration? I've read several pages from Microsoft, but found them all muddled or incomplete.

Thanks
Reply With Quote
  #2  
Old 02-28-2020, 11:44 PM
macropod's Avatar
macropod macropod is offline Best way to make variables in one sub available to a called sub Windows 7 64bit Best way to make variables in one sub available to a called sub Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,370
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Any variables you declare at the top of your code module (i.e. before any subs) will be available to all subs in that code module. See, for example: https://www.msofficeforums.com/148491-post2.html
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #3  
Old 02-29-2020, 12:18 AM
Jennifer Murphy's Avatar
Jennifer Murphy Jennifer Murphy is offline Best way to make variables in one sub available to a called sub Windows XP Best way to make variables in one sub available to a called sub Office 2007
Competent Performer
Best way to make variables in one sub available to a called sub
 
Join Date: Aug 2011
Location: Silicon Valley
Posts: 234
Jennifer Murphy is on a distinguished road
Default

Quote:
Originally Posted by macropod View Post
Any variables you declare at the top of your code module (i.e. before any subs) will be available to all subs in that code module. See, for example: https://www.msofficeforums.com/148491-post2.html
That's what I surmised.

After playing with that a bit, I decided to pass the variables I want the Sub to change with ByRef and the rest with ByVal. It's working now and I think it's more readable code. All of my declarations are together in the main Sub and it's clear what the sub Sub is working with.

Thanks.
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Array to iterate through variables and trap blank variables Marrick13 Word VBA 5 08-04-2015 06:19 AM
can word: make variables, find appropriate pages, fill out pages with variables, print only those 20GT Word VBA 1 10-15-2014 09:48 PM
Best way to make variables in one sub available to a called sub Can a function or sub be called using a variable name? omahadivision Excel Programming 18 12-05-2013 08:14 PM
excel where is procedure being called from davids67 Excel Programming 1 11-18-2011 06:48 AM
Best way to make variables in one sub available to a called sub What is this symbol called as? Franky Word 3 11-12-2011 08:16 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 06:08 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft