提示:其它语言是由 Google 机器翻译的。 你可以访问 English 版本。
登录
x
or
x
x
马上登记
x

or

如何创建一个命令按钮来复制和粘贴Excel中的数据?

假设您需要在更改数据后经常将一系列单元格复制到其他位置,则手动复制和粘贴方法将非常繁琐且耗时。 如何使这个复制和粘贴自动运行? 本文将向您展示如何使用命令按钮通过单击即可复制和粘贴数据。

创建一个命令按钮,用VBA代码复制和粘贴数据

Office选项卡在Office中启用选项卡式编辑和浏览,使您的工作更轻松......
Kutools for Excel - 最佳办公生产力工具将解决您的大部分Excel问题
  • 重用任何东西: 将最常用或最复杂的公式,图表和其他任何内容添加到您的收藏夹中,并在将来快速重复使用它们。
  • 超过20文本功能: 从文本字符串中提取数字; 提取或删除部分文本; 将数字和货币转换为英语单词...
  • 合并工具:多个工作簿和表格合二为一; 合并多个单元格/行/列而不丢失数据; 合并重复行和总和...
  • 拆分工具:根据价值将数据拆分为多个表格; 一个工作簿到多个Excel,PDF或CSV文件; 一列到多列......
  • 粘贴跳过 隐藏/过滤行; 数和总和 按背景颜色; 创建邮件列表和 通过Cell的价值发送电子邮件...
  • 超级过滤器: 创建高级过滤方案并应用于任何工作表; 排序 按周,日,频率等; 筛选 通过大胆,公式,评论......
  • 超过300强大的功能; 适用于Office 2007-2019和365; 支持所有语言; 在公司轻松部署; 全功能60天免费试用。

创建一个命令按钮,用VBA代码复制和粘贴数据


请按照以下步骤在点击命令按钮时自动复制和粘贴数据。

1。 点击插入命令按钮 开发人员 > 插页 > 命令按钮(ActiveX控件)。 看截图:

2。 在工作表中绘制一个命令按钮并右键单击它。 选择 查看代码 从上下文菜单。

3。 在弹出 Microsoft Visual Basic for Applications 窗口,请使用下面的VBA代码替换代码窗口中的原始代码。

VBA代码:使用命令按钮在Excel中复制和粘贴数据

Private Sub CommandButton1_Click()
    Application.ScreenUpdating = False
    Dim xSheet As Worksheet
    Set xSheet = ActiveSheet
        If xSheet.Name <> "Definitions" And xSheet.Name <> "fx" And xSheet.Name <> "Needs" Then
            xSheet.Range("A1:C17 ").Copy
            xSheet.Range("J1:L17").PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
        End If

    Application.ScreenUpdating = True
End Sub

注意:在代码中,CommandButton1是插入的命令按钮的名称。 A1:C17是您需要复制的范围,而J1:L17是粘贴数据的目标范围。 请根据需要更改它们。

4。 按 其他 + Q 键关闭 Microsoft Visual Basic for Applications 窗口。 然后关闭“开发人员”选项卡下的设计模式。

5。 现在单击命令按钮,A1范围内的所有数据:C17将被复制并粘贴到范围为J1:L17的单元格格式中。


相关文章:


Kutools for Excel - 最佳办公生产力工具提高80%的生产力

  • 重用: 快速插入 复杂的公式,图表 以及你以前用过的任何东西; 加密单元格 密码; 创建邮件列表 并发送电子邮件...
  • 超级方程式酒吧 (轻松编辑多行文字和公式); 阅读布局 (轻松读取和编辑大量单元格); 粘贴到过滤范围...
  • 合并单元格/行/列 不丢失数据; 分裂细胞含量; 组合重复的行/列...防止重复的细胞; 比较范围...
  • 选择复制或唯一 行; 选择空行 (所有细胞都是空的); 超级查找和模糊查找 在许多工作簿中; 随机选择......
  • 精确复制 多个单元格而不更改公式参考; 自动创建参考 多张表; 插入项目符号,复选框等等......
  • 提取文本,添加文本,按位置删除, 删除空间; 创建和打印分页小计; 在单元格内容和注释之间转换...
  • 超级过滤器 (将过滤方案保存并应用到其他工作表); 高级排序 按月/周/日,频率等; 特殊过滤器 用粗体,斜体......
  • 结合工作簿和工作表; 根据键列合并表; 将数据拆分为多个表格; 批量转换xls,xlsx和PDF...
  • 超过300强大的功能。 支持Office / Excel 2007-2019和365。 支持所有语言。 在您的企业或组织中轻松部署。 全功能60天免费试用。
