Excel VBA Error import Data from CSV - excel
I'm trying to run the following VBA code. It works on the first 2 files but not the third. I have no idea why as the file is there. I checked the path.
Here is the code and the output from debug.print:
Sub readData(folderName As String, fileName As String, worksheetName As String)
Dim connectionLocation As String
connectionLocation = "TEXT;" & folderName & fileName
Debug.Print (connectionLocation)
With Sheets(worksheetName).QueryTables.Add(Connection:=connectionLocation, _
Destination:=Sheets(worksheetName).Range("A1"))
.Name = worksheetName
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.TextFilePromptOnRefresh = False
.TextFilePlatform = xlMacintosh
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = False
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = True
.TextFileSpaceDelimiter = False
.Refresh BackgroundQuery:=False
.UseListObject = False
End With
End Sub
TEXT;/Users/gryslik/gamaCode/engineAnalysis/model/temp/enginePortfolio.csv
TEXT;/Users/gryslik/gamaCode/engineAnalysis/model/temp/inflatedData.csv
TEXT;/Users/gryslik/gamaCode/engineAnalysis/model/temp/uninflatedData.csv
By Request, here's the last few lines from uninflatedData.csv:
2025-10-01,6.99845608812112,6.99845608812112,2.37765391176136,0.550630416325757,0.550630416325757,0.91703100026816,0.519412428409091,0.519412428409091,1.00986157034952,1.00986157034952,0.91703100026816,0.91703100026816,0,1.74174518596591,1.74174518596591,2.012878005,1.15193822842105,2.15190176814235,2.15190176814235,2.58594116368453,2.15190176814235,7.40086014228261,7.40086014228261,0.2677056328125,2.6711781271875,2.24673667094063,15.8590859130978,15.8590859130978,15.8590859130978,15.8590859130978,15.8590859130978,0.913960283203125,5.18232955708875,4.35965109450623,4.35965109450623
2025-11-01,6.9685481561206,6.9685481561206,2.36694375900568,0.543571052013889,0.543571052013889,0.908986868686862,0.502098680795455,0.502098680795455,1.00100313552189,1.00100313552189,0.908986868686862,0.908986868686862,0,1.72646671942235,1.72646671942235,1.9982919325,1.13913891477193,2.12799174849632,2.12799174849632,2.55720848408804,2.12799174849632,7.36923253483696,7.36923253483696,0.26274812109375,2.64149837021875,2.2217729301524,15.7976165878532,15.7976165878532,15.7976165878532,15.7976165878532,15.7976165878532,0.903805168945312,5.16325063816739,4.34360089375361,4.34360089375361
2025-12-01,6.93864022412009,6.93864022412009,2.35623360625,0.53651168770202,0.53651168770202,0.900942737105564,0.484784933181818,0.484784933181818,0.992144700694267,0.992144700694267,0.900942737105564,0.900942737105564,0,1.71118825287879,1.71118825287879,1.98370586,1.1263396011228,2.1040817288503,2.1040817288503,2.52847580449154,2.1040817288503,7.3376049273913,7.3376049273913,0.257790609375,2.61181861325,2.19680918936417,15.7361472626087,15.7361472626087,15.7361472626087,15.7361472626087,15.7361472626087,0.8936500546875,5.14417171924604,4.327550693001,4.327550693001
2026-01-01,6.90873229211957,6.90873229211957,2.34552345349432,0.529452323390151,0.529452323390151,0.892898605524266,0.467471185568182,0.467471185568182,0.983286265866639,0.983286265866639,0.892898605524266,0.892898605524266,0,1.69590978633523,1.69590978633523,1.9691197875,1.11354028747368,2.08017170920427,2.08017170920427,2.49974312489505,2.08017170920427,7.30597731994565,7.30597731994565,0.25283309765625,2.58213885628125,2.17184544857594,15.6746779373641,15.6746779373641,15.6746779373641,15.6746779373641,15.6746779373641,0.883494940429687,5.12509280032468,4.31150049224838,4.31150049224838
Related
Create new sheets based on a list, and populate those sheets with three .txt files that need an insert/loop functionality within path name
Here is a flowchart of what I am trying to accomplish: I have a list of cities: Tokyo Delhi Shanghai Sao+Paulo Mexico+City Cairo Mumbai Beijing Dhaka Osaka Each of these cities has three .txt files pertaining to each in this format: PopulationFile{}.txt LocationFile{}.txt CityFile{}.txt Here is the complete list of files that are in the same folder: PopulationFileTokyo?.txt PopulationFileBeijing?.txt PopulationFileCairo?.txt PopulationFileDelhi?.txt PopulationFileDhaka?.txt PopulationFileMexico+City?.txt PopulationFileMumbai?.txt PopulationFileOsaka.txt PopulationFileSao+Paulo?.txt PopulationFileShanghai?.txt LocationFileTokyo?.txt LocationFileShanghai?.txt LocationFileSao+Paulo?.txt LocationFileOsaka.txt LocationFileMumbai?.txt LocationFileMexico+City?.txt LocationFileDhaka?.txt LocationFileDelhi?.txt LocationFileCairo?.txt LocationFileBeijing?.txt CityFileTokyo?.txt CityFileShanghai?.txt CityFileSao+Paulo?.txt CityFileOsaka.txt CityFileMumbai?.txt CityFileMexico+City?.txt CityFileDhaka?.txt CityFileDelhi?.txt CityFileCairo?.txt CityFileBeijing?.txt Here's the goal: Create sheets based on a list of values (in this case being Cities) Use a range to iterate through specific file names (an example being PopulationFileTokyo.txt, then LocationFileTokyo.txt, CityFileTokyo.txt) to paste to Column 1, Column 2, and Column 3 on the {Cities} sheet (example Sheetname: Tokyo) Loop insert .txt file data process for each city Using a new Workbook, I want to automate this process. I want to define the city list in VBA, then, instruct VBA to create sheets based on this list of cities, next, iterate through these sheets pasting the three relevant files for each city in the first three columns The script should insert the city name from Cities into the file path/name to correctly insert relevant files to the relevant sheets (as shown after the 'Insert .txt files into columns' box in the flowchart) Only the file name in the file path needs to be edited as all of these files are in the folder Here is a Macro I created showing manual steps of this process (creating three sheets, then going back and inserting the relevant files): Sub Macro3() ' ' Macro3 Macro ' Name sheet, create sheet, create sheet, insert data, insert data, insert data, switch sheet, insert data, insert data, insert data ' ' Sheets("Sheet1").Select Sheets("Sheet1").Name = "Tokyo" Sheets.Add After:=ActiveSheet Sheets("Sheet2").Select Sheets("Sheet2").Name = "Delhi" Sheets.Add After:=ActiveSheet Sheets("Sheet3").Name = "Shanghai" Sheets("Tokyo").Select Application.CutCopyMode = False With ActiveSheet.QueryTables.Add(Connection:= _ "TEXT;/Users/MyName/PycharmProjects/pythonProject7/PopulationFileTokyo" & Chr(10) & ".txt" _ , Destination:=Range("$A$2")) .Name = "PopulationFileTokyo" & Chr(10) & "" .FieldNames = True .RowNumbers = False .FillAdjacentFormulas = False .PreserveFormatting = True .RefreshOnFileOpen = False .RefreshStyle = xlInsertDeleteCells .SavePassword = False .SaveData = True .RefreshPeriod = False .TextFilePromptOnRefresh = False .TextFilePlatform = 10000 .TextFileStartRow = 1 .TextFileParseType = xlDelimited .TextFileTextQualifier = xlTextQualifierDoubleQuote .TextFileConsecutiveDelimiter = False .TextFileTabDelimiter = True .TextFileSemicolonDelimiter = False .TextFileCommaDelimiter = False .TextFileSpaceDelimiter = False .TextFileColumnDataTypes = Array(1) .TextFileTrailingMinusNumbers = True .Refresh BackgroundQuery:=False End With Range("B2").Select Application.CutCopyMode = False With ActiveSheet.QueryTables.Add(Connection:= _ "TEXT;/Users/MyName/PycharmProjects/pythonProject7/LocationFileTokyo" & Chr(10) & ".txt" _ , Destination:=Range("$B$2")) .Name = "LocationFileTokyo" & Chr(10) & "" .FieldNames = True .RowNumbers = False .FillAdjacentFormulas = False .PreserveFormatting = True .RefreshOnFileOpen = False .RefreshStyle = xlInsertDeleteCells .SavePassword = False .SaveData = True .RefreshPeriod = False .TextFilePromptOnRefresh = False .TextFilePlatform = 10000 .TextFileStartRow = 1 .TextFileParseType = xlDelimited .TextFileTextQualifier = xlTextQualifierDoubleQuote .TextFileConsecutiveDelimiter = False .TextFileTabDelimiter = True .TextFileSemicolonDelimiter = False .TextFileCommaDelimiter = False .TextFileSpaceDelimiter = False .TextFileColumnDataTypes = Array(1) .TextFileTrailingMinusNumbers = True .Refresh BackgroundQuery:=False End With Range("C2").Select Application.CutCopyMode = False With ActiveSheet.QueryTables.Add(Connection:= _ "TEXT;/Users/MyName/PycharmProjects/pythonProject7/CityFileTokyo" & Chr(10) & ".txt" _ , Destination:=Range("$C$2")) .Name = "CityFileTokyo" & Chr(10) & "" .FieldNames = True .RowNumbers = False .FillAdjacentFormulas = False .PreserveFormatting = True .RefreshOnFileOpen = False .RefreshStyle = xlInsertDeleteCells .SavePassword = False .SaveData = True .RefreshPeriod = False .TextFilePromptOnRefresh = False .TextFilePlatform = 10000 .TextFileStartRow = 1 .TextFileParseType = xlDelimited .TextFileTextQualifier = xlTextQualifierDoubleQuote .TextFileConsecutiveDelimiter = False .TextFileTabDelimiter = True .TextFileSemicolonDelimiter = False .TextFileCommaDelimiter = False .TextFileSpaceDelimiter = False .TextFileColumnDataTypes = Array(1) .TextFileTrailingMinusNumbers = True .Refresh BackgroundQuery:=False End With Sheets("Delhi").Select Range("A2").Select Application.CutCopyMode = False With ActiveSheet.QueryTables.Add(Connection:= _ "TEXT;/Users/MyName/PycharmProjects/pythonProject7/PopulationFileDelhi" & Chr(10) & ".txt" _ , Destination:=Range("$A$2")) .Name = "PopulationFileDelhi" & Chr(10) & "" .FieldNames = True .RowNumbers = False .FillAdjacentFormulas = False .PreserveFormatting = True .RefreshOnFileOpen = False .RefreshStyle = xlInsertDeleteCells .SavePassword = False .SaveData = True .RefreshPeriod = False .TextFilePromptOnRefresh = False .TextFilePlatform = 10000 .TextFileStartRow = 1 .TextFileParseType = xlDelimited .TextFileTextQualifier = xlTextQualifierDoubleQuote .TextFileConsecutiveDelimiter = False .TextFileTabDelimiter = True .TextFileSemicolonDelimiter = False .TextFileCommaDelimiter = False .TextFileSpaceDelimiter = False .TextFileColumnDataTypes = Array(1) .TextFileTrailingMinusNumbers = True .Refresh BackgroundQuery:=False End With Range("B2").Select Application.CutCopyMode = False With ActiveSheet.QueryTables.Add(Connection:= _ "TEXT;/Users/MyName/PycharmProjects/pythonProject7/LocationFileDelhi" & Chr(10) & ".txt" _ , Destination:=Range("$B$2")) .Name = "LocationFileDelhi" & Chr(10) & "" .FieldNames = True .RowNumbers = False .FillAdjacentFormulas = False .PreserveFormatting = True .RefreshOnFileOpen = False .RefreshStyle = xlInsertDeleteCells .SavePassword = False .SaveData = True .RefreshPeriod = False .TextFilePromptOnRefresh = False .TextFilePlatform = 10000 .TextFileStartRow = 1 .TextFileParseType = xlDelimited .TextFileTextQualifier = xlTextQualifierDoubleQuote .TextFileConsecutiveDelimiter = False .TextFileTabDelimiter = True .TextFileSemicolonDelimiter = False .TextFileCommaDelimiter = False .TextFileSpaceDelimiter = False .TextFileColumnDataTypes = Array(1) .TextFileTrailingMinusNumbers = True .Refresh BackgroundQuery:=False End With Range("C2").Select Application.CutCopyMode = False With ActiveSheet.QueryTables.Add(Connection:= _ "TEXT;/Users/MyName/PycharmProjects/pythonProject7/CityFileDelhi" & Chr(10) & ".txt" _ , Destination:=Range("$C$2")) .Name = "CityFileDelhi" & Chr(10) & "" .FieldNames = True .RowNumbers = False .FillAdjacentFormulas = False .PreserveFormatting = True .RefreshOnFileOpen = False .RefreshStyle = xlInsertDeleteCells .SavePassword = False .SaveData = True .RefreshPeriod = False .TextFilePromptOnRefresh = False .TextFilePlatform = 10000 .TextFileStartRow = 1 .TextFileParseType = xlDelimited .TextFileTextQualifier = xlTextQualifierDoubleQuote .TextFileConsecutiveDelimiter = False .TextFileTabDelimiter = True .TextFileSemicolonDelimiter = False .TextFileCommaDelimiter = False .TextFileSpaceDelimiter = False .TextFileColumnDataTypes = Array(1) .TextFileTrailingMinusNumbers = True .Refresh BackgroundQuery:=False End With Sheets("Shanghai").Select Range("A2").Select Application.CutCopyMode = False With ActiveSheet.QueryTables.Add(Connection:= _ "TEXT;/Users/MyName/PycharmProjects/pythonProject7/PopulationFileShanghai" & Chr(10) & ".txt" _ , Destination:=Range("$A$2")) .Name = "PopulationFileShanghai" & Chr(10) & "" .FieldNames = True .RowNumbers = False .FillAdjacentFormulas = False .PreserveFormatting = True .RefreshOnFileOpen = False .RefreshStyle = xlInsertDeleteCells .SavePassword = False .SaveData = True .RefreshPeriod = False .TextFilePromptOnRefresh = False .TextFilePlatform = 10000 .TextFileStartRow = 1 .TextFileParseType = xlDelimited .TextFileTextQualifier = xlTextQualifierDoubleQuote .TextFileConsecutiveDelimiter = False .TextFileTabDelimiter = True .TextFileSemicolonDelimiter = False .TextFileCommaDelimiter = False .TextFileSpaceDelimiter = False .TextFileColumnDataTypes = Array(1) .TextFileTrailingMinusNumbers = True .Refresh BackgroundQuery:=False End With Range("B2").Select Application.CutCopyMode = False With ActiveSheet.QueryTables.Add(Connection:= _ "TEXT;/Users/MyName/PycharmProjects/pythonProject7/LocationFileShanghai" & Chr(10) & ".txt" _ , Destination:=Range("$B$2")) .Name = "LocationFileShanghai" & Chr(10) & "" .FieldNames = True .RowNumbers = False .FillAdjacentFormulas = False .PreserveFormatting = True .RefreshOnFileOpen = False .RefreshStyle = xlInsertDeleteCells .SavePassword = False .SaveData = True .RefreshPeriod = False .TextFilePromptOnRefresh = False .TextFilePlatform = 10000 .TextFileStartRow = 1 .TextFileParseType = xlDelimited .TextFileTextQualifier = xlTextQualifierDoubleQuote .TextFileConsecutiveDelimiter = False .TextFileTabDelimiter = True .TextFileSemicolonDelimiter = False .TextFileCommaDelimiter = False .TextFileSpaceDelimiter = False .TextFileColumnDataTypes = Array(1) .TextFileTrailingMinusNumbers = True .Refresh BackgroundQuery:=False End With Range("C2").Select Application.CutCopyMode = False With ActiveSheet.QueryTables.Add(Connection:= _ "TEXT;/Users/MyName/PycharmProjects/pythonProject7/CityFileShanghai" & Chr(10) & ".txt" _ , Destination:=Range("$C$2")) .Name = "CityFileShanghai" & Chr(10) & "" .FieldNames = True .RowNumbers = False .FillAdjacentFormulas = False .PreserveFormatting = True .RefreshOnFileOpen = False .RefreshStyle = xlInsertDeleteCells .SavePassword = False .SaveData = True .RefreshPeriod = False .TextFilePromptOnRefresh = False .TextFilePlatform = 10000 .TextFileStartRow = 1 .TextFileParseType = xlDelimited .TextFileTextQualifier = xlTextQualifierDoubleQuote .TextFileConsecutiveDelimiter = False .TextFileTabDelimiter = True .TextFileSemicolonDelimiter = False .TextFileCommaDelimiter = False .TextFileSpaceDelimiter = False .TextFileColumnDataTypes = Array(1) .TextFileTrailingMinusNumbers = True .Refresh BackgroundQuery:=False End With End Sub Finally, the outcome should be 10 sheets named [Cities] with Column 1 containing PopulationFile{Cities}.txt, Column 2 containing LocationFile{Cities}.txt, and Column 3 containing CityFile{Cities}.txt. 30 .txt files should be imported into their proper sheets in the end. Here is Python code that executes creating these files with the content written inside: Cities.txt Tokyo Delhi Shanghai Sao+Paulo Mexico+City Cairo Mumbai Beijing Dhaka Osaka readNames = open('Cities.txt', 'r').readlines() print(readNames) for i in readNames: open('PopulationFile{}.txt'.format(i), 'w').write('Pop1\nPop2\nPop3') open('LocationFile{}.txt'.format(i), 'w').write('Loc1\nLoc2\nLoc3') open('CityFile{}.txt'.format(i), 'w').write('City1\nCity2\nCity3') If any other information is needed or further clarification please ask!
Please, test the next code. But take care to eliminate the strange character from the end of the files name (a question mark in the text you show, and Chr(10) in your code, which is not allowed. A file name placed on two lines is not allowed by Windows...). So, PopulationFileTokyo?.txt should become PopulationFileTokyo.txt and so on. And read carefully the code lines comments before running it: Sub testPopulateCities() Dim wb As Workbook, foldPath As String, fileName As String, arrTxt Dim arrC, cit, arrCol, cl, colNo As Long, sheetN As String, i As Long 'fill the cities array: arrC = Split("Tokyo,Delhi,Shanghai,Sao+Paulo,Mexico+City,Cairo,Mumbai,Beijing,Dhaka,Osaka", ",") arrCol = Split("Population,Location,City", ",") 'array of prefixes with meaning related to the insertion column Set wb = Workbooks.Add(xlWBATWorksheet) 'create a new workbook with a single sheet 'insert the necessary sheets and naming them according to arrC array elements: For Each cit In arrC i = i + 1 If i = 1 Then wb.Sheets(i).Name = cit 'name the first sheet Else wb.Sheets.Add(After:=wb.Sheets(wb.Sheets.Count)).Name = cit 'insert and name the following sheets End If Next cit foldPath = "C:\Users\RealName\PycharmProjects\pythonProject7\" 'The folder path where the text files exist. Take care of its ending backslash (\)!!! fileName = dir(foldPath & "*.txt") 'determine the first file in the folder Do While fileName <> "" For Each cl In arrCol 'iterate between the array elements If InStr(fileName, cl) > 0 Then 'if the element exists in the file name: colNo = Application.match(cl, arrCol, 0) 'column where to insert data sheetN = Split(fileName, ".")(0) sheetN = Right(sheetN, Len(sheetN) - (Len(cl) + 4)) 'sheet name to insert data Exit For End If Next 'place the text file content in an array, split by vbCrLf (Carriage return - linefeed combination): arrTxt = Split(CreateObject("Scripting.FileSystemObject").OpenTextFile(foldPath & fileName, 1).ReadAll, vbCrLf) wb.Sheets(sheetN).cells(2, colNo).Resize(UBound(arrTxt) + 1, 1) = Application.Transpose(arrTxt) ' drop the array content fileName = dir() 'continue the iteration between files Loop MsgBox "Ready...", vbInformation, "Job done" End Sub
Reworked a text file with vba
I have a text file, I imported it into excel thanks to vba now I would like to remove the first column and the first 9 rows. You think it's possible to do that by rewriting the following code With ActiveSheet.QueryTables.Add(Connection:= _ "TEXT;" & fpath & "\" & ffilename, Destination:=Range("$A$1")) .Name = "text" .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 = True .TextFileSemicolonDelimiter = False .TextFileCommaDelimiter = False .TextFileSpaceDelimiter = False .TextFileOtherDelimiter = "|" .TextFileColumnDataTypes = Array(1) .TextFileTrailingMinusNumbers = True .Refresh BackgroundQuery:=False End With
You can add after that statement something to delete the ranges you want With ActiveSheet .Columns(1).EntireColumn.Delete 'delete first column .Rows("1:9").EntireRow.Delete 'delete first 9 rows End With
Importing from textfile with macro
I have an issue where i want to import value from a textfile into my excel-document which it does but when i run macro over and over to refresh said values with new ones it just moves it over and adds more, it needs to delete values and replace them, i didnt write this code myself so i cant understand the issue with it so here i am pleading for help Sub ImportSaldo() Call Shell("C:\import\GetFromFTP.bat", vbNormalFocus) Dim fileName As String, folder As String folder = "c:\import\" fileName = "Saldot.txt" ActiveCell.Offset(0, 0).Range("A1").Select With ActiveSheet.QueryTables _ .Add(Connection:="TEXT;" & folder & fileName, Destination:=ActiveCell) .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 = True .TextFileCommaDelimiter = False .TextFileSpaceDelimiter = False .TextFileColumnDataTypes = Array(1, 1, 1, 1) .TextFileTrailingMinusNumbers = True .Refresh BackgroundQuery:=False End With End Sub
.RefreshOnFileOpen = False change this to True Once the file is opened it should clear the existing values in the ActiveSheet.
VBA: How to use InputBox to prompt user for .txt file path when importing .txt to Excel?
I have been following a thread else where that showed me how to use Query Tables in the VBA editor to import a .txt file from a specific path into a worksheet. The code is as follows: Sub Sample() With ActiveSheet.QueryTables.Add(Connection:= _ "TEXT;C:\Sample.txt", Destination:=Range("$A$1") _ ) .Name = "Sample" .FieldNames = True .RowNumbers = False .FillAdjacentFormulas = False .PreserveFormatting = True .RefreshOnFileOpen = False .RefreshStyle = xlInsertDeleteCells .SavePassword = False .SaveData = True .AdjustColumnWidth = True .RefreshPeriod = 0 .TextFilePromptOnRefresh = False .TextFilePlatform = 437 .TextFileStartRow = 1 .TextFileParseType = xlDelimited .TextFileTextQualifier = xlTextQualifierDoubleQuote .TextFileConsecutiveDelimiter = False .TextFileTabDelimiter = True .TextFileSemicolonDelimiter = False .TextFileCommaDelimiter = True .TextFileSpaceDelimiter = False .TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1) .TextFileTrailingMinusNumbers = True .Refresh BackgroundQuery:=False End With End Sub I have tried to modify the code so that instead of having to hardcode the path each time, instead a user is prompted for the path with an InputBox that stores the path as a string in a variable, then that variable is called upon instead of the path. I keep getting errors regarding the .Refresh BackgroundQuery:=False line. Here is my modified code below. Option Explicit Sub importEXP() Dim txtloc As String txtloc = InputBox("Provide path of .txt file to analyze") With ActiveSheet.QueryTables.Add(Connection:="TEXT;textloc", destination:=Range("$A$1")) .Name = "Sample" .FieldNames = True .RowNumbers = False .FillAdjacentFormulas = False .PreserveFormatting = True .RefreshOnFileOpen = False .RefreshStyle = xlInsertDeleteCells .SavePassword = False .SaveData = True .AdjustColumnWidth = True .RefreshPeriod = 0 .TextFilePromptOnRefresh = False .TextFilePlatform = 437 .TextFileStartRow = 1 .TextFileParseType = xlDelimited .TextFileTextQualifier = xlTextQualifierDoubleQuote .TextFileConsecutiveDelimiter = False .TextFileTabDelimiter = True .TextFileSemicolonDelimiter = False .TextFileCommaDelimiter = True .TextFileSpaceDelimiter = False .TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1) .TextFileTrailingMinusNumbers = True .Refresh BackgroundQuery:=False End With End Sub Any help is appreciated, Thank You
Change Connection:="TEXT;textloc" to Connection:="TEXT;" & textloc
You need to replace the line: With ActiveSheet.QueryTables.Add(Connection:="TEXT;textloc", destination:=Range("$A$1")) With: With ActiveSheet.QueryTables.Add(Connection:="TEXT;" & textloc, destination:=Range("$A$1")) Double quotes in VBA don't expand variables (like you can do in PowerShell and Perl); you have to explicitly concatenate.
You need to change With ActiveSheet.QueryTables.Add(Connection:="TEXT;textloc", destination:=Range("$A$1")) to With ActiveSheet.QueryTables.Add(Connection:="TEXT;" & textloc, destination:=Range("$A$1")) textloc is a variable so it mustn't be placed inside the quotes.
How to keep csv file dateformat in excel
dateformat of csv file is "yyyy-mm-dd" when open in excel it shows dd/mm/yyyy I want to show the original format in excel thanks
Starting with data like: Try running a macro like: Sub GetDatesFromCSV() With ActiveSheet.QueryTables.Add(Connection:= _ "TEXT;C:\Users\JamesRavenswood\Desktop\temp.csv", Destination:=Range("$A$1")) .Name = "temp" .FieldNames = True .RowNumbers = False .FillAdjacentFormulas = False .PreserveFormatting = True .RefreshOnFileOpen = False .RefreshStyle = xlInsertDeleteCells .SavePassword = False .SaveData = True .AdjustColumnWidth = True .RefreshPeriod = 0 .TextFilePromptOnRefresh = False .TextFilePlatform = 437 .TextFileStartRow = 1 .TextFileParseType = xlDelimited .TextFileTextQualifier = xlTextQualifierDoubleQuote .TextFileConsecutiveDelimiter = False .TextFileTabDelimiter = True .TextFileSemicolonDelimiter = False .TextFileCommaDelimiter = False .TextFileSpaceDelimiter = False .TextFileColumnDataTypes = Array(5) .TextFileTrailingMinusNumbers = True .Refresh BackgroundQuery:=False End With Range("A:A").NumberFormat = "yyy-mm-dd" End Sub