将所选单元格导出为 .txt 文件,无需 "quotation marks"

Write export of selected cells as a .txt file without "quotation marks"

我正在研究 Excel sheet,它可以获得大量信息。 有些列包含我需要在脚本中使用的信息,我使用我发现的以下代码在单击按钮后将 select 保存在 .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"

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

为避免向任何 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