Excel VBA 代码转换

发布于 2024-12-08 17:11:18 字数 952 浏览 0 评论 0 原文

以下代码中的 FieldInfo 给我带来了一些问题。我尝试过在线转换器,但它们似乎对这部分代码处理得不太好。我也尝试通过MSDN查找有关它的信息,但我找不到任何信息。我在哪里可以找到有关如何转换该部分的信息,即 Array(x,y) 在 C# 中转换为什么?

任何帮助表示赞赏。

下面是我正在努力解决的代码示例。

    Workbooks.OpenText Filename:= _
    "" & myZdrive & "\CI_System\Source_Files\" & myPosFile & "", Origin:= _
    xlWindows, 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, 2), Array(3, 2), Array(4, 2), Array(5, 2), Array(6, 2), Array(7, 2), Array(8, 1), _
    Array(9, 1), Array(10, 1), Array(11, 1), Array(12, 1), Array(13, 1), Array(14, 2), Array(15 _
    , 1), Array(16, 1), Array(17, 2), Array(18, 2), Array(19, 2), Array(20, 2), Array(21, 2))

    Rows("1:1").Select
    Selection.Font.Bold = True
    Cells.Select
    Cells.EntireColumn.AutoFit

The FieldInfo in the following code is giving me some problems. I've tried online converters, but they don't seem to do very well with this portion of the code. I've also tried to look through MSDN for information on it, but I couldn't find any. Where would I be able to find information on how to convert that part, i.e. what the Array(x,y) translates to in C#?

Any help is appreciated.

Below is an example of the code that I'm struggling with.

    Workbooks.OpenText Filename:= _
    "" & myZdrive & "\CI_System\Source_Files\" & myPosFile & "", Origin:= _
    xlWindows, 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, 2), Array(3, 2), Array(4, 2), Array(5, 2), Array(6, 2), Array(7, 2), Array(8, 1), _
    Array(9, 1), Array(10, 1), Array(11, 1), Array(12, 1), Array(13, 1), Array(14, 2), Array(15 _
    , 1), Array(16, 1), Array(17, 2), Array(18, 2), Array(19, 2), Array(20, 2), Array(21, 2))

    Rows("1:1").Select
    Selection.Font.Bold = True
    Cells.Select
    Cells.EntireColumn.AutoFit

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

弃爱 2024-12-15 17:11:18

您发布的 VBA 代码看起来像是在 Excel 中完成的宏录制。例如,我通过打开纯文本文件 (.txt) 记录了以下内容:

Workbooks.OpenText Filename:= _
    "C:\test.txt" _
    , Origin:=xlWindows, startRow:=1, DataType:=xlDelimited, TextQualifier _
    :=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, Semicolon:= _
    False, Comma:=False, Space:=False, Other:=False, FieldInfo:=Array(1, 1)

这是在第一个对话框中选中“分隔”选项时记录的。如果我选择“固定宽度”,然后在输入数据中手动创建多个列,则记录如下所示:

Workbooks.OpenText Filename:= _
    "C:\test.txt" _
    , Origin:=xlWindows, startRow:=1, DataType:=xlFixedWidth, FieldInfo:= _
    Array(Array(0, 1), Array(2, 1), Array(10, 1), Array(18, 1), Array(30, 1), _
    Array(41, 1), Array(55, 1))

 Excel 文本导入对话框屏幕截图

如果我必须猜测的话,我会说有人使用“固定宽度”选项录制了宏,然后将 VBA 代码从 DataType:=xlFixedWidth 更改为数据类型:=xlDelimited

如果您的输入数据实际上是分隔的,您应该能够省略所有杂乱的 FieldInfo 数组信息,并让 Excel 自动处理分隔符,只需设置 DataType:=xlDelimited

如果您确实需要指定固定宽度的列,Bobort 发布的 rel="nofollow noreferrer">链接 将是一个很好的起点。从外观上看,FieldInfo 需要一个包含任意数量子数组的 VBA Variant 数组,每个子数组包含两个数字:列宽和数据类型。

编辑:

Origin 字段用于描述文本文件的格式:从 Macintosh、Windows (ANSI) 或 MS-DOS (PC-8) 中选择下拉式菜单。

The VBA code you posted looks like a macro recording done within Excel. For example, I recorded the following by opening a plain text file (.txt):

Workbooks.OpenText Filename:= _
    "C:\test.txt" _
    , Origin:=xlWindows, startRow:=1, DataType:=xlDelimited, TextQualifier _
    :=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, Semicolon:= _
    False, Comma:=False, Space:=False, Other:=False, FieldInfo:=Array(1, 1)

This was recorded with the "Delimited" option checked on the first dialog box. If I instead select "Fixed Width", and then manually create a number of columns in the input data, the recording looks like this:

Workbooks.OpenText Filename:= _
    "C:\test.txt" _
    , Origin:=xlWindows, startRow:=1, DataType:=xlFixedWidth, FieldInfo:= _
    Array(Array(0, 1), Array(2, 1), Array(10, 1), Array(18, 1), Array(30, 1), _
    Array(41, 1), Array(55, 1))

Excel text import dialog box screenshot

If I had to guess, I would say that someone recorded the macro using the "Fixed Width" option, and then later changed the VBA code from DataType:=xlFixedWidth to DataType:=xlDelimited.

If your input data is actually delimited, you should be able to omit all of the messy FieldInfo array info and let Excel handle the delimiters automatically just by setting DataType:=xlDelimited

If you do need to specify fixed-width columns, the link that Bobort posted would be a great place to start. By the look of it, FieldInfo requires a VBA Variant array contaning any number of sub-arrays, each containing two numbers: a column width and a data type.

Edit:

The Origin field is used to describe the format of the text file: Macintosh, Windows (ANSI), or MS-DOS (PC-8) as selected from the drop-down menu.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文