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

I'm working on a Excel sheet that gets a lot of information. Some columns have information that i need to use in a script and i use the following code I've found to save whatever i select in a .txt file after i click a button.

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

Thing is everything i save ends up with quotation marks like the example below:

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

The cells i select usually contain a string from another cell in which i use a macro to get it.

Upvotes: 2

Views: 4394

Answers (1)

user4039065
user4039065

Reputation:

To avoid adding wrapping quotes to anything Excel interprets as a string (even text-that-looks-like-a-number), use Print instead of 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

Upvotes: 3

Related Questions