NCSUVBAnewb
Technical User
Hey all, I'm new to this forum and VB in general (only been using for 2 days now) and I need some help executing a text import into Excel.
I've got a series of text files with columns of data that import just fine into Excel using the Import Data Wizard, but I'd like to automate this process with a script that opens a new Excel workbook and imports and formats the data. Ideally I'd like to have it so I just have to change the input filename and execute the script, and eventually maybe automate that too.
Currently the script will run and a window flashes open and closed instantly, and I cannot for the life of me figure out why!? Notable errors to this point were every time I had a ":=", namely in the
Selection.Insert Shift.Shift=xlShiftToRight
line, I would get a "Expected Statement" error at the comma. So, I removed them and since then the script executes but flashes the window open and closed. There are no saved files produced by the NewBook subroutine.
The majority of the code was taken from a macro recorded directly in Excel and cleaned up a bit since then.
Any help is appreciated!
Code:
Set objXL = CreateObject("Excel.Application")
objXL.Visible = True
Sub AddNew()
Set NewBook = Workbooks.Add
With NewBook
.Title = "TEST"
.Subject = "TEST"
.SaveAs Filename="TEST.xlsm"
End With
End Sub
Sub ImportTXTtoXLSM()
' ImportTXTtoXLSM Macro
' Imports .txt input calibration files using Import Wizard into properly formatted .xlsm files.
With objXL.NewBook.QueryTables.Add("D:\3DMGT\08ALLTXTS\0801\16-41_080104.TXT",Range("$A$3"))
.Name = "16-41_080104"
.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 = xlFixedWidth
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = True
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = False
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1)
.TextFileFixedColumnWidths = Array(9, 8, 8, 8, 8, 8, 7)
.TextFileTrailingMinusNumbers = True
End With
With objXL.NewBook
Range("B2").Select
ActiveCell.FormulaR1C1 = "1"
Range("C2").Select
ActiveCell.FormulaR1C1 = "2"
Range("D2").Select
ActiveCell.FormulaR1C1 = "3"
Range("E2").Select
ActiveCell.FormulaR1C1 = "4"
Range("F2").Select
ActiveCell.FormulaR1C1 = "5"
Range("G2").Select
ActiveCell.FormulaR1C1 = "6"
Range("H2").Select
ActiveCell.FormulaR1C1 = "7"
Range("B1:H1").Select
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Selection.Merge
Columns("B:B").Select
Selection.Insert Shift=xlShiftToRight
Range("B2").Select
ActiveCell.FormulaR1C1 = "Timestamp"
Range("B3").Select
ActiveCell.FormulaR1C1 = "0:00:00"
Range("B4").Select
ActiveCell.FormulaR1C1 = "0:00:01"
Range("B3:B4").Select
Selection.AutoFill Destination=Range("B3:B84400")
End With
Columns("A:A").ColumnWidth = 9
Columns("B:B").ColumnWidth = 11
Columns("C:C").ColumnWidth = 7
Columns("C:I").Select
Range("C2").Activate
Selection.ColumnWidth = 10
Cells.Select
Range("C2").Activate
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
End With
Range("B2:I2").Select
Selection.Font.Bold = True
End Sub
I've got a series of text files with columns of data that import just fine into Excel using the Import Data Wizard, but I'd like to automate this process with a script that opens a new Excel workbook and imports and formats the data. Ideally I'd like to have it so I just have to change the input filename and execute the script, and eventually maybe automate that too.
Currently the script will run and a window flashes open and closed instantly, and I cannot for the life of me figure out why!? Notable errors to this point were every time I had a ":=", namely in the
Selection.Insert Shift.Shift=xlShiftToRight
line, I would get a "Expected Statement" error at the comma. So, I removed them and since then the script executes but flashes the window open and closed. There are no saved files produced by the NewBook subroutine.
The majority of the code was taken from a macro recorded directly in Excel and cleaned up a bit since then.
Any help is appreciated!
Code:
Set objXL = CreateObject("Excel.Application")
objXL.Visible = True
Sub AddNew()
Set NewBook = Workbooks.Add
With NewBook
.Title = "TEST"
.Subject = "TEST"
.SaveAs Filename="TEST.xlsm"
End With
End Sub
Sub ImportTXTtoXLSM()
' ImportTXTtoXLSM Macro
' Imports .txt input calibration files using Import Wizard into properly formatted .xlsm files.
With objXL.NewBook.QueryTables.Add("D:\3DMGT\08ALLTXTS\0801\16-41_080104.TXT",Range("$A$3"))
.Name = "16-41_080104"
.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 = xlFixedWidth
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = True
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = False
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1)
.TextFileFixedColumnWidths = Array(9, 8, 8, 8, 8, 8, 7)
.TextFileTrailingMinusNumbers = True
End With
With objXL.NewBook
Range("B2").Select
ActiveCell.FormulaR1C1 = "1"
Range("C2").Select
ActiveCell.FormulaR1C1 = "2"
Range("D2").Select
ActiveCell.FormulaR1C1 = "3"
Range("E2").Select
ActiveCell.FormulaR1C1 = "4"
Range("F2").Select
ActiveCell.FormulaR1C1 = "5"
Range("G2").Select
ActiveCell.FormulaR1C1 = "6"
Range("H2").Select
ActiveCell.FormulaR1C1 = "7"
Range("B1:H1").Select
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Selection.Merge
Columns("B:B").Select
Selection.Insert Shift=xlShiftToRight
Range("B2").Select
ActiveCell.FormulaR1C1 = "Timestamp"
Range("B3").Select
ActiveCell.FormulaR1C1 = "0:00:00"
Range("B4").Select
ActiveCell.FormulaR1C1 = "0:00:01"
Range("B3:B4").Select
Selection.AutoFill Destination=Range("B3:B84400")
End With
Columns("A:A").ColumnWidth = 9
Columns("B:B").ColumnWidth = 11
Columns("C:C").ColumnWidth = 7
Columns("C:I").Select
Range("C2").Activate
Selection.ColumnWidth = 10
Cells.Select
Range("C2").Activate
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
End With
Range("B2:I2").Select
Selection.Font.Bold = True
End Sub