Declare variable by choosing file

sh1ne

New Member
Joined
Jul 3, 2017
Messages
33
Office Version
  1. 2016
Platform
  1. Windows
Hi Guys,

I have a little problem, everytime when I need to run macro the name of files are changed. I want to automatize it maximally as I can and it need to be friendly for newbies.
How can I assign name of chosen files to macros using box like below?
WbbXZxN.png


Need to run twice this box, choose file and assign it to variable. Simply? Not for me

Would be thankful if someone helps me.
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
Rather than creating your own input box you can use the inbuilt filepicker like
Code:
Sub GetFname()
'sh1ne

    Dim Fname1 As String
    Dim Fname2 As String
    Dim Wb1 As Workbook
    Dim Wb2 As Workbook
    
    Fname1 = Application.GetOpenFilename(FileFilter:="Excel Workbooks (*.xls*),*.xls*", _
                Title:="Select Source File")
    Fname2 = Application.GetOpenFilename(FileFilter:="Excel Workbooks (*.xls*),*.xls*", _
                Title:="Select Source File")

    If Fname1 <> "False" Then Set Wb1 = Workbooks.Open(Fname1)
    If Fname2 <> "False" Then Set Wb2 = Workbooks.Open(Fname1)

End Sub
 
Upvote 0
Thanks Fluff,

there arrives problem "subscript out of range" there:
NS2GuVx.png


I've realized these things:

1. Variables are strings, and I always have its opened, one or both files(Im running macro from "destinyfile") so all what I need is just get name of this Workbook. (my proposition)
2. We can do the same to "sourcefile", get filename or just open = no matters
3. Before it, Ive just changing names in first lines of code, but as I said - need to automatize it and being friendly UX. (thats why Ive wanted combox(?))
4. Operations are made in both workbooks in "Data" worksheets. So name of worksheets are constant, but name of workbooks are variables.
 
Upvote 0
If you are using the code snippet I supplied the problem line should be
Code:
Wb1.Worksheets("Data")
 
Upvote 0

Forum statistics

Threads
1,224,821
Messages
6,181,163
Members
453,021
Latest member
Justyna P

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