I'm not sure how he response stuff would fit in--if you want to quit, you can
exit right away.
But if you wanted to pass it that parm, you could do something like:
Dim Response as long
....
response = msgbox(prompt:="whatever",buttons:=vbyesno)
if doSomethingElse(response) = true then
'something completely different
else
'something else completely
end if
....
function dosomethingelse(whattheyanswered as long) as boolean
if whattheyanswered = vbyes then
'do a bunch of things
dosomethingelse = true
else
'do a bunch of other things
dosomethingelse = false
end if
end function
I don't like this example, but you may be able to see how you can pass parms to
subroutines or functions.
A better example that may actually do something useful is a function posted by
Myrna Larson:
http://google.com/[email protected]
This is a snippet:
Private Function GetFileList(Pattern As String, FileNames() As String) As Long
Dim f As String
Dim n As Integer
n = 0
Erase FileNames()
f = Dir$(Pattern)
Do While Len(f)
n = n + 1
ReDim Preserve FileNames(1 To n) As String
FileNames(n) = f
f = Dir$()
Loop
GetFileList = n
End Function 'GetFileList
You pass it the pattern ("c:\my documents\excel\*.xls") and reserve room for
returning filenames and the function returns the number of files that match.
(It also gives the names back in that second passed parameter.)
Dim filelist() as string
dim iCtr as long
'...do lots of stuff
if getfilelist("c:\my documents\excel\*.xls", filelist()) > 0 then
for ictr = lbound(filelist) to ubound(filelist)
'do something to each file
next ictr
end if
===========
What variable you should use depends on what you're working with:
If it's a whole number, you can use Long
if it's a decimal number, Double
If it's a string, String
You may want to invest in a book:
A lot of people like John Walkenbach's:
Excel 2003 Power Programming with VBA
You can find a whole list at Debra Dalgleish's site:
http://www.contextures.com/xlbooks.html
Maybe you could print it and visit your local bookstore to see if you can find
one you like.
rob said:
Thanks Dave.
I'm constantly in the process of learning how to process VBA code better and
your comments are appreciated.
I am always trying to make the code smaller, but in the past, and still
today, I seem to have some trouble with how variables should be defined,
etc. In the course of transferring parts of code to modules or separate
subs, I find sometimes that the value in a variable doesn't work any more.
For instance ...
"If Response = vbNo Then Exit Sub"
with some code below that is common to a number of subs....
I've tried to following code in a separate sub or module but find that the
procedure "loses" the answer to the response.
Another trouble I have is how to declare a variable, as sometimes what I
think should be an integer needs to be a string, etc. What variable should
you use in what circumstances?
Rob