简体   繁体   English

VBA FieldInfo 和.NumberFormat 的区别 = "@"

[英]VBA Difference between FieldInfo and .NumberFormat = "@"

I've got a program extracting me data under.XLS format it gives me something like that:我有一个程序在.XLS 格式下提取我的数据,它给了我类似的东西:

在宏之前提取我的文件

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如您所见,在第一个屏幕中,只有 1 个数字的行和其他 13 个数字的行

After the macro, some row are equal to在宏之后,一些行等于00000000000001 others to其他人0000000000000000000001

I'd tried something, from the initial file I changed column D format into text (NumberFormat ('@')) but it doesn't change anything.我尝试了一些东西,从最初的文件中我将列 D 格式更改为文本(NumberFormat('@')),但它没有改变任何东西。

Someone can explain to me difference and how the VBA instruction know how much "0" must be present before "1"?有人可以向我解释差异以及 VBA 指令如何知道“1”之前必须存在多少“0”?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM