I've got a program extracting me data under.XLS format it gives me something like that:
Then, I run a macro:
Dim file As String
Const r_as400 = "C:\test\"
Const r_colos = "C:\test\save\"
Const file_name= "TCODES_PRODUITS.xls"
Sub test()
'
' test Macro
'
'
file= r_as400 & file_name
Workbooks.OpenText Filename:=file, Origin:=xlMSDOS, StartRow:=1, _
DataType:=xlDelimited, TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, _
Tab:=True, Semicolon:=False, Comma:=False, Space:=False, Other:=False, _
FieldInfo:=Array(Array(1, 2), Array(2, 1), Array(3, 1), Array(4, 2), Array(5, 1), _
Array(6, 1), Array(7, 1), Array(8, 1), Array(9, 1), Array(10, 1), Array(11, 1), _
Array(12, 1), Array(13, 1), Array(14, 1), Array(15, 1), Array(16, 1), Array(17, 1), _
Array(18, 1)), TrailingMinusNumbers:=True
After that my file is looking like this:
As you can see, in the first screen there is rows with only 1 digit and others with 13
After the macro, some row are equal to others to
I'd tried something, from the initial file I changed column D format into text (NumberFormat ('@')) but it doesn't change anything.
Someone can explain to me difference and how the VBA instruction know how much "0" must be present before "1"?
Please, try the next code. Of course, your used constants must exist as they are:
Sub testCreateNecessaryLeadingZeros()
Dim wb As Workbook, ws As Worksheet, lastR As Long, rng As Range, arr, i As Long
Set wb = Workbooks.Open(r_as400 & file_name)
Set ws = wb.Sheets(1)
lastR = ws.Range("D" & ws.rows.count).End(xlUp).row 'last row on D:D
Set rng = ws.Range("D2:D" & lastR)
arr = rng.value 'place it in an array
For i = 1 To UBound(arr)
arr(i, 1) = Format(arr(i, 1), "00000000000000")
Next
rng.NumberFormat = "@" 'format the range as text
rng.value = arr 'drop the array content, at once
End Sub
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.