With some help I got a macro put together to import a text file and hopefully preserve the leading zeros. Unfortunately it preserves some zeros but not all. If anyone could take a look at the code and let me know what might be causing this I would be very grateful. Please pardon the overflow of comments, this is intended to be used by someone else when I finish and I thought it best to document more than less.
Thanks,
Bill
Thanks,
Bill
Code:
Sub Import_Text_to_Estimating_Tool_Spreadsheet()
'Macro to open the text file dumped from the AS400, copy all the data, paste it into the Item Master worksheet in the Estimating Tool Workbook
'Set format as Text for all columns to preserve leading zeros in item numbers and left justify
Selection.NumberFormat = "@" 'select as text to preserve leading zeroes
Columns("A:G").HorizontalAlignment = xlLeft
'Open the text file dumped from the AS400 and preserves leading zeroes in the Item field
Workbooks.OpenText fileName:= _
"H:\Estimator\QPQUPRFIL_GJH_QDFTJOBD*.txt", Origin:=65001 _
, StartRow:=1, DataType:=xlFixedWidth, FieldInfo:=Array(Array(0, 1), Array( _
3, 1), Array(14, 1), Array(20, 1), Array(33, 1), Array(69, 1), Array(78, 1), Array(89, 1), _
Array(101, 1), Array(103, 1), Array(111, 1)), TrailingMinusNumbers:=True
Set wbTxt = ActiveWorkbook 'Assign a logical for the text file
Cells.Select 'Select all the cells
Selection.Copy 'Copy the selection
ThisWorkbook.Activate
Worksheets("Item Master").Activate
Cells.Select 'Select all cells in the Item Master worksheet in Estimating Tool
ActiveSheet.Paste Destination:=Worksheets("Item Master").Range("A1:N62743")
Application.DisplayAlerts = False 'Turn off alerts like "Turn on alerts like Save Changes or Save information in Paste Buffer
Columns.AutoFit 'Autofit Columns to display info properly
ActiveWorkbook.Save 'Save the workbook with the new info pasted in.
Columns("A:G").HorizontalAlignment = xlLeft 'Alight columns left justified
Columns.AutoFit 'Autofit Columns to display info properly
ActiveWorkbook.Save 'Save the workbook with the new info pasted in.
wbTxt.Close False 'Close the text file
Set wbTxt = Nothing 'Unassign the logical from the text file
Application.DisplayAlerts = True 'Turn on alerts like Save Changes or Save information in Paste Buffer
End Sub