简体   繁体   English

在 VBA Excel 中用覆盖多个单元格的内容填充剪贴板,这些单元格本身包含换行符 (Alt-Enter)?

[英]Filling the Clipboard with content covering multiple cells which itself are containing line breaks (Alt-Enter) in VBA Excel?

I'm working on a VBA Excel Add-In to provide users with clipboard text containing values from multiple cells, but also having multiple lines within a cell.我正在开发一个 VBA Excel 加载项,为用户提供包含来自多个单元格的值的剪贴板文本,但在一个单元格中也有多行。 Unfortunately I couldn't find a solution which keeps cell seperations as well as line breaks within a cell, correctly.不幸的是,我找不到一个解决方案来正确地保持单元格分离以及单元格内的换行符。 Is there any way of modifying my exemplary Sub "ClipboardDoesntWork" in order to deliver the same clipboard as Sub "CopyPasteWorks" but without using the workaround of filling cells and copying them?有什么方法可以修改我的示例性 Sub“ClipboardDoesntWork”,以便提供与 Sub“CopyPasteWorks”相同的剪贴板,但不使用填充单元格并复制它们的解决方法?

Public Sub CopyPasteWorks()

     Dim text1a As String
     Dim text1b As String
     Dim text2a As String
     Dim text2b As String
     Dim firstcell As String
     Dim secondcell As String
          
     Columns("A:C").Clear
     Columns("A:C").ColumnWidth = 20
          
     text1a = "Line 1 in Cell 1"
     text1b = "Line 2 in Cell 1"
     text2a = "Line 1 in Cell 2"
     text2b = "Line 2 in Cell 2"
     
     firstcell = text1a & vbLf & text1b
     secondcell = text2a & vbLf & text2b
     
     Range("A1").Value = firstcell
     Range("A2").Value = secondcell  
     Range("A1:A2").Copy     
     MsgBox "Clipboard filled with content of two cells!" & vbLf & "(covering two lines, each)"

End Sub

Public Sub ClipboardDoesntWork()
'NOTE! Code requires library reference: Microsoft Forms 2.0 Object Library

     Dim text1a As String
     Dim text1b As String
     Dim text2a As String
     Dim text2b As String
     Dim firstcell As String
     Dim secondcell As String
     Dim objData As New DataObject
          
     text1a = "Line 1 in Cell 1"
     text1b = "Line 2 in Cell 1"
     text2a = "Line 1 in Cell 2"
     text2b = "Line 2 in Cell 2"
     
     firstcell = text1a & vbLf & text1b
     secondcell = text2a & vbLf & text2b
     
     objData.SetText firstcell & vbCrLf & secondcell
     objData.PutInClipboard
     MsgBox "Clipboard filled with content of four cells!"

End Sub

I tried various versions of line feeds and/or carrier returns (vbLf, vbCr, vbCrLf, vbNewLine, chr(10)) and different techniques of filling the clipboard (MSForms.DataObject, API method), but nothing works so far.我尝试了各种版本的换行和/或载体返回(vbLf、vbCr、vbCrLf、vbNewLine、chr(10))和填充剪贴板的不同技术(MSForms.DataObject、API 方法),但到目前为止没有任何效果。 (My final Code is generating the clipboard string by joing array fields (coming from a database query), but the hurdle I couldn't take can also be demonstrated well with the simplified codes shown above.) (我的最终代码是通过连接数组字段(来自数据库查询)生成剪贴板字符串,但我无法克服的障碍也可以用上面显示的简化代码很好地证明。)

Okay, I have the answer.好的,我有答案了。
Close your File Browser.关闭文件浏览器。
Apparently .PutInClipboard doesn't work in Windows 10 while browser is open.显然,当浏览器打开时,.PutInClipboard 在.PutInClipboard 10 中不起作用。

Option Explicit
Public Sub ClipboardDoesntWork()
'NOTE! Code requires library reference: Microsoft Forms 2.0 Object Library

     Dim text1a As String
     Dim text1b As String
     Dim text2a As String
     Dim text2b As String
     Dim firstcell As String
     Dim secondcell As String
     Dim objData As DataObject
     Set objData = New DataObject
          
     text1a = "Line 1 in Cell 1"
     text1b = "Line 2 in Cell 1"
     text2a = "Line 1 in Cell 2"
     text2b = "Line 2 in Cell 2"
     
     firstcell = text1a & Chr(10) & text1b
     secondcell = text2a & Chr(10) & text2b
     
     objData.SetText firstcell & Chr(10) & secondcell
     objData.PutInClipboard
     MsgBox "Clipboard filled with content of four cells!"

End Sub

Example:例子:
在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使Excel将文件另存为文本,并支持Unicode,并支持单元格中的多行(alt输入)? - How to make Excel save a file as text, with support for Unicode, and support for multiple lines within a cell (alt-enter)? Pandas/Excel:调用 DataFrame.to_excel() 时,有什么方法可以将 ALT-ENTER / CHAR(10) 换行符编码为数据? - Pandas/Excel: Any way to encode the ALT-ENTER / CHAR(10) line break into data when calling DataFrame.to_excel()? 在换行符处拆分单元格,其中包含VBA中的内容 - split cells at line breaks with content with in VBA 将文本粘贴到 excel 单元格中,其中一些单元格包含换行符 - Pasting text into excel cells with some cells containing line breaks 单元格中的 Excel VBA 换行符会减慢合并任务的执行速度 - Excel VBA line breaks in cells slow down the execution of a merging task 使用VBA插入换行符(Alt + Enter) - Insert a line break (Alt+Enter) with VBA 如何使用换行符将具有换行符的不同单元格拆分为一个单元格(VBA Excel) - How to loop the split of different cells with line breaks into one cell with line breaks (VBA Excel) VBA Excel - 用 Alt+ Enter 替换 Enter 键 - VBA Excel - Replace Enter key with Alt+ Enter 在excel VBA中识别换行符 - Indentify line breaks in excel VBA SAS / Excel-展开包含Alt + Enter的单个单元格进入行 - SAS / Excel - Expand Single Cell Containing Alt+Enter into Rows
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM