Excel/User Input

Advertisement


Question
Hi,  I am really new to VBA.  
I want to create a program in Excel where a form asks a user for a file name, or lets them browse for the file like file>>open does in any application and once the file has been selected opens it and rips data from a specified cell range.  I can make the user form and I am confident I can create the code to copy the data from the cell range.  However I have no idea how to code the file location/ browse   to file part.  Can you help please?

Answer
Dave,
The GetOpenFilename method will do exactly what you need.  It uses the open file dialog box to let the user browse to a file and select it.  The short snippet of code below gives an example. There are several options that can be used to set filters and other options.  Search for it in the VBA help to find all of the settings that can be used.  If you would like to set a specific folder to have the open dialog start in, use the ChDir method (also shown in the code below). To open the file after the user has selected it, use the Open method.

Best regards,
Chad Welch

Sub GetFile()
   Dim txtFName As String
   Dim wbkOpened As Workbook
   ChDir "C:\Users\"
   txtFName = Application.GetOpenFilename
   Set wbkOpened = Workbooks.Open(txtFName)
End Sub

About Excel
This topic answers questions related to Microsoft Excel spreadsheet (or workbook) stand-alone or Mircrosoft Office Excel including Excel 2003, Excel 2007, Office 2000, and Office XP. You can get Excel help on Excel formulas(or functions), Excell macros, charting in Excel, advanced features, and the general use of Excel. This does not provide a general Excel tutorial nor the basics of using a spreadsheet. It provides specific answers to using Microsoft Excel only. If you do not see your Excel question answered in this area then please ask an Excel question here

Excel

All Answers


Answers by Expert:


Ask Experts

Volunteer


Chad Welch

Expertise

I am a heavy user of Excel and VBA. Unfortunately, I do not have the time to help you design a complex project. However, I am very happy to help troubleshoot problems you are encountering or provide direction with formulas or snippets of code.

Experience

I have created several complex spreadsheets for use in data collection/aggregation as well as scientific laboratory studies and procedures.

Organizations
Brainbench MVP for MS Excel.

Education/Credentials
AS - Computer Science (emphasis on programming) BS - Chemistry

©2012 About.com, a part of The New York Times Company. All rights reserved.