2022年4月18日 星期一

Eliminating Double Quotes When Copying Multiline Cells in Excel



Have you ever encountered an issue where Excel adds double quotes when copying a cell containing multiple lines of text? For example, when you copy the following cell content:

Hi,
I am a boy

Excel pastes it as:

"Hi,
I am a boy"

This can be frustrating, especially if you want to retain the original formatting without the added double quotes. In this blog post, we will guide you on how to use Visual Basic for Applications (VBA) to eliminate these unwanted double quotes when copying cells in Excel.

## Using VBA to Remove Double Quotes


To solve this problem, we will create a VBA macro that removes double quotes after copying a cell. Follow these simple steps:

1. Open Excel and press `Alt + F11` to open the Visual Basic for Applications (VBA) editor.

2. In the VBA editor, click on `Insert` in the toolbar and select `Module` from the dropdown menu. This will create a new module in your Excel workbook.

3. Copy and paste the following VBA code into the newly created module:

```vba
Sub CopyWithoutQuotes()
    Dim OriginalClipboard As DataObject
    Dim ModifiedClipboard As DataObject
    Dim OriginalText As String
    
    Set OriginalClipboard = New DataObject
    Set ModifiedClipboard = New DataObject
    
    ' Save the original clipboard content.
    OriginalClipboard.GetFromClipboard
    
    ' Get the copied text from the clipboard.
    OriginalText = OriginalClipboard.GetText
    
    ' Remove double quotes from the text.
    OriginalText = Replace(OriginalText, Chr(34), "")
    
    ' Set the modified text to the clipboard.
    ModifiedClipboard.SetText OriginalText
    ModifiedClipboard.PutInClipboard
End Sub

```

4. Close the VBA editor and return to your Excel workbook.

5. To run the macro, press `Alt + F8` to open the "Macro" dialog. Select `CopyWithoutQuotes` from the list and click "Run."

Now, whenever you copy a cell with multiple lines of text, run this macro before pasting the content. The pasted text will no longer include double quotes.

## Conclusion


By using this VBA macro, you can easily remove any unwanted double quotes that Excel may add when copying cells containing multiline text. This method ensures that your copied content remains true to its original format.

Keep in mind that this macro is a workaround and may require you to run it every time you copy multiline cells. However, it provides a convenient solution to a common Excel issue.

**References:**


- [How to avoid extra double quotes in Excel](https://www.peelonion.com/2017/03/how-to-avoid-extra-double-quotes-excel.html)
- [Why is Excel adding quotes when I copy a cell with multiple lines?](https://answers.microsoft.com/en-us/msoffice/forum/all/why-is-excel-adding-quotes-when-i-copy-a-cell-with/28e23792-4b66-4d6b-94f9-4720c238659a)


沒有留言:

我的白蝕治療之路:從發現到自我管理

  大家好,想和大家分享我與白蝕, 白癜風(vitiligo)抗爭的經歷, 這抗爭於今天開始, 立此Blog 以作紀錄。   2024-08-14 這個故事開始於三年前, 三年前,也就是新冠肺炎初期,當時我剛買了新車,在駕駛過程中突然發現了手上皮膚的異常。我注意到了皮膚的出現了...