117

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

Details:
I'm using Excel 2007 on a Windows 7 machine. If I have a cell with the following formula:

="1"&CHAR(9)&"SOME NOTES FOR LINE 1."&CHAR(9)&"2"&CHAR(9)&"SOME NOTES FOR LINE 2."

The output in the cell (formatted as number) looks like this in Excel:

1SOME NOTES FOR LINE 1.2SOME NOTES FOR LINE 2.

Well and good. But, if I copy the cell into another program, such as notepad, I get annoying double-quotes at the beginning and end. Notice the tabs created by "CHAR(9)" are kept, which is good.

"1  SOME NOTES FOR LINE 1.  2     SOME NOTES FOR LINE 2."

How can I keep these double-quotes from showing up, when I copy to another program? In other words, can I keep these from being automatically added when the cell is copied to clipboard?

pnuts
  • 58,317
  • 11
  • 87
  • 139
Aaron Thomas
  • 5,054
  • 8
  • 43
  • 89
  • Have you tried changing the format to something other than number? I think I have seen ways around that, but I can't open Excel at this exact moment, I'll come back in a few minutes if there's still no answer. – AdamMc331 Jul 23 '14 at 12:23
  • @McAdam331 on my setup, if the format is something like text, the cell displas the formula, not the calculated result. Formatting as number seems to override this for some reason. – Aaron Thomas Jul 23 '14 at 12:25
  • Why did you opt to use the formula? Is it because Excel doesn't support an easy way to insert tab characters? I can't seem to work around this either, maybe there are options other than Excel that you can use but I'm not sure what the nature of the project is. – AdamMc331 Jul 23 '14 at 12:37
  • Also, it may be a hassle but if you open up Excel and begin typing in a cell (which is by default formatted as General) and you enter text, it will copy without the quotes. (Tested using Excel 2013 and Notepad++) Will it affect your project to just enter 4 spaces instead of a tab? Again, hard to say without knowing the rest of the problem. – AdamMc331 Jul 23 '14 at 12:43
  • 1
    @McAdam331 unfortunately I need this to work exactly as I put here, minus the leading and trailing double quotes. Please don't let the tabs take away from the focus of this question - they must be there. – Aaron Thomas Jul 23 '14 at 13:00
  • I noticed someone voted to close this. In case it matters, I am still paying attention to this, to see if a suitable answer comes up. I haven't found a solution that meets the question yet myself, but am keeping an eye out for something that would work. – Aaron Thomas Oct 10 '14 at 18:47
  • I never figured this out. I remember trying so long for this. Have you considered starting a bounty on the question? – AdamMc331 Oct 10 '14 at 19:24
  • This appears to be a duplicate of a question on superuser: http://superuser.com/questions/324271/how-to-copy-multi-line-text-from-excel-without-quotes I'm not an expert here so don't know if this means it needs flagging. – Kit Johnson Jul 02 '15 at 02:46
  • This may be off-topic, but why would MS cause this unwanted behavior in the first place? – posfan12 Jun 16 '19 at 01:52
  • See this answer for the why and a work around https://stackoverflow.com/a/60251126/1279373 – spioter Jun 30 '22 at 21:32

16 Answers16

121

