prompting the file selection

lucabaz

New Member
Joined
Jun 28, 2024
Messages
1
Office Version
  1. 2019
Platform
  1. Windows
Hello, I'm trying to change the below code that has a fixed path and file name to import a csv file with the option to browse the file. Any help would be appreciated.

Sheets("Input").Select
Range("A1").Select
Application.CutCopyMode = False
With ActiveSheet.QueryTables.Add(Connection:= _
"TEXT;C:\Users\LON\Active Forecast_27062024_06_50.csv" _
, Destination:=Range("$A$1"))

.Name = "Active_Forecast_LON_27062024_06_50_1"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 850
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = False
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = True
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(1, 1, 4, 1, 1, 1, 1, 1, 1, 1, 1, 1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False

Sheets("Sold Rooms").Select
Range("F14").Select
ActiveSheet.PivotTables("PivotTable7").PivotCache.Refresh
End With
Sheets("Instructions").Select
End Sub
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Here's one way:

VBA Code:
Dim TargetFile As String
    '
    ' Open dialogue box to browse for file
    '
    TargetFile = Application.GetOpenFilename("CSV files (*.csv), *.csv")
    '
    ' No file selected
    '
    If TargetFile = "False" Then
        '
        ' Display message box with an error and exit
        '
        MsgBox "No file selected.", vbExclamation, "Error"
        Exit Sub
    End If

Then in your code you would have:
VBA Code:
With ActiveSheet.QueryTables.Add(Connection:= _
"TEXT;" & TargetFile, Destination:=Range("$A$1"))
 
Upvote 0

Forum statistics

Threads
1,224,901
Messages
6,181,640
Members
453,059
Latest member
jkevin

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