简体   繁体   English

使用VBA中的FieldInfo动态格式化Workbook.OpenText上的列

[英]Dynamically format columns on Workbook.OpenText using FieldInfo in VBA

I'm writing a program in VBA to open a text file. 我正在VBA中编写一个程序来open文本文件。 I want to format the cells to text on open to perserve data (issue: long number strings are being converted to numbers and truncate). 我想format单元格文本on open持之以恒数据(问题:长数串被转换成数字,截断)。

Through research I've learned this can be done using the FieldInfo argument for .OpenText . 通过研究,我了解到可以使用.OpenTextFieldInfo参数完成此操作。

All current examples working on the internet are static and require the programmer to hard code in FieldInfo:=Array(Array(1,2),Array(2,2).....Array(n,2) . 当前在互联网上运行的所有示例都是静态的,需要程序员在FieldInfo:=Array(Array(1,2),Array(2,2).....Array(n,2)进行硬编码。

Here is the closest I got to an answer but this doesn't work either. 这是我最接近答案的方法,但这也不起作用。 http://www.excelforum.com/excel-general/522433-workbooks-opentext-method-fieldinfo-parameter.html . http://www.excelforum.com/excel-general/522433-workbooks-opentext-method-fieldinfo-parameter.html It gives me a 1004 error. 它给我一个1004错误。 Here is my version: 这是我的版本:

Dim colInfo(1 To 1000,1 To 2)
For i = 1 To 1000
    colInfo(i , 1) = i
    colInfo(i , 2) = 2
Next i

I can't find any vba documentation on Array . 我在Array上找不到任何vba文档。 If I could figure out how to create an Array variable NOT a Dim x() As y , I could solve this issue. 如果我能弄清楚如何创建一个Array变量而不是Dim x() As y ,那么我可以解决这个问题。 Any help would be greatly appreciated! 任何帮助将不胜感激!

The Array function just returns a variant - it is just a quick way of initialising. Array函数仅返回一个变体-这只是初始化的一种快速方法。 So you could just create one of those instead and pass that in as your argument. 因此,您可以只创建其中之一,然后将其作为参数传递。 For example: 例如:

Sub abc()

Dim v1 As Variant, v2 As Variant

v1 = Array(1, 2, 3)

ReDim v2(3)
v2(0) = 1
v2(1) = 2
v2(2) = 3

MsgBox v1(0) & vbTab & v2(0) & vbCr & v1(1) & vbTab & v2(1) & vbCr & v1(2) & vbTab & v2(2)

End Sub

These two arrays (v1 and v2) are the same. 这两个数组(v1和v2)相同。

I hope this helps. 我希望这有帮助。

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

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