I just had this problem and wrapping each cell with the CLEAN function fixed it for me. That should be relatively easy to do by doing =CLEAN(, selecting your cell, and then autofilling the rest of the column. After I did this, pastes into Notepad or any other program no longer had duplicate quotes.

Paul
  • 4,160
  • 3
  • 30
  • 56
Greg Smalter
  • 6,571
  • 9
  • 42
  • 63
  • 30
    Good answer for erasing extra quotes, but unfortunately it also erases the tab characters... which, in the question, I want to keep. Thanks for the input though! – Aaron Thomas Feb 13 '15 at 13:49
  • This worked for me, I didn't care about tabs - in fact I wanted all of them and newline characters removed anyways. – Cody Oct 08 '15 at 17:52
  • 27
    It removes all ASCII characters below 32. So new lines as well. – NeplatnyUdaj Apr 21 '16 at 15:29
  • @AaronThomas, the existence of the tab characters (or likely a list of other 'strange' characters) is causing/forcing excel to interpret this as text - hence the quotes. It seems like an inherent problem. You have lots of good work-arounds listed here now. My favorite is "=CLEAN()" – Watki02 May 10 '16 at 19:39
  • 2
    This is the real answer. – Tyler Murry May 17 '17 at 00:17
  • 19
    This isn't a good solution as it removes all special characters. – Paul Sep 22 '17 at 11:09
  • 1
    YOU ARE MY SAVIOUR. Was not expecting this approach to work, but I'll be damned if this doesn't work PERFECTLY. – 1owk3y Jan 08 '18 at 01:28
  • I am creating Xml from an Excel document, building it with a mix of Xml strings and concatenating values from the document, the result pasting into the Xml editor had the double quotes, and after wrapping with `CLEAN()` it solves the issue. The Xml editor takes care of the indentation and line feeds automatically so that is not a problem. – djv Jan 31 '22 at 21:42
  • As long as you don't need a CR/LF character, ... `=CLEAN("some text")&CHAR(09)&CLEAN("some text after the TAB")&CHAR(09)&CLEAN("and so on, ...") – ergohack Jun 16 '23 at 19:44
40

If you try pasting into Word-Pad, Notepad++ or Word you wouldn't have this issue. To copy the cell value as pure text, to achieve what you describe you have to use a macro:

In the workbook where you want this to apply (or in your Personal.xls if you want to use across several workbooks), place the following code in a standard module:

Code:

Sub CopyCellContents()
'create a reference in the VBE to Microsft Forms 2.0 Lib
' do this by (in VBA editor) clicking tools - > references and then ticking "Microsoft Forms 2.0 Library"
Dim objData As New DataObject
Dim strTemp As String
strTemp = ActiveCell.Value
objData.SetText (strTemp)
objData.PutInClipboard
End Sub

To add a standard module to your project (workbook), open up the VBE with Alt+F11 and then right-click on your workbook in the top left Project Window and select Insert>Module. Paste the code into the code module window which will open on the right.

Back in Excel, go Tools>Macro>Macros and select the macro called "CopyCellContents" and then choose Options from the dialog. Here you can assign the macro to a shortcut key (eg like CTRL+C for normal copy) - I used CTRL+Q.

Then, when you want to copy a single cell over to Notepad/wherever, just do Ctrl+q (or whatever you chose) and then do a CTRL+V or Edit>Paste in your chosen destination.

My answer is copied (with a few additions) from: here

EDIT: (from comments)

If you don't find Microsoft Forms 2.0 Library in the references list, You can try

  • looking for FM20.DLL instead (thanks @Peter Smallwood)
  • clicking Browse and selecting C:\Windows\System32\FM20.dll (32 bit Windows) (thanks @JWhy)
  • clicking Browse and selecting C:\Windows\SysWOW64\FM20.dll (on 64-bit)
user3616725
  • 3,485
  • 1
  • 18
  • 27
  • 1
    It looks like VBA is the only solution to this - thanks for the input. – Aaron Thomas Mar 16 '15 at 20:46
  • Could this be done without "Microsft Forms 2.0 Lib"? I don't have it in a list of available references and I fear that when I somehow import the dll, the workbook won't work on another PC. – NeplatnyUdaj Apr 21 '16 at 15:34
  • 1
    If you can't see the Microsoft Forms lib in the list of references then browse for FM20.DLL – Peter Smallwood Dec 25 '16 at 09:17
  • 19
    I still get the quotes if I paste into Notepad++. – Kat Feb 20 '17 at 21:42
  • found any solution @kat ? – monkey intern May 11 '17 at 08:25
  • Perfect! Thanks - I had to use FM20.DLL. – chris Jun 02 '17 at 16:39
  • 1
    @monkeyintern I think I had multiple lines of text in one cell, and splitting them into one line per cell let me get around it. – Kat Jun 26 '17 at 22:22
  • The above code only works on single cells. How do you make it work on an entire selection spanning multiple cells? – posfan12 Nov 12 '17 at 22:26
  • I get the same issue in Notepad++. – Hamid Sarfraz Dec 26 '17 at 09:14
  • 1
    Your first sentence is inaccurate. In Word, paste (Keep Text Only) retains the quotes. Also as others nave noted, pasting in Notepad++ produces quotes too. – Tony Jun 10 '18 at 16:57
  • @Tony and others. Please note OP's original question: "When copying **a cell**". When I tested this 4 (!) years ago, when pasting a SINGLE cell into the aforementioned software, quotes were not pasted. It's possible that either you are pasting MULTIPLE cells (which does result in quotes) or the software version has been updated in the 4 years. – user3616725 Jun 11 '18 at 15:15
  • 3
    @user3616725 Updated versions may have changed this behavior, but the current versions of Word and Notepad++ (as of this comment's post date) paste these quotes when either a single or multiple cell selection. – Tony Jun 11 '18 at 17:32
  • 1
    Single cells or multiple cells, I still get the quotes in Notepad++. – posfan12 Sep 08 '20 at 05:16
  • might be in `C:\Program Files\Microsoft Office\root\vfs\System\FM20.DLL` – Nor.Z Dec 15 '22 at 08:58
35

First paste it into Word, then you can paste it into notepad and it will appear without the quotes

Praveen Gowda I V
  • 9,569
  • 4
  • 41
  • 49
Jonathan Wilson
  • 626
  • 6
  • 14
  • Thanks for the input, but I can't mark this as a good solution. Plus it's a duplicate of a solution that's already been offered to this question, see http://stackoverflow.com/a/24913557/2658159. – Aaron Thomas Mar 16 '15 at 20:45
  • 4
    Thanks, I liked this solution. Quick and easy – Bajal Jan 06 '16 at 18:11
  • Upvoting this one... in my case, my issue was that, for a particular type of request I receive, data comes in an Excel file and I need to paste it into a plain text box. When I do that, I get the quotes. I had used a text editor to remove the extra quotes (it quoted quotes too.. annoying!). Looking for a simple solution, I found pasting to Word first works as the extra quotes are gone. Wish I could upvote this 10x :) – Carnix Apr 24 '18 at 20:20
  • This will remove the tab characters. At least when using word, not sure about wordpad – Matthew Lueder Jun 22 '18 at 20:25
  • This is the best solution even for today. – zygimantus Aug 21 '18 at 10:59
  • @zygimantus even today too! – Jithin P Gopal May 09 '22 at 10:00
10

If you want to select multiple cells and copy their values to the clipboard without all those annoying quotes the following code may be useful. This is an enhancement of the code given above from user3616725.

Sub CopyCells()
 'Attach Microsoft Forms 2.0 Library: tools\references\Browse\FM20.DLL
 'Then set a keyboard shortcut to the CopyCells Macro (eg Crtl T)
 Dim objData As New DataObject
 Dim cell As Object
 Dim concat As String
 Dim cellValue As String
 CR = ""
  For Each cell In Selection
  If IsNumeric(cell.Value) Then
   cellValue = LTrim(Str(cell.Value))
  Else
   cellValue = cell.Value
  End If
  concat = concat + CR + cellValue
  CR = Chr(13)
 Next
 objData.SetText (concat)
 objData.PutInClipboard
End Sub
Peter Smallwood
  • 470
  • 5
  • 16
  • I think this is the best answer so far. – posfan12 Nov 12 '17 at 22:31
  • 2
    See my Answer based on this Answer that fixes the following bugs with it: 1. Convert an Empty Cell to an empty String vs. "0". 2. Append Tab (ASCII 9) vs. CR (ASCII 13) after each Cell. 3. 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). – Tom Jun 29 '18 at 21:38
  • ^Also fixes "Variable not defined" Compiler Error (for "CR" Variable). – Tom Jun 29 '18 at 22:35
