Leave out quotes when copying from cell

后端 未结 13 1126
情话喂你
情话喂你 2020-12-01 04:16

Problem:
When copying a cell from Excel outside of the program, double-quotes are added automatically.

Details:
I\'m using

13条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-01 04:44

    You can do this in an Excel macro via VBA, sending the results to a file:

    Sub SimpleVBAWriteToFileWithoutQuotes()
        Open "c:\TEMP\Excel\out.txt" For Output As #1
        Print #1, Application.ActiveSheet.Cells(2, 3)
        Close #1
    End Sub
    

    And if you are wanting to get filenames and content into multiple files, here is a short snippet that avoids the double quotes around the output.

    Sub DumpCellDataToTextFilesWithoutDoubleQuotes()
        ' this will work for filename and content in two different columns such as:
        ' filename column       data column
        ' 101                   this is some data
        ' 102                   this is more data
    
        Dim rngData As Range
        Dim strData As String
        Dim strTempFile As String
        Dim strFilename As String
        Dim i As Long
        Dim intFilenameColumn As Integer
        Dim intDataColumn As Integer
        Dim intStartingRow As Integer
    
        intFilenameColumn = 1     ' the column number containing the filenames
        intDataColumn = 3         ' the column number containing the data
        intStartingRow = 2        ' the row number to start gathering data
    
    
        For i = intStartingRow To Range("A1", Range("A1").End(xlDown)).Rows.Count
    
            ' copy the data cell's value
            Set rngData = Application.ActiveSheet.Cells(i, intDataColumn)
    
            ' get the base filename
            strFilename = Application.ActiveSheet.Cells(i, intFilenameColumn)
    
            ' assemble full filename and path
            strTempFile = "w:\TEMP\Excel\" & strFilename & ".txt"
    
            ' write to temp file
            Open strTempFile For Output As #1
            Print #1, rngData
            Close #1
    
        Next i
    
        ' goto home cell
        Application.ActiveSheet.Cells(1, 1).Select
        Range("A1").ClearOutline
    End Sub
    

提交回复
热议问题