VBA – Build a Custom Import Interface

Associated Files Download Links

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)
ActiveWorkbook.Close SaveChanges:=False

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.

You may also like some of this related content...