5

"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
Tom
  • 870
  • 11
  • 13
4

My solution when I hit the quotes issue was to strip carriage returns from the end of my cells' text. Because of these carriage returns (inserted by an external program), Excel was adding quotes to the entire string.

3

Possible problem in relation to answer from "user3616725":
Im on Windows 8.1 and there seems to be a problem with the linked VBA code from accepted answer from "user3616725":

Sub CopyCellContents()
 ' !!! IMPORTANT !!!:
 ' CREATE A REFERENCE IN THE VBE TO "Microsft Forms 2.0 Library" OR "Microsft Forms 2.0 Object Library"
 ' DO THIS BY (IN VBA EDITOR) CLICKING TOOLS -> REFERENCES & THEN TICKING "Microsoft Forms 2.0 Library" OR "Microsft Forms 2.0 Object Library"
 Dim objData As New DataObject
 Dim strTemp As String
 strTemp = ActiveCell.Value
 objData.SetText (strTemp)
 objData.PutInClipboard
End Sub

Details:
Running above code and pasting clipboard into a cell in Excel I get two symbols composed of squares with a question mark inside, like this: ⍰⍰. Pasting into Notepad doesn't even show anything.

Solution:
After searching for quite some time I found another VBA script from user "Nepumuk" which makes use of the Windows API. Here's his code that finally worked for me:

