Using one variable accross multiple Sub

dcharland

New Member
Joined
Mar 2, 2011
Messages
40
I would like to have some help with regards to how to place properly declare a global variable (i think?) and then use that variable in subsequent Sub.

Here is the code I have so far. I allows user to select a file using a browser window and then the second sub should write the filepath into the cell.value.

HTML:
Sub BrowseFile()
 
  Dim Action As Variant
  Dim FileName As String

    With Application.FileDialog(msoFileDialogFilePicker)
      .InitialFileName = "C:\documents\"
      .Title = "Browse Files"
      Action = .Show
      If Action = -1 Then
         FileName = .SelectedItems(1)
      Else
         FileName = ""
      End If
    End With
    
End Sub

Sub selectOft()


Call BrowseFile

Cells(5, "C").Value = FileName
End Sub
Thanks
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
Declare Filename at the top of the module before any subs like this

Code:
Dim FileName As String

Sub BrowseFile()
 
  Dim Action As Variant
 
Upvote 0

Forum statistics

Threads
1,224,521
Messages
6,179,290
Members
452,902
Latest member
Knuddeluff

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top