Application.GetOpenFilename not working

AMAS

Active Member
Joined
Apr 11, 2010
Messages
472
Hi,

I don't know why this is not working. Any ideas?

Code:
Dim TxtFile                  As String

' Open dialog box
  TxtFile = Application.GetOpenFilename("XML Files,*.xml")
    If TxtFile = "False" Then Exit Function

' Import data from Text file
      With ThisWorkbook.ActiveSheet.QueryTables.Add(Connection:="TEXT;TxtFile", _
        Destination:=Cells(Rows.Count, "A").End(xlUp)(2))
        .Refresh BackgroundQuery:=False
    End With
If I hardcode the file address then it works fine, but when I use TxtFile instead of the file path then it gives an error that it can't find the file and highlights:

Code:
.Refresh BackgroundQuery:=False
AMAS
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
You need to concatenate the file name/path into the Connection argument:

Rich (BB code):
With ThisWorkbook.ActiveSheet.QueryTables.Add(Connection:="TEXT;" & TxtFile, _
 
Upvote 0
Hi Peter and Richard,

Peter: Thanks Peter, but sorry to report that this solution doesn't resolve the issue. The file that I am opening is an xml file and I just want to import it as a txt file. If I try to open it first then excel tries to parse everything in a row rather than leave it in one column.

Richard: Thanks Richard. You were right. I needed quotation marks and AND signs around the variable. That resolved the problem.

Thanks again everyone.

AMAS
 
Upvote 0

Forum statistics

Threads
1,224,502
Messages
6,179,126
Members
452,890
Latest member
Nikhil Ramesh

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