Option Explicit

Private Declare Function OpenClipboard Lib "user32.dll" ( _
    ByVal hwnd As Long) As Long
Private Declare Function CloseClipboard Lib "user32.dll" () As Long
Private Declare Function EmptyClipboard Lib "user32.dll" () As Long
Private Declare Function SetClipboardData Lib "user32.dll" ( _
    ByVal wFormat As Long, _
    ByVal hMem As Long) As Long
Private Declare Function GlobalAlloc Lib "kernel32.dll" ( _
    ByVal wFlags As Long, _
    ByVal dwBytes As Long) As Long
Private Declare Function GlobalLock Lib "kernel32.dll" ( _
    ByVal hMem As Long) As Long
Private Declare Function GlobalUnlock Lib "kernel32.dll" ( _
    ByVal hMem As Long) As Long
Private Declare Function GlobalFree Lib "kernel32.dll" ( _
    ByVal hMem As Long) As Long
Private Declare Function lstrcpy Lib "kernel32.dll" ( _
    ByVal lpStr1 As Any, _
    ByVal lpStr2 As Any) As Long

Private Const CF_TEXT As Long = 1&

Private Const GMEM_MOVEABLE As Long = 2

Public Sub Beispiel()
    Call StringToClipboard("Hallo ...")
End Sub

Private Sub StringToClipboard(strText As String)
    Dim lngIdentifier As Long, lngPointer As Long
    lngIdentifier = GlobalAlloc(GMEM_MOVEABLE, Len(strText) + 1)
    lngPointer = GlobalLock(lngIdentifier)
    Call lstrcpy(ByVal lngPointer, strText)
    Call GlobalUnlock(lngIdentifier)
    Call OpenClipboard(0&)
    Call EmptyClipboard
    Call SetClipboardData(CF_TEXT, lngIdentifier)
    Call CloseClipboard
    Call GlobalFree(lngIdentifier)
End Sub

To use it the same way like the first VBA code from above, change the Sub "Beispiel()" from:

Public Sub Beispiel()
    Call StringToClipboard("Hallo ...")
End Sub

To:

Sub CopyCellContents()
    Call StringToClipboard(ActiveCell.Value)
End Sub

And run it via Excel macro menu like suggested from "user3616725" from accepted answer:

Back in Excel, go Tools>Macro>Macros and select the macro called "CopyCellContents" and then choose Options from the dialog. Here you can assign the macro to a shortcut key (eg like Ctrl+c for normal copy) - I used Ctrl+q.

Then, when you want to copy a single cell over to Notepad/wherever, just do Ctrl+q (or whatever you chose) and then do a Ctrl+v or Edit>Paste in your chosen destination.


Edit (21st of November in 2015):
@ comment from "dotctor":
No, this seriously is no new question! In my opinion it is a good addition for the accepted answer as my answer addresses problems that you can face when using the code from the accepted answer. If I would have more reputation, I would have created a comment.
@ comment from "Teepeemm":
Yes, you are right, answers beginning with title "Problem:" are misleading. Changed to: "Possible problem in relation to answer from "user3616725":". As a comment I certainly would have written much more compact.

SourceSeeker
  • 121
  • 7
  • I think this works as a valid answer (although opening with **Problem** made it seem like you had a new problem). And comments are capped at 600 characters, so this would definitely be too long for that. – Teepeemm Nov 21 '15 at 20:21
  • Wonderful! Thanks! The only problem seems to be that it only works for a single cell. Can it be improved somehow to allow copying ranges? – NeplatnyUdaj Apr 21 '16 at 15:37
2

Note:The cause of the quotes is that when data moves from excel to clipboard it is fully complying with CSV standards which include quoting values that include tabs, new lines etc (and double-quote characters are replaced with two double-quote characters )

So another approach, especially as in OP's case when tabs/new lines are due to the formula, is to use alternate characters for tabs and hard returns. I use ascii Unit Separator =char(31) for tabs and ascii Record Separator =char(30) for new lines.

Then pasting into text editor will not involve the extra CSV rules and you can do a quick search and replace to convert them back again.

If the tabs/new lines are embedded in the data, you can do a search and replace in excel to convert them.

