复制excel vb中的多个标签和文本框

时间:2018-03-07 22:58:31

标签: excel-vba vba excel

我的问题是复制按钮我老实说不知道从哪里开始我试过谷歌但没有结果,我知道这是一个混乱的代码,但请耐心等待我。我刚从互联网上复制的一些代码就像消息框一样,我希望复制按钮复制Ctrl + C,当我按Ctrl + V到记事本时都会显示。您可以查看我发布的图片。

Userform


这是我的代码:

Private Sub CommandButton2_Click()

Answer = MsgBox("Are you sure you want to continue?", vbYesNoCancel + vbInformation, "Application Message")

If Answer = vbYes Then Else Exit Sub

    TextBox1.Text = ""
    TextBox2.Text = ""
    TextBox3.Text = ""
    TextBox4.Text = ""
    TextBox5.Text = ""
    TextBox6.Text = ""
    TextBox7.Text = ""
    TextBox8.Text = ""
    TextBox9.Text = ""
    TextBox10.Text = ""
    TextBox11.Text = ""
---------------------------------------
Private Sub CommandButton3_Click()  <--COPY button

Label1.text&""&TextBox1.text Select
Label2.text&""&TextBox2.text Select
and so forth
Select.Copy

End Sub

1 个答案:

答案 0 :(得分:0)

以下宏允许您将一串文本发送到剪贴板:

Sub CopyText(Text As String)
    'VBA Macro using late binding to copy text to clipboard.
    'By Justin Kay, 8/15/2014
    Dim MSForms_DataObject As Object
    Set MSForms_DataObject = CreateObject("new:{1C3B4210-F441-11CE-B9EA-00AA006B1A69}")
    MSForms_DataObject.SetText Text
    MSForms_DataObject.PutInClipboard
    Set MSForms_DataObject = Nothing
End Sub

免责声明:这不是我的,但它来自this answer

将此宏添加到项目后,复制按钮的代码应如下所示:

Private Sub CommandButton3_Click()  <--COPY button

    Dim stringToCopy As String
    stringToCopy = Label1.Text & ": " & TextBox1.Text & vbNewLine
    stringToCopy = stringToCopy & Label2.Text & ": " & TextBox2.Text & vbNewLine
    'etc.

    'then you send this string to the clipboard:
    CopyText stringToCopy

End Sub

您的剪贴板现在包含您可以粘贴到计算机中任意位置的字符串。