kte tab 201905

Office选项卡为Office提供选项卡式界面,使您的工作更轻松

  • 在Word,Excel,PowerPoint中启用选项卡式编辑和阅读,Publisher,Access,Visio和Project。
  • 在同一窗口的新选项卡中打开并创建多个文档,而不是在新窗口中。
  • 通过50%提高您的工作效率,每天为您减少数百次鼠标点击!
官方底部
Say something here...
symbols left.
You are guest ( Sign Up? )
or post as a guest, but your post won't be published automatically.
Loading comment... The comment will be refreshed after 00:00.
  • To post as a guest, your comment is unpublished.
    Jonas · 5 days ago
    Hello!
    Is there a way to make a code so i can have a button copy a range (lets say A1:D5).
    And then paste it to A6:D10.

    And if i press the button once more it will copy A1:D5 to A11:D15.

    And so on?
  • To post as a guest, your comment is unpublished.
    Lexi · 19 days ago
    Is it possible to make a button that just copies one selected cell on one sheet to another sheet? Thanks!
    • To post as a guest, your comment is unpublished.
      crystal · 11 days ago
      Hi lexi,
      The below VBA code can help you solve the problem. Please change "Sheet3" to the sheet name as you need.
      Select a cell and press the command button to make it work.

      Private Sub CommandButton1_Click()
      Dim xSWName As String
      Dim xSheet As Worksheet
      Dim xPSheet As Worksheet
      Dim xIntR As Integer
      xSWName = "Sheet3"
      On Error Resume Next
      Application.ScreenUpdating = False
      Set xSheet = ActiveSheet
      If xSheet.Name <> "Definitions" And xSheet.Name <> "fx" And xSheet.Name <> "Needs" Then
      Selection.CurrentRegion.Select
      Selection.Copy
      Set xPSheet = Worksheets.Item(xSWName)
      xIntR = xPSheet.UsedRange.Rows.Count
      xPSheet.Cells(xIntR + 1, 1).PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
      End If
      Application.ScreenUpdating = True
      End Sub
  • To post as a guest, your comment is unpublished.
    S J · 1 months ago
    please send VBAcode for copy single cell content using commmand button click without using text box so tha will be able to paste in any application like notepad,msword
  • To post as a guest, your comment is unpublished.
    angela · 3 months ago
    master bagaimana jika berbeda sheet dan copy data yg diinginkan
    contoh sheet1 hanya data kolom B & kolom D copy ke sheet2
    terimakasih
  • To post as a guest, your comment is unpublished.
    Ted Jillett · 4 months ago
    I'm looking for some help on that a user can click on a row number and then hit the command button that it will make a copy of that row and insert it into the row below it.
    • To post as a guest, your comment is unpublished.
      crystal · 2 months ago
      Good day,
      Sorry can help you with that yet. Thanks for your comment.
  • To post as a guest, your comment is unpublished.
    Yesaya · 5 months ago
    hi, help my case, i want have a button to generate macro where i have excel and export the data where in the <" "> to word, thanks
    • To post as a guest, your comment is unpublished.
      crystal · 4 months ago
      Hi,
      Sorry I didn't get your point. Would be nice if you could explain with more details of what you are trying to do.
  • To post as a guest, your comment is unpublished.
    Kyaw Ye Min · 5 months ago
    Hi Author,

    The article you mentioned above is very helpful to me. I am not used to excel code and command. I still need to know rather than this article. I am ok with the copy and paste into next sheet. But I still need to know "how to paste and add into new line in next sheet every time I press the button". Otherwise, my data on next sheet will be replaced every time. I would really be grateful and looking forward to see your reply.
    • To post as a guest, your comment is unpublished.
      crystal · 2 months ago
      Hi Kyaw Ye Min,
      Sorry for replying so late. Follow the steps and replace the code with the below. In the code, Sheet4 is the destination worksheet you will copy data into, please change it and the copied range A1:C17 as you need. Thanks for your comment.

      Private Sub CommandButton1_Click()
      Dim xSWName As String
      Dim xSheet As Worksheet
      Dim xPSheet As Worksheet
      Dim xIntR As Integer
      xSWName = "Sheet4"
      On Error Resume Next
      Application.ScreenUpdating = False
      Set xSheet = ActiveSheet
      If xSheet.Name <> "Definitions" And xSheet.Name <> "fx" And xSheet.Name <> "Needs" Then
      xSheet.Range("A1:C17 ").Copy
      Set xPSheet = Worksheets.Item(xSWName)
      xIntR = xPSheet.UsedRange.Rows.Count
      xPSheet.Cells(xIntR + 1, 1).PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
      End If
      Application.ScreenUpdating = True
      End Sub
      • To post as a guest, your comment is unpublished.
        Chris · 1 months ago
        Hi there please help. I require the same as above but i just need to paste whatever the active cell is into a new line (if possible above previous entries) in a different worksheet. Really appreciate any help with this. Best Regards
  • To post as a guest, your comment is unpublished.
    Jim · 6 months ago
    I have a qusrion I have sheet 1 with a column of date b3:b33 and I want that to be copied to sheet 2 B33:b63 its text but I need the same format for text ie colour size
  • To post as a guest, your comment is unpublished.
    Robbert · 6 months ago
    If I want to Copy this not in the current workbook but into another workbook (which is still not opened) in excel. How do I change this VBA code accordingly?
    • To post as a guest, your comment is unpublished.
      crystal · 6 months ago
      Hi Robbert,
      Sorry can't help you with that. Thanks for commenting.
  • To post as a guest, your comment is unpublished.
    MangkaNorr · 8 months ago
    sir how to copy the range of the cell in current worksheet then paste to another worksheets ?
    • To post as a guest, your comment is unpublished.
      crystal · 7 months ago
      Hi,
      The below VBA code can help you solve the problem.
      You need to replace the "Sheet4" and "A1:C17" in the code with your specified sheet and range.

      Private Sub CommandButton1_Click()
      Dim xSWName As String
      Dim xSheet As Worksheet
      Dim xPSheet As Worksheet
      Dim xIntR As Integer
      xSWName = "Sheet4"
      On Error Resume Next
      Application.ScreenUpdating = False
      Set xSheet = ActiveSheet
      If xSheet.Name <> "Definitions" And xSheet.Name <> "fx" And xSheet.Name <> "Needs" Then
      xSheet.Range("A1:C17 ").Copy
      Set xPSheet = Worksheets.Item(xSWName)
      xIntR = xPSheet.UsedRange.Rows.count
      xPSheet.Cells(xIntR + 1, 1).PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
      End If
      Application.ScreenUpdating = True
      End Sub
  • To post as a guest, your comment is unpublished.
    Adam · 10 months ago
    Would it be possible to for the command button to instantly paste the selected range to a specified sheet (in the next empty cell) rather than having to input each time where you want to paste the data?
    • To post as a guest, your comment is unpublished.
      crystal · 7 months ago
      Hi Adam,
      The below VBA code can help you solve the problem.
      You need to replace the "Sheet4" and "A1:C17" in the code with your specified sheet and range.

      Private Sub CommandButton1_Click()
      Dim xSWName As String
      Dim xSheet As Worksheet
      Dim xPSheet As Worksheet
      Dim xIntR As Integer
      xSWName = "Sheet4"
      On Error Resume Next
      Application.ScreenUpdating = False
      Set xSheet = ActiveSheet
      If xSheet.Name <> "Definitions" And xSheet.Name <> "fx" And xSheet.Name <> "Needs" Then
      xSheet.Range("A1:C17 ").Copy
      Set xPSheet = Worksheets.Item(xSWName)
      xIntR = xPSheet.UsedRange.Rows.count
      xPSheet.Cells(xIntR + 1, 1).PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
      End If
      Application.ScreenUpdating = True
      End Sub
      • To post as a guest, your comment is unpublished.
        Mohamed shoman · 7 months ago
        very good , i need to copy and paste many time is it possible to make a choice to paste a row of data to 3 rows or 5 rows whatever i like ?
  • To post as a guest, your comment is unpublished.
    Jeff McCollum · 10 months ago
    I would like to expand this one step... After pasting the actual values not formulas... I need to copy the new cell and paste in another program that's not excel, should simply I need to click the button copy the formula from a specific cell and past the actual value in another, then copy that new value to windows clipboard to past in another app. Hope this make sense, and appreciate your help in knowing how to add the new code... I know what I want it to do, just not familiar with how to code it...
    Thanks
    • To post as a guest, your comment is unpublished.
      crystal · 9 months ago
      Hi,
      The code has been updated in the article, please have a try. Thank you for your comment.
  • To post as a guest, your comment is unpublished.
    Magnus · 11 months ago
    What if I would like to make a paste queue?
    To explain further. I click CommandButton1 then it copy A1. When I have used paste, the copy from A1 - Then I want to copy A2 without clicking another button, so that I can paste A2 right away, somewhere else. Then When I have used paste, the copy from A2 then copy A3. If that is possible? Can this also work in the background when use a normal Excel sheet, and try to do this action where I copy from Excel, and then paste in a completely different program like a internet browser, another program, word, txt file, and so on?

    This article was anyway extremely helpful, thank you so much!
    • To post as a guest, your comment is unpublished.
      crystal · 10 months ago
      Hi Magnus,
      Sorry can't help you with that. Thanks for your comment.
  • To post as a guest, your comment is unpublished.
    Azam as · 1 years ago
    how is the formula for us to copy files
    then paste it to the next sheet. and paste the next one make a space or keep going down without deleting the previous paste.
    • To post as a guest, your comment is unpublished.
      crystal · 1 years ago
      Good day,
      The below VBA code can help you solve the problem. Please have a try. Thank you.

      Private Sub CommandButton1_Click()
      Dim xSheet, xDWS As Worksheet
      Dim xFNum As Integer
      Dim xSRg As Range

      On Error Resume Next
      Set xSRg = Application.InputBox("Please select cell to paste the range:", "Kutools for Excel", xTxt, , , , , 8)
      If xSRg Is Nothing Then Exit Sub

      Application.ScreenUpdating = False
      Range("A1:C17 ").Copy
      xSRg.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
      Application.ScreenUpdating = True

      End Sub
  • To post as a guest, your comment is unpublished.
    Jason · 1 years ago
    How can you alter the code to paste the results to another page with in the workbook? and how do you get the insert a row before putting the new data in so it does not overwrite it
    • To post as a guest, your comment is unpublished.
      crystal · 1 years ago
      Hi Jason,
      If you want to paste the results to another worksheets within the workbook, please try the below VBA code.

      Private Sub CommandButton1_Click()
      Dim xSheet, xDWS As Worksheet
      Dim xFNum As Integer
      Dim xSRg As Range

      On Error Resume Next
      Set xSRg = Application.InputBox("Please select cell to paste the range:", "Kutools for Excel", xTxt, , , , , 8)
      If xSRg Is Nothing Then Exit Sub

      Application.ScreenUpdating = False
      Range("A1:C17 ").Copy
      xSRg.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
      Application.ScreenUpdating = True

      End Sub
  • To post as a guest, your comment is unpublished.
    Dom · 2 years ago
    I have used this function in my workbook. I am using active x buttons to copy and paste data within the same sheet on multiple sheets. However there are sheets without active x buttons or macros that get pasted data from the respective fields on the sheet which I don't want. Help?