VBA Import text to Excel only saving some zeros.

billfinn

Board Regular
Joined
Jun 7, 2018
Messages
114
Office Version
  1. 2016
Platform
  1. Windows
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

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
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.

Forum statistics

Threads
1,223,958
Messages
6,175,635
Members
452,661
Latest member
Nonhle

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