提示:其他語言是Google翻譯的。 你可以訪問 English 版本。
登入
x
or
x
x
註冊
x

or

如何創建一個命令按鈕來複製和粘貼Excel中的數據?

假設您在更改數據後需要頻繁地將一系列單元格複製到其他位置,手動複製和粘貼方法將非常繁瑣且耗時。 如何使這個複制粘貼事務自動運行? 本文將向您展示如何使用命令按鈕來複製和粘貼數據只需點擊一下。

創建一個命令按鈕來複製和粘貼數據與VBA代碼

Office選項卡在Office中啟用選項卡式編輯和瀏覽,使您的工作更輕鬆......
Kutools for Excel解決了您的大多數問題,並使您的生產率提高了80%
  • 重用任何東西: 將最常用或最複雜的公式,圖表和其他任何內容添加到您的收藏夾中,並在將來快速重複使用它們。
  • 超過20文本功能: 從文本字符串中提取數字; 提取或刪除部分文本; 將數字和貨幣轉換為英語單詞...
  • 合併工具:多個工作簿和表格合二為一; 合併多個單元格/行/列而不丟失數據; 合併重複行和總和...
  • 拆分工具:根據價值將數據拆分為多個表格; 一個工作簿到多個Excel,PDF或CSV文件; 一列到多列......
  • 粘貼跳過 隱藏/過濾行; 數和總和 按背景顏色; 創建郵件列表和 通過Cell的價值發送電子郵件...
  • 超級過濾器: 創建高級過濾方案並應用於任何工作表; 分類 按週,日,頻率等; 過濾 通過大膽,公式,評論......
  • 超過300強大的功能; 與Office 2007-2019和365一起使用; 支持所有語言; 在您的企業或組織中輕鬆部署。

創建一個命令按鈕來複製和粘貼數據與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。 支持所有語言。 在您的企業或組織中輕鬆部署。 全功能30天免費試用。
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.
    Abi · 3 days ago
    This is fab - i've adapted for my s.sheet but I need it to be more dynamic and use labels rather than hard code the cell locations.
    I.e in the range of A2-A6 find the name 'Jamie' and then find the name in Column K2 and paste

    I also need each click to +1 and run the same copy and paste but to the next column i.e.

    click 1 - pastes into Q1, Click 2 - paste into Q2 etc

    Example:

    Column A Column B ...... Column K Column L
    1. Name Hrs worked Name Q1 Q2 Q3 Q4
    2. Jamie 22 Sammy <Paste here>
    3. Sammy 40 Judith <Paste here>
    4. Judith 18 Jamie <Paste here>
    5. Tammy 16 Keith <Paste here>
    6. Keith 42 Tammy <Paste here>


    Any help would be greatly appreciated.
  • To post as a guest, your comment is unpublished.
    Jonas · 13 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 · 27 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 · 18 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 · 5 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 · 5 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 · 6 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 · 7 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 · 8 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 · 8 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 · 10 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 · 11 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?