Return to VBA Code Examples

VBA – Build a Custom Import Interface

Written by

Editorial Team

Reviewed by

Steve Rynearson

Last updated on February 9, 2023

Would you prefer to have control over users importing files, instead of having them use the Excel features to do it? Do you need more control over your imports for validation or modification on import? There are multiple ways to do this and just as many requirement variations, but here are some of the building blocks to start from.

1. Put the following code in a module then run it

Public Sub CustomImport()

'Define Variables
Dim ImportFile As String
Dim ImportTitle As String
Dim TabName As String
Dim ControlFile As String

'Open common dialog and get filename
ImportFile = Application.GetOpenFilename( _
"Excel Files, *.xls,   All Files, *.*")
ImportTitle = _
Mid(ImportFile, InStrRev(ImportFile, "\") + 1)

'Check cancel wasn't clicked
If ImportFile = "False" Then
Exit Sub
End If

'Import file
TabName = "MyCustomImport"
ControlFile = ActiveWorkbook.Name
Workbooks.Open Filename:=ImportFile
ActiveSheet.Name = TabName
Sheets(TabName).Copy _
Before:=Workbooks (ControlFile).Sheets(1)
Windows(ImportTitle).Activate
ActiveWorkbook.Close SaveChanges:=False
Windows(ControlFile).Activate 

End Sub

Sidenote: This works well for *.xls, *.xlsx, *.xlsm, *.csv, and *.txt files. You can add or call code before the End Sub to modify the imported data before the user can touch it.

VBA Coding Made Easy

Stop searching for VBA code online. Learn more about AutoMacro - A VBA Code Builder that allows beginners to code procedures from scratch with minimal coding knowledge and with many time-saving features for all users! vba save as


Learn More!
vba-free-addin

VBA Code Examples Add-in

Easily access all of the code examples found on our site.

Simply navigate to the menu, click, and the code will be inserted directly into your module. .xlam add-in.

(No installation required!)

Free Download

Return to VBA Code Examples