VBA - Open file from current directory

Phosphonothioic

Board Regular
Joined
Sep 27, 2009
Messages
194
I've created a macro to import a text file that the user can select. Problem is, the prompt starts on the desktop and they have to navigate to the proper directory/file manually.

Is there a way, when I use the GetOpenFilename application to start the search in the directory that the excel workbook is in?

Here's some of my code:

Code:
Sub Import()
Dim qry             As QueryTable
Dim strFilFulNam    As String
Dim strQryName      As String
Dim LastRow         As Long
Dim ContainerWB     As Workbook
    
    strFilFulNam = Application.GetOpenFilename(FileFilter:="Text Files (*.txt),*.txt", _
                                               Title:="Select Textfile to Import", _
                                               MultiSelect:=False)
    
    If strFilFulNam = "False" Then
            Exit Sub
        'Else
            'Set ContainerWB = Workbooks.Open(strFilFulNam)
    End If
    
    strFilFulNam = "TEXT;" & strFilFulNam
        
    
    With ActiveSheet
    
        On Error GoTo ErrorCatch:
        
        'Append to previous data, if applicable
        If Range("A" & Rows.Count).End(xlUp).Row = 1 Then
            LastRow = Range("A" & Rows.Count).End(xlUp).Row
            Else
            LastRow = Range("A" & Rows.Count).End(xlUp).Row + 1
        End If
        
        'MsgBox "LastRow value is:" & LastRow 'verification test
        
        Set qry = .QueryTables.Add(Connection:=strFilFulNam, _
                                   Destination:=.Range("A" & LastRow))
        With qry
            .Name = "Filename"
            .FieldNames = True
            .RowNumbers = False
            .FillAdjacentFormulas = False
            .PreserveFormatting = True
            .RefreshOnFileOpen = False
            .RefreshStyle = xlInsertDeleteCells
            .SavePassword = False
            .SaveData = True
            .AdjustColumnWidth = True
            .RefreshPeriod = 0
            .TextFilePromptOnRefresh = False
            .TextFilePlatform = xlWindows
            .TextFileStartRow = 1
            .TextFileParseType = xlFixedWidth
            .TextFileTextQualifier = xlTextQualifierDoubleQuote
            .TextFileConsecutiveDelimiter = False
            .TextFileTabDelimiter = True
            .TextFileSemicolonDelimiter = False
            .TextFileCommaDelimiter = False
            .TextFileSpaceDelimiter = False
            .TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1, 1)
            .TextFileFixedColumnWidths = Array(14, 12, 11, 6, 6, 9, 7, 7)
            .Refresh BackgroundQuery:=False
            'DoEvents
            'strQryName = .Name
            '.Delete
            'ThisWorkbook.Names(strQryName).Delete
        End With
    End With
Exit Sub
ErrorCatch:
MsgBox "Unexpected Error. Type:" & Err.Description
End Sub
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
You could add as the first line of code

Code:
ChDir ThisWorkbook.Path
'or
ChDir ActiveWorkbook.Path
 
Upvote 0

Forum statistics

Threads
1,222,622
Messages
6,167,137
Members
452,098
Latest member
xel003

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