VBA HELP Command button

CodesiriuS

New Member
Joined
Dec 18, 2016
Messages
27
Hello - Im new to VBA and Im trying to make a userform that has a button that pretty much launches the window explorer but then lets the user navigate through the directory and upon selecting a file - also opens it so they can utilize other macros to format it. I have this so far but I don't know the code to open the file

Private Sub CommandButton1_Click()
ThisWorkbook.FollowHyperlink "file:\\c:"
End Sub
 

Some videos you may like

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.

BQardi

Active Member
Joined
Aug 30, 2016
Messages
483
Just add the path and filename:

Private Sub CommandButton1_Click()
ThisWorkbook.FollowHyperlink "file:\\c:\path\file.txt"
End Sub

EDIT: Nevermind, I was a little to quick to post without really reading your question...
 
Last edited:

CodesiriuS

New Member
Joined
Dec 18, 2016
Messages
27
Thanks BQardi!! This code upon button click will allow the users to search through the directory and open a file however, upon opening the selected workbook the selected file freezes up and wont let me close out of it. I'm trying to figure out if I should purse trying to troubleshoot this or just make the form to where the user clicks the button to get the file, and then once the file is selected the path shows up in an adjacent text box or something.Then the user can check what formatting options they want, then hit a processing button and be done with it. I have used other utilities like this but I'm green and still don't know what best practice is or in my case easier to start with. Any help or direction would be appreciated.
 

BQardi

Active Member
Joined
Aug 30, 2016
Messages
483

ADVERTISEMENT

You're welcome

As far as I understand, you are trying to change formatting of other workbooks, from this main workbook?

This is advanced in it self and probably harder when using userforms for this task (userforms lockup Excel until closed and this is probably the issue you are experiencing when trying to open another workbook).
I would recommend NOT using userforms for tasks like this when you are, as you said, "green"...
 

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,219
Office Version
  1. 365
Platform
  1. Windows
If you want the user to chose a file then why not use Application.GetOpenFilename?
 

CodesiriuS

New Member
Joined
Dec 18, 2016
Messages
27

ADVERTISEMENT

Thanks! Maybe I should tackle from a different angle.
 

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,219
Office Version
  1. 365
Platform
  1. Windows
It will get you the filename of the workbook the code has selected which you can then open using Workbooks.Open.
 

mikerickson

MrExcel MVP
Joined
Jan 15, 2007
Messages
23,917
This will allow the user to browse and open the file, but the name has to be extracted after its open

Code:
If Application.Dialogs(xlDialogOpen).Show Then
    MsgBox "The newly opened file is " & ActiveWorkbook.Name
Else
    MsgBox "User canceled"
End If
 

Watch MrExcel Video

Forum statistics

Threads
1,123,144
Messages
5,599,978
Members
414,354
Latest member
Flaxarn

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
Top