Skip to content
WJunction - Webmaster Forum

VBA - Passing a string from one sub to another sub in the same macro

Status
Not open for further replies.
I have a really long excel macro... I will shorten it here to a few lines and give you an example to what I actually want.

Code:
Sub Part1()

Dim Folder As String

Folder = InputBox("Reference")

    If Len(Dir("C:\Ashley\" & Folder, vbDirectory)) = 0 Then
        MkDir ("C:\Ashley\" & Folder)
    End If

///// Lots of code /////

Call Part2

End Sub
¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
Sub Part2()

///// Lots of code /////

    ActiveWorkbook.SaveAs Filename:= _
        "C:\Ashley\" & Folder & "file.xls", FileFormat _
        :=xlTextPrinter, CreateBackup:=False

End Sub
¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯

You see, "Folder" is firstly being declared at the start, but then it is needed at the bottom... Of course I could switch it to the bottom, but it's not the order I want to do things.

How can I get Part2 to get the "Folder" string?
Thanks

---EDIT---

Typical, I've only gone and solved it again.

In case anyone wants to know, I just declared the variable like this.

Code:
Call Part2(Folder)

End Sub
¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
Sub Part2(Folder As String)

Doing this, I was able to use it in Part2.
 
Last edited:
Status
Not open for further replies.

About the author

A
Senior Member · Joined
1,242
Messages
304
Reactions
83
Points
Grow your business

Advertise on WJunction

Connect with an established international community of webmasters, hosting providers, developers and online entrepreneurs.

  • Premium & Premium+ Sticky
  • Dedicated section banners
  • Site-wide banner advertising
Explore advertising options
Back
Top Bottom