简体   繁体   English

Excel 宏在分配给按钮时按预期停止运行

[英]Excel Macro stops running as intended when assigned to a button

I have the below Macro.我有下面的宏。 When this runs it essentially copy and pastes ranges on the 'Master Sheet' to elsewhere on the sheet where needed:当它运行时,它基本上将“主表”上的范围复制并粘贴到工作表上需要的其他地方:

Sub Macro2()
With Worksheets("Master Sheet")
Range("CC25:CE33").Select
Selection.Copy
Range("CC44").Select
Selection.PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:= _
    xlNone, SkipBlanks:=False, Transpose:=False
Range("CC21").Select
Selection.Copy
Range("CC40").Select
Selection.PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:= _
    xlNone, SkipBlanks:=False, Transpose:=False
Range("CC6:CE14").Select
Selection.Copy
Range("CC25").Select
Selection.PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:= _
    xlNone, SkipBlanks:=False, Transpose:=False
Range("CC2").Select
Selection.Copy
Range("CC21").Select
Selection.PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:= _
    xlNone, SkipBlanks:=False, Transpose:=False
End With 
End Sub 

The macro runs how intended.宏按预期运行。 I want to assign it to a button on a different sheet ('Summary_QC').我想将它分配给不同工作表上的按钮(“Summary_QC”)。 When I do, the Macro is ran but for some reason it uses ranges on the 'Summary_QC' sheet rather than the 'Master Sheet', despite me stating With Worksheets("Master Sheet").当我这样做时,宏会运行但由于某种原因它使用“Summary_QC”工作表而不是“主工作表”上的范围,尽管我说明了 With Worksheets(“主工作表”)。

Any ideas what my problem may be, and possible solutions?任何想法我的问题可能是什么,以及可能的解决方案?

To add the button;添加按钮;

  1. Go to the sheet you want to put it in Go 到你要放入的表格
  2. On the top go to Developer tab and click on Insert under the Controls在顶部 go 到 Developer 选项卡,然后单击 Controls 下的 Insert
  3. Select Button from Form Control, it will popup Macro selection Select Form Control的按钮,会弹出宏选择
  4. Select Macro2 Select 宏2

Sub Macro2()

Application.ScreenUpdating = False

Dim wb As Workbook
Dim wsMaster As Worksheet, wsCopyTo As Worksheet

Set wsMaster = wb.Sheets("Master Sheet")

wsMaster.Activate

wsMaster.Range("CC25:CE33").Copy
wsCopyTo.Range("CC44").Select
Selection.PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:= _
    xlNone, SkipBlanks:=False, Transpose:=False

wsMaster.Range("CC21").Copy
wsCopyTo.Range("CC40").Select
Selection.PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:= _
    xlNone, SkipBlanks:=False, Transpose:=False
    
wsMaster.Range("CC6:CE14").Copy
wsCopyTo.Range("CC25").Select
Selection.PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:= _
    xlNone, SkipBlanks:=False, Transpose:=False

wsMaster.Range("CC2").Copy
wsCopyTo.Range("CC21").Select
Selection.PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:= _
    xlNone, SkipBlanks:=False, Transpose:=False

wb.Sheets("Summary_QC").Activate

Application.ScreenUpdating = True
End Sub

add this at the beginning of the code before (with): Worksheets("Master Sheet").activate将此添加到代码的开头(使用):Worksheets("Master Sheet").activate

and add this at the end of the code before (end sub): Worksheets("Summary_QC").activate并在 (end sub) 之前的代码末尾添加:Worksheets("Summary_QC").activate

Using the With Statement使用 With 语句

  • ThisWorkbook refers to the workbook containing this code. ThisWorkbook引用包含此代码的工作簿。
  • Note the dots at the beginning of each line in the With statement: they are 'telling' VBA that the ranges are located on the Master Sheet worksheet.请注意With语句中每一行开头的点:它们“告诉”VBA 这些范围位于Master Sheet工作表上。
  • You don't have to select anything.你不需要select任何东西。
  • The removed PasteSpecial parameters were the default values so you don't need them.删除的PasteSpecial参数是默认值,因此您不需要它们。
Sub Macro2()
    With ThisWorkbook.Sheets("Master Sheet")
        .Range("CC25:CE33").Copy
        .Range("CC44").PasteSpecial xlPasteValuesAndNumberFormats
        .Range("CC21").Copy
        .Range("CC40").PasteSpecial xlPasteValuesAndNumberFormats
        .Range("CC6:CE14").Copy
        .Range("CC25").PasteSpecial xlPasteValuesAndNumberFormats
        .Range("CC2").Copy
        .Range("CC21").PasteSpecial xlPasteValuesAndNumberFormats
    End With
    Application.CutCopyMode = False
End Sub

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM