Losing leading 0's when reading .csv

mystery_man

New Member
Joined
Mar 13, 2017
Messages
1
So im writing Macros code that reads in different workbooks, prompts users to make a decision and saves a results files. The problem is that when Leading 0's are eliminated for numerical numbers with less than 8 digits. At the moment im reading in the files by:
Code:
Set VATWkbk = Workbooks.Open(varPath & pathSybol & "VAT.csv"
VATWkbk.Worksheets(1).USedRange.Copy Destination :=MAINWkbk.Worksheets("VAT").Range ("A1")
VATWkbk.Close SaveChanges:=False

I tried.OpenText but i kept on getting errors in trying to get it to work. (Im very new to VBA). Any help on this would be great thanks
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Welcome to the Board!

The issue is that when opening CSV files directly in Excel, Excel makes it own decisions on the format of fields, and formatting numbers with leading zeroes as numbers (and not text) will cause those leading zeroes to drop.

There are a few ways around this, such as:
- Change the file extension from "csv" to something like "txt", which should invoke the Import Wizard and allow you to specify the format of the field as Text
- Import the file by going to Data -> Get External Date -> From Text. This will also invoke the Import Wizard.

Note that you can get VBA code for either of these methods my turning on the Macro Recorder and recording yourself manually opening/importing the files.
 
Upvote 0

Forum statistics

Threads
1,225,204
Messages
6,183,577
Members
453,170
Latest member
sameer98

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