Increment default InPutBox value

I have an inputbox that has a default value of today date and I would like to start incrementing it instead having it the same each time.  There is a small issue that when I use my Sub Save_XML it is called from within another Sub that is running a Do While loop.  If the InputBox is going to increment now, I will need to pass a variable from one sub to the other in order to keep a counter correct.  Ideally I would like to increment the default text in the InputBox to "File001", "File002"  ....

So two questions in one here.  One how do I make a variable global so it can be used in a called sub from a sub?  Two how would I change the code below to use that variable to increment the InputBox default value?

Please advise and thanks.

Sub Save_XML()
    Dim strFilePath, strFileName As String
    
    strFilePath = "C:\_SAP\LynxFiles\" 'This needs to be changed to correct directory and must end with \
    'strFilePath = gstDestinationFolder
    
    strFileName = InputBox("Please enter a filename or click OK to accept the default.", "Save File", _
                "TestFile-" & Format(Now(), "yyyy-mm-dd")) '<--- change to new default

    If strFileName = "" Then
        Sheets("SIF Data").Select
        ActiveWindow.SelectedSheets.Visible = False
        Sheets("Order Line Items").Select
        ActiveWindow.SelectedSheets.Visible = False
    
        Exit Sub 'Will happen if Cancel is pressed
    End If
    strFileName = strFileName & ".xml"
    
    Application.DisplayAlerts = False
    ActiveSheet.Copy
    
    ActiveWorkbook.SaveAs Filename:= _
        strFilePath & strFileName, FileFormat:=xlTextPrinter, CreateBackup:=False
    ActiveWorkbook.Close savechanges:=False
    
    Application.DisplayAlerts = True

     Sheets("MasterCopy").Select
    
End Sub

Open in new window

RWayneHAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Rey Obrero (Capricorn1)Commented:
-change the Save_XML
with
  sub Save_XML(strFile as string)
     'use the strFile as the strFileName
strFileName=strFile

in the Sub that is running a Do While loop, create a string variable

dim str as string, j as variant

do while
         str="File"
         j=Format(j+1,"000")
        str=str & j

'your code here
       call Save_XML(str)
loop

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
RWayneHAuthor Commented:
Ok... while debugging..  I have a Sub with a do while, that calls another sub, that has the call to the code above...  probably not the best structure, but when it gets the InputBox, the values are Empty...  Is there a way to make a variable truly global so it can be used across a Sub that calls a sub that calls a sub ......   If not I will have to look at restricting the subs.
0
RWayneHAuthor Commented:
Even when I copied the Save_XML out of its own sub and moved it one layer up, they are still Empty.  It changes worksheets and as soon as the worksheet tab changes the values disappear.  How do I hold the variable value to use in the InputBox?
0
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

Rey Obrero (Capricorn1)Commented:
<How do I hold the variable value to use in the InputBox? >
why is the need for the input box when you already have a value for "strFileName"

just comment or remove the line with Inputbox
0
RWayneHAuthor Commented:
I think we are close, because it is keeping the values now..  File001  002 ....  but when the InputBox shows it is blank...  What did I screw up in the InputBox statement to show?

   strFileName = InputBox("Please enter a filename or click OK to accept the default.", "Save File", _
                str) '<--- change to new default
0
RWayneHAuthor Commented:
To answer your other question, I will use it with an InputBox and without.  We want the user to be able to edit the default name or leave it as is.
0
Rey Obrero (Capricorn1)Commented:
to declare a global variable,
 remove the declaration inside the Sub
declare it at the top of any regular moduleoption explicit

Global str as string
0
RWayneHAuthor Commented:
All is good, except for getting it to show in the InputBox...  It is blank, but str is defined correctly.  How would I edit the following to work with the variable str?

strFileName = InputBox("Please enter a filename or click OK to accept the default.", "Save File", _
                 str) '<--- change to new default
0
Rey Obrero (Capricorn1)Commented:
what did you put in the sub Save_XML(??)

try using
sub Save_XML(str)

strFileName = InputBox("Please enter a filename or click OK to accept the default.", "Save File", _
                  str)
0
RWayneHAuthor Commented:
Figured it out....  I was getting an error in the Call Save_XML(str).  So I took that out.  Putting in a True/False to ask user if they want to use edit file names or use suggested default...  Thanks for the help...  I am still trying to grab the global variable concept... so I can press them from sub to sub...  I like calling to existing Subs that I write for specific things, then do a lot of calling to them, (reduces rewrites and copy/pasting) but if I am going to continue doing that... I want to define globals more than not.
0
Rey Obrero (Capricorn1)Commented:
< I was getting an error in the Call Save_XML(str).  >
what is the error?
0
RWayneHAuthor Commented:
It was a Compile Error:  Wrong number of arguments or invaled assignment.  Is it because your was Sub Call Save_XML(str) and mine had Call Save_XML(str)?   This is what is calling the already defined str value right?
0
Rey Obrero (Capricorn1)Commented:
<Is it because your was Sub Call Save_XML(str)> where did you get this?
0
RWayneHAuthor Commented:
Thank you for the help, I really appreciate it.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.