使用“打印”时的空格在VBA中

发布于 2024-09-03 13:02:13 字数 576 浏览 1 评论 0原文

由于某种原因,我在尝试打印到平面文本文件时,每个值前面都有很多空格。

'append headers
Cells(start_row - 2, 1).Select
For i = 1 To ActiveCell.SpecialCells(xlLastCell).Column
    If ActiveCell.Offset(0, 1).Column = ActiveCell.SpecialCells(xlLastCell).Column Then
        Print #finalCSV, Cells(start_row - 2, i) & "\n",
    Else
        Print #finalCSV, Cells(start_row - 2, i) & ",",
    End If
Next i

输出示例:

DC Capacity:hi,             Resistive Capacity:lo,      Resistive Capacity:hi,      Reactive Capacity:lo,

有什么方法可以去掉这些空格吗?

For some reason I am getting a lot of spaces in front of each value while trying to print to a flat text file.

'append headers
Cells(start_row - 2, 1).Select
For i = 1 To ActiveCell.SpecialCells(xlLastCell).Column
    If ActiveCell.Offset(0, 1).Column = ActiveCell.SpecialCells(xlLastCell).Column Then
        Print #finalCSV, Cells(start_row - 2, i) & "\n",
    Else
        Print #finalCSV, Cells(start_row - 2, i) & ",",
    End If
Next i

Example output:

DC Capacity:hi,             Resistive Capacity:lo,      Resistive Capacity:hi,      Reactive Capacity:lo,

Is there any way to get rid of these spaces?

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

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

发布评论

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

评论(3

抱着落日 2024-09-10 13:02:13
Print #finalCSV, Cells(start_row - 2, i) & ",";

如果我没记错的话,VB 中的逗号会插入制表符,而分号只会抑制换行符。

Print #finalCSV, Cells(start_row - 2, i) & ",";

If I remember my VB correctly, a comma inserts a tab, while a semicolon just suppresses the newline.

寄居者 2024-09-10 13:02:13
Print #finalCSV, Trim(Cells(start_row - 2, i)) & ",",
Print #finalCSV, Trim(Cells(start_row - 2, i)) & ",",
豆芽 2024-09-10 13:02:13

文森特的答案是正确的,但我想让它更清楚:

打印#finalCSV,单元格(start_row - 2, i) & “,”,

将给出这种输出:

> DC Capacity:hi,             Resistive Capacity:lo,      Resistive
> Capacity:hi,      Reactive Capacity:lo,

并在句子末尾使用分号:

打印#finalCSV,单元格(start_row - 2, i) & “,”;

会给你这个输出:

> DC Capacity:hi,Resistive Capacity:lo,Resistive Capacity:hi,Reactive
> Capacity:lo,

Vincent's answer is right but I want to make it even more clear:

Print #finalCSV, Cells(start_row - 2, i) & ",",

will give this kind of output:

> DC Capacity:hi,             Resistive Capacity:lo,      Resistive
> Capacity:hi,      Reactive Capacity:lo,

and using semicolon at the end of the sentence:

Print #finalCSV, Cells(start_row - 2, i) & ",";

will give you this output:

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