Whether using formula or changing the data, the key to choosing delimiters is never use characters that can be in the actual data. This is why I recommend the low level ascii characters.

spioter
  • 1,829
  • 1
  • 13
  • 19
  • This is useful - I hadn't thought about it this way, but doing a search/replace to convert MYCHAR back into newlines is much easier than trying to tidy up the rest of the mess caused by Excel messing with quotes. Thank you! – caponica Jun 28 '22 at 12:22
2

Much easier method:
Paste first into a draft new email - no quotes will be included.
Ctrl-A and copy again from there, discard the draft email.

AjV Jsy
  • 5,799
  • 4
  • 34
  • 30
  • this almost works perfectly; new lines between cells get lost too, which may be the desired behavior, but not in my case – VeraKozya May 12 '23 at 13:19
1

To keep line breaks when pasting in notepad, replace this line in the macro:

strTemp = ActiveCell.Value

by:

strTemp = Replace(ActiveCell.Value, Chr(10), vbCrLf)
JCP
  • 21
  • 3
0

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
ColmanJ
  • 457
  • 2
  • 12
  • 28
JoshInDC
  • 31
  • 3
0
  • if formula having multi line (means having line break in formula) then copy paste will work in that way
  • if can remove multi line then no quotes will appear while copy paste.
  • else use CLEAN function as said by @greg in previous answer
vignesh
  • 1,414
  • 5
  • 19
  • 38
0

I was with the same problem and none of the solutions of this post helped me. Then I'll share the solution which definitely worked well for me, in case others may be in the same situation.

First, this solution also complies with one bug recently reported to Microsoft, which was causing the clipboard content to be transformed into unreadable content, after any modification using VBA when the user accessed any "Quick Acces Folder" using file explorer.

Documentation for the solution of the copy past bug, which the code will be used in this answer, to remove the quotes from clipboard: https://learn.microsoft.com/en-us/office/vba/access/Concepts/Windows-API/send-information-to-the-clipboard

You'll need to build a macro as below, and assign the "ctrl+c" as a hotkey to it. (Hotkey assignment = Developer tab, Macros, click the macro, options, then put the letter "c" in the hotkey field).

Sub ClipboardRemoveQuotes()
    Dim strClip As String
    strClip = Selection.Copy
    strClip = GetClipboard()
    On Error Resume Next - Needed in case clipboard is empty
    strClip = Replace(strClip, Chr(34), "") 
    On Error GoTo 0
    SetClipboard (strClip)
End Sub

This will still need for you to build the functions "SetClipboard" and "GetClipboard".

Below we have the definition of the "SetClipboard" and "GetClipboard" functions, with a few adjustments to fit different excel versions. (Put the below code in a module)

    Option Explicit
#If VBA7 Then
    Private Declare PtrSafe Function OpenClipboard Lib "User32" (ByVal hWnd As LongPtr) As LongPtr
    Private Declare PtrSafe Function EmptyClipboard Lib "User32" () As LongPtr
    Private Declare PtrSafe Function CloseClipboard Lib "User32" () As LongPtr
    Private Declare PtrSafe Function IsClipboardFormatAvailable Lib "User32" (ByVal wFormat As LongPtr) As LongPtr
    Private Declare PtrSafe Function GetClipboardData Lib "User32" (ByVal wFormat As LongPtr) As LongPtr
    Private Declare PtrSafe Function SetClipboardData Lib "User32" (ByVal wFormat As LongPtr, ByVal hMem As LongPtr) As LongPtr
    Private Declare PtrSafe Function GlobalAlloc Lib "kernel32.dll" (ByVal wFlags As Long, ByVal dwBytes As Long) As LongPtr
    Private Declare PtrSafe Function GlobalLock Lib "kernel32.dll" (ByVal hMem As LongPtr) As LongPtr
    Private Declare PtrSafe Function GlobalUnlock Lib "kernel32.dll" (ByVal hMem As LongPtr) As LongPtr
    Private Declare PtrSafe Function GlobalSize Lib "kernel32" (ByVal hMem As LongPtr) As Long
    Private Declare PtrSafe Function lstrcpy Lib "kernel32.dll" Alias "lstrcpyW" (ByVal lpString1 As Any, ByVal lpString2 As Any) As LongPtr
