如何修复excelvba中的命令点击代码

如何修复excelvba中的命令点击代码,excel,vba,Excel,Vba,首先,我是vba的新手,我找到了我需要的代码,但当我尝试执行时,出现了一个错误“Expected End Sub” 我无法解决问题所在 我在行尾添加了“End Sub”(我想应该是两个End Sub,一个是“CommandButton1\u Click”,一个是“Copy\u with\u autofilter” 但是没有运气 Private Sub CommandButton1_Click() Sub Copy_With_AutoFilter1() 'Note: This macro use

首先,我是vba的新手,我找到了我需要的代码,但当我尝试执行时,出现了一个错误“Expected End Sub”

我无法解决问题所在

我在行尾添加了“End Sub”(我想应该是两个End Sub,一个是“CommandButton1\u Click”,一个是“Copy\u with\u autofilter”

但是没有运气

Private Sub CommandButton1_Click()

Sub Copy_With_AutoFilter1()
'Note: This macro use the function LastRow
    Dim My_Range As Range
    Dim CalcMode As Long
    Dim ViewMode As Long
    Dim FilterCriteria As String
    Dim CCount As Long
    Dim WSNew As Worksheet
    Dim sheetName As String
    Dim rng As Range

    'Set filter range on ActiveSheet: A1 is the top left cell of your filter range
    'and the header of the first column, D is the last column in the filter range.
    'You can also add the sheet name to the code like this :
    'Worksheets("Sheet1").Range("A1:D" & LastRow(Worksheets("Sheet1")))
    'No need that the sheet is active then when you run the macro when you use this.
    Set My_Range = Worksheets("Data").Range("A2:AP" & LastRow(Worksheets("Data")))
    My_Range.Parent.Select

    If ActiveWorkbook.ProtectStructure = True Or _
       My_Range.Parent.ProtectContents = True Then
        MsgBox "Sorry, not working when the workbook or worksheet is protected", _
               vbOKOnly, "Copy to new worksheet"
        Exit Sub
    End If

    'Change ScreenUpdating, Calculation, EnableEvents, ....
    With Application
        CalcMode = .Calculation
        .Calculation = xlCalculationManual
        .ScreenUpdating = False
        .EnableEvents = False
    End With
    ViewMode = ActiveWindow.View
    ActiveWindow.View = xlNormalView
    ActiveSheet.DisplayPageBreaks = False

    'Firstly, remove the AutoFilter
    My_Range.Parent.AutoFilterMode = False

    'Filter and set the filter field and the filter criteria :
    'This example filter on the first column in the range (change the field if needed)
    'In this case the range starts in A so Field 1 is column A, 2 = column B, ......
    'Use "<>Netherlands" as criteria if you want the opposite
    My_Range.AutoFilter Field:=2, Criteria1:="=Akut-1"

    'If you want to filter on a cell value you can use this, use "<>" for the opposite
    'This example uses the activecell value
    'My_Range.AutoFilter Field:=1, Criteria1:="=" & ActiveCell.Value

    'This will use the cell value from A2 as criteria
    'My_Range.AutoFilter Field:=1, Criteria1:="=" & Range("A2").Value

    ''If you want to filter on a Inputbox value use this
    'FilterCriteria = InputBox("What text do you want to filter on?", _
     '                              "Enter the filter item.")
    'My_Range.AutoFilter Field:=1, Criteria1:="=" & FilterCriteria

    'Check if there are not more then 8192 areas(limit of areas that Excel can copy)
    CCount = 0
    On Error Resume Next
    CCount = My_Range.Columns(1).SpecialCells(xlCellTypeVisible).Areas(1).Cells.Count
    On Error GoTo 0
    If CCount = 0 Then
        MsgBox "There are more than 8192 areas:" _
             & vbNewLine & "It is not possible to copy the visible data." _
             & vbNewLine & "Tip: Sort your data before you use this macro.", _
               vbOKOnly, "Copy to worksheet"
    Else
        'Add a new Worksheet
        Set WSNew = Worksheets.Add(After:=Sheets(ActiveSheet.Index))

        'Ask for the Worksheet name
        sheetName = "Akut-1"

        On Error Resume Next
        WSNew.Name = sheetName
        If Err.Number > 0 Then
            MsgBox "Change the name of sheet : " & WSNew.Name & _
                 " manually after the macro is ready. The sheet name" & _
                 " you fill in already exists or you use characters" & _
                 " that are not allowed in a sheet name."
            Err.Clear
        End If
        On Error GoTo 0

        'Copy/paste the visible data to the new worksheet
        My_Range.Parent.AutoFilter.Range.Copy
        With WSNew.Range("A2")
            ' Paste:=8 will copy the columnwidth in Excel 2000 and higher
            ' Remove this line if you use Excel 97
            .PasteSpecial Paste:=8
            .PasteSpecial xlPasteValues
            .PasteSpecial xlPasteFormats
            Application.CutCopyMode = False
            .Select
        End With

        ' If you want to delete the rows that you copy, also use this
        ' With My_Range.Parent.AutoFilter.Range
        '     On Error Resume Next
        '     Set rng = .Offset(1, 0).Resize(.Rows.Count - 1, .Columns.Count) _
        '               .SpecialCells(xlCellTypeVisible)
        '     On Error GoTo 0
        '     If Not rng Is Nothing Then rng.EntireRow.Delete
        ' End With

    End If

    'Close AutoFilter
    My_Range.Parent.AutoFilterMode = False

    'Restore ScreenUpdating, Calculation, EnableEvents, ....
    My_Range.Parent.Select
    ActiveWindow.View = ViewMode
    If Not WSNew Is Nothing Then WSNew.Select
    With Application
        .ScreenUpdating = True
        .EnableEvents = True
        .Calculation = CalcMode
    End With

End Sub


Function LastRow(sh As Worksheet)
    On Error Resume Next
    LastRow = sh.Cells.Find(What:="*", _
                            After:=sh.Range("A2"), _
                            Lookat:=xlPart, _
                            LookIn:=xlValues, _
                            SearchOrder:=xlByRows, _
                            SearchDirection:=xlPrevious, _
                            MatchCase:=False).Row
    On Error GoTo 0
End Function

End Function
Private子命令按钮1\u单击()
带有自动过滤器1()的子副本
'注意:此宏使用函数LastRow
将我的_范围变暗为范围
暗淡的CalcMode与长
将视图模式变暗为长
作为字符串的Dim FilterCriteria
长帐
新建为工作表
将sheetName设置为字符串
变暗rng As范围
'在ActiveSheet上设置筛选范围:A1是筛选范围的左上角单元格
'并且第一列的标题D是筛选范围中的最后一列。
'您还可以将图纸名称添加到代码中,如下所示:
'工作表(“表1”)。范围(“A1:D”和最后一行(工作表(“表1”))
'使用此选项时,在运行宏时,无需工作表处于活动状态。
设置我的范围=工作表(“数据”)。范围(“A2:AP”和最后一行(工作表(“数据”))
My_Range.Parent.Select
如果ActiveWorkbook.ProtectStructure=True或_
My_Range.Parent.ProtectContents=则为True
MsgBox“抱歉,工作簿或工作表受保护时不工作”_
vbOKOnly,“复制到新工作表”
出口接头
如果结束
'更改屏幕更新、计算、启用事件。。。。
应用
CalcMode=.Calculation
.Calculation=xlCalculationManual
.ScreenUpdate=False
.EnableEvents=False
以
ViewMode=ActiveWindow.View
ActiveWindow.View=xlNormalView
ActiveSheet.DisplayPageBreaks=False
'首先,卸下自动过滤器
My_Range.Parent.AutoFilterMode=False
'筛选并设置筛选字段和筛选条件:
'此示例筛选器位于范围中的第一列(如果需要,请更改字段)
'在这种情况下,范围从so字段开始,1是A列,2=B列。。。。。。
如果您想要相反的结果,请使用“荷兰”作为标准
My_Range.AutoFilter字段:=2,准则1:=“=Akut-1”
'如果要对单元格值进行筛选,可以使用此选项,使用“”作为相反的选项
'此示例使用activecell值
'My_Range.AutoFilter字段:=1,Criteria1:=“=”&ActiveCell.Value
'这将使用A2中的单元格值作为标准
'My_Range.AutoFilter字段:=1,Criteria1:=“=”&范围(“A2”).值
''如果要对Inputbox值进行筛选,请使用此
'FilterCriteria=InputBox(“您想筛选什么文本?”_
““输入筛选项。”)
'My_Range.AutoFilter字段:=1,准则1:=“=”&筛选准则
'检查是否有超过8192个区域(Excel可以复制的区域限制)
帐户=0
出错时继续下一步
CCount=My_Range.Columns(1).SpecialCells(xlCellTypeVisible).Areas(1).Cells.Count
错误转到0
如果CCount=0,则
MsgBox“有超过8192个区域:”_
&vbNewLine&“无法复制可见数据。”_
&vbNewLine&“提示:在使用此宏之前对数据进行排序。”_
vbOKOnly,“复制到工作表”
其他的
'添加新工作表
设置WSNew=Worksheets.Add(之后:=Sheets(ActiveSheet.Index))
'询问工作表名称
sheetName=“Akut-1”
出错时继续下一步
WSNew.Name=sheetName
如果错误编号>0,则
MsgBox“更改工作表名称:&WSNew.name&_
“在宏准备就绪后手动。工作表名称”&_
“您填写的内容已经存在,或者您使用了字符”&_
“工作表名称中不允许的。”
呃,明白了
如果结束
错误转到0
'将可见数据复制/粘贴到新工作表
My_Range.Parent.AutoFilter.Range.Copy
使用WSNew.Range(“A2”)
'粘贴:=8将在Excel 2000及更高版本中复制列宽
'如果使用Excel 97,请删除此行
.Paste特殊粘贴:=8
.Paste特殊XLPaste值
.Paste特殊XLPaste格式
Application.CutCopyMode=False
.选择
以
'如果要删除复制的行,也可以使用此
'使用My_Range.Parent.AutoFilter.Range
'出现错误时,请继续下一步
'设置rng=.Offset(1,0).调整大小(.Rows.Count-1、.Columns.Count)_
'.SpecialCells(xlCellTypeVisible)
'在出现错误时转到0
'如果不是,则rng为Nothing,然后rng.EntireRow.Delete
"以
如果结束
'关闭自动过滤器
My_Range.Parent.AutoFilterMode=False
'还原屏幕更新、计算、启用事件。。。。
My_Range.Parent.Select
ActiveWindow.View=ViewMode
如果不是WSNew,则WSNew为Nothing。选择
应用
.ScreenUpdate=True
.EnableEvents=True
.Calculation=CalcMode
以
端接头
函数LastRow(sh作为工作表)
出错时继续下一步
LastRow=sh.Cells.Find(内容:=“*”_
之后:=sh.Range(“A2”)_
看:=xlPart_
LookIn:=xlValues_
搜索顺序:=xlByRows_
搜索方向:=xlPrevious_
MatchCase:=False)。行
错误转到0
端函数
端函数
当我点击按钮时,它会过滤“数据”表并复制另一张表。
因此,我可以使用过滤表计算单元格的公式。

您试图将子对象放入子对象中,但这不能像@Tom在注释中所述那样完成。如果您希望通过单击命令按钮来运行此子对象,可以通过两种方式完成。或者您可以像这样删除第二个子对象:

Private Sub CommandButton1_Click()

'Note: This macro use the function LastRow
    Dim My_Range As Range
    Dim CalcMode As Long
    Dim ViewMode As Long
etc...
或者你让你的同事
Private Sub CommandButton1_Click()
Copy_With_AutoFilter1
End sub

Sub Copy_With_AutoFilter1()
'Note: This macro use the function LastRow
    Dim My_Range As Range
    Dim CalcMode As Long
    Dim ViewMode As Long
etc...