Copy text file to Excel - excel

I'm using this code, and its working.
But my text file is to long, so I can't see all the text.
Its like the height of the row reached the limit.
What can I do?
Maybe copy one row from the text file to one row in the Excel worksheet.
Sub CopyTextFile()
Dim oFso : Set oFso = CreateObject("Scripting.FileSystemObject")
Dim oFile : Set oFile = oFso.OpenTextFile(""L:\00010\COMPANY.bat"", 1)
Dim sText
sText = oFile.ReadAll
oFile.Close
ThisWorkbook.Sheets("Text file").Range("A1").Value = sText
End Sub

Sub Macro1()
Dim comp, path1 As String
comp = "COMPANY"
path1 = "TEXT;L:\00010\COMPANY.bat"
With ActiveSheet.QueryTables.Add(Connection:=path1, _
Destination:=Range("C1"))
.Name = comp
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 1252
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = False
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = False
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
End Sub

Related

VBA text file to excel by text file column numbers

I need find a way to import a text file to excel. I would like to import it by specific column numbers within the text file. (ex. COL 1-4 in A:A, COL 6-9 in B:B...etc) see text file clip below. Not very proficient with VBA so bear with me. Thank you in advance
from this:
original text file snip
to this:
desired excel sheet snip
This is what im starting with.
Dim fd As FileDialog
Set fd = Application.FileDialog(msoFileDialogOpen)
Dim vrtSelectedItem As Variant
With fd
If .Show = -1 Then
For Each vrtSelectedItem In .SelectedItems
SelectedFile = vrtSelectedItem
Next vrtSelectedItem
mycancel = 2
Else
mycancel = 1
End If
End With
Set fd = Nothing
sh_(1).Select
outputfile = "TEXT;" & SelectedFile
With ActiveSheet.QueryTables.Add( _
Connection:=outputfile, _
Destination:=Range("A2"))
.Name = "text reader"
.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 = True
.TextFileTabDelimiter = True
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = True
.TextFileSpaceDelimiter = True
.TextFileColumnDataTypes = Array(1, 1, 1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With

Changing Number type to Text type when import csv by vba

I got an import csv code below
Private Sub Workbook_Open()
Dim xFileName As Variant
Dim Rg As Range
Dim xAddress As String
xFileName = Application.GetOpenFilename("CSV File (*.csv), *.csv", , , , False)
If xFileName = False Then Exit Sub
On Error Resume Next
xAddress = Range("A1").Address
With ActiveSheet.QueryTables.Add("TEXT;" & xFileName, Range(xAddress))
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 65001
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = True
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = True
.TextFileSpaceDelimiter = False
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
End Sub
Normally, it worked but when import csv but some kind of column that has start with 0 number, then excel treat that cell as Number and delete ( hide ) all 0 begin cell.
I've tried to add this script in but that not work.
ActiveSheet.NumberFormat = "#"
Have you tried format the destination field
Example
QueryTable.TextFileColumnDataTypes property
.TextFileColumnDataTypes = Array(xlTextFormat)

vbscript retain leading zeroes when converting txt to xlsx

I'm trying to convert a text file (tab delimited) to xlsx. The below script does the conversion successfully but it removes the leading 0s, I tried formatting all cells to text beforehand using
objWorkSheet.Cells.NumberFormat = "#"
but it the result still has no leading zeroes even
though all cells are formatted to text.
Is there anything i can add to the code to make it retain leading 0s? Thank you
Function convert()
Const xlTextQualifierDoubleQuote = 1
Const xlInsertDeleteCells = 1
Const xlDelimited = 1
Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = False
objExcel.DisplayAlerts= False
strCSVfile = "C:\Users\xxxx\Downloads\a.txt"
strXLSfile = "C:\Users\xxxx\Downloads\b.xlsx"
Set objWorkbook = objExcel.Workbooks.Add
Set objWorkSheet = objWorkbook.Worksheets(1)
objWorkSheet.Cells.NumberFormat = "#"
With objWorkSheet.QueryTables.Add("TEXT;" & strCSVfile, objWorkSheet.Range("$A$1"))
.Name = "input"
.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
.TextFileOtherDelimiter = "#"
.TextFileColumnDataTypes = Array(1, 1, 1)
.TextFileTrailingMinusNumbers = True
.Refresh False
End With
'Save Spreadsheet, 51 = Excel 2007-2010
objWorkSheet.SaveAs strXLSfile, 51
'Release Lock on Spreadsheet
objExcel.Quit()
Set ObjExcel = Nothing
End Function

Importing all workbooks from a folder (VBA - Mac)

I want to import all the workbooks from this folder into the active sheet. (Path is in the code).
So far my code is this:
Option Explicit
Sub load_all_from_folder()
Dim idx As Integer
Dim fpath As String
Dim fname As String
idx = 0
fpath = "/Users/charliekeegan/Desktop/Tester1/"
fname = Dir(fpath & "*.csv")
While (Len(fname) > 0)
idx = idx + 1
Sheets("Sheet" & idx).Select
With ActiveSheet.QueryTables.Add(Connection:="TEXT;" _
& fpath & fname, Destination:=Range("A1"))
.Name = "a" & idx
.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 = False
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = False
.TextFileSpaceDelimiter = False
.TextFileOtherDelimiter = "|"
.TextFileColumnDataTypes = Array(1, 1, 1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
fname = Dir
End With
Wend
End Sub
The macro runs but nothing happens in my active sheet - can you see the problem?
Also do you know how i can delete all the sheets after they have been uploaded?

Excel VBA Error import Data from CSV

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

Resources