Leave out quotes when copying from cell

后端 未结 13 1061
情话喂你
情话喂你 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:49

    "If you want to Select multiple Cells and Copy their values to the Clipboard without all those annoying quotes" (without the bugs in Peter Smallwood's multi-Cells solution) "the following code may be useful." This is an enhancement of the code given above from Peter Smallwood (which "is an enhancement of the code given above from user3616725"). This fixes the following bugs in Peter Smallwood's solution:

    • Avoids "Variable not defined" Compiler Error (for "CR" - "clibboardFieldDelimiter " here)
    • Convert an Empty Cell to an empty String vs. "0".
    • Append Tab (ASCII 9) vs. CR (ASCII 13) after each Cell.
    • Append a CR (ASCII 13) + LF (ASCII 10) (vs. CR (ASCII 13)) after each Row.

    NOTE: You still won't be able to copy characters embedded within a Cell that would cause an exit of the target field you're Pasting that Cell into (i.e. Tab or CR when Pasting into the Edit Table Window of Access or SSMS).


    Option Explicit
    
    Sub CopyCellsWithoutAddingQuotes()
    
    ' -- Attach Microsoft Forms 2.0 Library: tools\references\Browse\FM20.DLL
    ' -- NOTE: You may have to temporarily insert a UserForm into your VBAProject for it to show up.
    ' -- Then set a Keyboard Shortcut to the "CopyCellsWithoutAddingQuotes" Macro (i.e. Crtl+E)
    
    Dim clibboardFieldDelimiter As String
    Dim clibboardLineDelimiter As String
    Dim row As Range
    Dim cell As Range
    Dim cellValueText As String
    Dim clipboardText As String
    Dim isFirstRow As Boolean
    Dim isFirstCellOfRow As Boolean
    Dim dataObj As New dataObject
    
    clibboardFieldDelimiter = Chr(9)
    clibboardLineDelimiter = Chr(13) + Chr(10)
    isFirstRow = True
    isFirstCellOfRow = True
    
    For Each row In Selection.Rows
    
        If Not isFirstRow Then
            clipboardText = clipboardText + clibboardLineDelimiter
        End If
    
        For Each cell In row.Cells
    
            If IsEmpty(cell.Value) Then
    
                cellValueText = ""
    
            ElseIf IsNumeric(cell.Value) Then
    
                cellValueText = LTrim(Str(cell.Value))
    
            Else
    
                cellValueText = cell.Value
    
            End If ' -- Else Non-empty Non-numeric
    
            If isFirstCellOfRow Then
    
                clipboardText = clipboardText + cellValueText
                isFirstCellOfRow = False
    
            Else ' -- Not (isFirstCellOfRow)
    
                clipboardText = clipboardText + clibboardFieldDelimiter + cellValueText
    
            End If ' -- Else Not (isFirstCellOfRow)
    
        Next cell
    
        isFirstRow = False
        isFirstCellOfRow = True
    
    Next row
    
    clipboardText = clipboardText + clibboardLineDelimiter
    
    dataObj.SetText (clipboardText)
    dataObj.PutInClipboard
    
    End Sub
    

提交回复
热议问题