#Else
    Private Declare Function OpenClipboard Lib "user32.dll" (ByVal hWnd As Long) As Long
    Private Declare Function EmptyClipboard Lib "user32.dll" () As Long
    Private Declare Function CloseClipboard Lib "user32.dll" () As Long
    Private Declare Function IsClipboardFormatAvailable Lib "user32.dll" (ByVal wFormat As Long) As Long
    Private Declare Function GetClipboardData Lib "user32.dll" (ByVal wFormat As Long) As Long
    Private Declare Function SetClipboardData Lib "user32.dll" (ByVal wFormat As Long, ByVal hMem As Long) As Long
    Private Declare Function GlobalAlloc Lib "kernel32.dll" (ByVal wFlags As Long, ByVal dwBytes As Long) As Long
    Private Declare Function GlobalLock Lib "kernel32.dll" (ByVal hMem As Long) As Long
    Private Declare Function GlobalUnlock Lib "kernel32.dll" (ByVal hMem As Long) As Long
    Private Declare Function GlobalSize Lib "kernel32" (ByVal hMem As Long) As Long
    Private Declare Function lstrcpy Lib "kernel32.dll" Alias "lstrcpyW" (ByVal lpString1 As Long, ByVal lpString2 As Long) As Long
#End If

Public Sub SetClipboard(sUniText As String)
    #If VBA7 Then
        Dim iStrPtr As LongPtr
        Dim iLock As LongPtr
    #Else
        Dim iStrPtr As Long
        Dim iLock As Long
    #End If
    Dim iLen As Long
    Const GMEM_MOVEABLE As Long = &H2
    Const GMEM_ZEROINIT As Long = &H40
    Const CF_UNICODETEXT As Long = &HD
    OpenClipboard 0&
    EmptyClipboard
    iLen = LenB(sUniText) + 2&
    iStrPtr = GlobalAlloc(GMEM_MOVEABLE Or GMEM_ZEROINIT, iLen)
    iLock = GlobalLock(iStrPtr)
    lstrcpy iLock, StrPtr(sUniText)
    GlobalUnlock iStrPtr
    SetClipboardData CF_UNICODETEXT, iStrPtr
    CloseClipboard
End Sub

Public Function GetClipboard() As String
#If VBA7 Then
    Dim iStrPtr As LongPtr
    Dim iLock As LongPtr
#Else
    Dim iStrPtr As Long
    Dim iLock As Long
#End If
    Dim iLen As Long
    Dim sUniText As String
    Const CF_UNICODETEXT As Long = 13&
    OpenClipboard 0&
    If IsClipboardFormatAvailable(CF_UNICODETEXT) Then
        iStrPtr = GetClipboardData(CF_UNICODETEXT)
        If iStrPtr Then
            iLock = GlobalLock(iStrPtr)
            iLen = GlobalSize(iStrPtr)
            sUniText = String$(iLen \ 2& - 1&, vbNullChar)
            lstrcpy StrPtr(sUniText), iLock
            GlobalUnlock iStrPtr
        End If
        GetClipboard = sUniText
    End If
    CloseClipboard
End Function

I hope it may help others as well as it helped me.

Gabz
  • 391
  • 5
  • 11
0

Same problem: I need the outcame of Excel formulas (including newlines, but with no extra quotes) as input for a console program.

My personal solution: instead of program, I run squ | program (or squ | program > OUT) and then copy/paste the content of the cells into the console (a manual [Ctrl]+[Z] may be needed at the end).

The source of squ is:

#include <stdio.h>

int main(void)
{
 char c[2];
 while(fgets(c,2,stdin)) if(*c!=34) fputs(c,stdout);
 return 0;
}
GGa
  • 97
  • 4
-1

Please use the below formula

=Clean("1"&CHAR(9)&"SOME NOTES FOR LINE 1."&CHAR(9)&"2"&CHAR(9)&"SOME NOTES FOR LINE 2.")

and you will get what you want ;-)

altocumulus
  • 21,179
  • 13
  • 61
  • 84
-3

It's also possible to remove these double-quotes by placing your result on the "Clean" function.

Example:

=CLEAN("1"&CHAR(9)&"SOME NOTES FOR LINE 1."&CHAR(9)&"2"&CHAR(9)&"SOME NOTES FOR LINE 2.")

The output will be pasted without the double-quotes on other programs such as Notepad++.

geisterfurz007
  • 5,292
  • 5
  • 33
  • 54