gpt4 book ai didi

vba - 将选定单元格的导出写入不带 "quotation marks"的 .txt 文件

转载 作者:行者123 更新时间:2023-12-03 02:31:10 25 4
gpt4 key购买 nike

我正在处理一个包含大量信息的 Excel 工作表。有些列包含我需要在脚本中使用的信息,我使用以下代码来保存我在单击按钮后在 .txt 文件中选择的内容。

Private Sub CommandButton21_Click()
Dim myFile As String, rng As Range, cellValue As Variant, i As Integer, j As Integer
myFile = Application.DefaultFilePath & "\NumeroChamados.txt"
Set rng = Selection
Open myFile For Output As #1
For i = 1 To rng.Rows.Count
For j = 1 To rng.Columns.Count
cellValue = rng.Cells(i, j).Value
If j = rng.Columns.Count Then

Write #1, cellValue

Else
Write #1, cellValue,
End If
Next j
Next i
Close #1
End Sub

事情是我保存的所有内容都以引号结束,如下例所示:

"4146546546523133"
"4285725763131"
"461"
"4230236646435356694197285187451644148"
"4230375763756379653464564"

我选择的单元格通常包含来自另一个单元格的字符串,我在其中使用宏来获取它。

最佳答案

为避免向 Excel 解释为字符串的任何内容(甚至是看起来像数字的文本)添加换行引号,请使用 Print 而不是 Write

Private Sub CommandButton1_Click()
Dim myFile As String, rng As Range, cellValue As Variant, i As Integer, j As Integer
myFile = Application.DefaultFilePath & "\NumeroChamados.txt"
Set rng = Selection
Open myFile For Output As #1
For i = 1 To rng.Rows.Count
For j = 1 To rng.Columns.Count
cellValue = rng.Cells(i, j).Value
If j = rng.Columns.Count Then
Print #1, cellValue
Else
Print #1, cellValue,
End If
Next j
Next i
Close #1
End Sub

enter image description here

关于vba - 将选定单元格的导出写入不带 "quotation marks"的 .txt 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42983201/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com