Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何组合两个vba代码?_Vba_Excel - Fatal编程技术网

如何组合两个vba代码?

如何组合两个vba代码?,vba,excel,Vba,Excel,我想在一张表中组合(运行)两个不同的vba函数。怎么做 Option Explicit Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean) 'Updateby Extendoffice 20161123 Dim rgTable As Range Dim rgData As Range Dim xColumn As Integer On Error Res

我想在一张表中组合(运行)两个不同的vba函数。怎么做

Option Explicit
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
'Updateby Extendoffice 20161123
    Dim rgTable As Range
    Dim rgData As Range
    Dim xColumn As Integer
    On Error Resume Next
    Application.ScreenUpdating = False
    Set rgTable = Range("mydata")
    With rgTable
        Set rgData = .Offset(1, 0).Resize(.Rows.Count - 1, .Columns.Count)
        If Not Application.Intersect(ActiveCell, rgData.Cells) Is Nothing Then
            xColumn = ActiveCell.Column - .Column + 1
            If ActiveSheet.AutoFilterMode = False Then
                .AutoFilter
            End If
            If ActiveSheet.AutoFilter.Filters(xColumn).On = True Then
                .AutoFilter Field:=xColumn
            Else
                .AutoFilter Field:=xColumn, Criteria1:=ActiveCell.Value
            End If
        End If
    End With
    Set rgData = Nothing
    Set rgTable = Nothing
    Application.ScreenUpdating = True
End Sub

资料来源:

这就是如何将两种功能融为一体的方法。假设您有
Code1
Code2
,并且您希望一起构建
Code1和Code2

Public Sub Code1()
    Debug.Print "I am code 1"
End Sub

Public Sub Code2()
    Debug.Print "I am code 2"
End Sub
您应该做的是检查这些代码正在做什么,并将它们结合在一起。一般来说,它应该是这样的:

Public Sub Code1AndCode2Together()
    Debug.Print "I am code 1"
    Debug.Print "I am code 2"
End Sub
Public Sub Code1AndCode2Together()
    Code1
    Code2
End Sub
或者像这样:

Public Sub Code1AndCode2Together()
    Debug.Print "I am code 1"
    Debug.Print "I am code 2"
End Sub
Public Sub Code1AndCode2Together()
    Code1
    Code2
End Sub

在您的情况下,简单地复制和粘贴可能不起作用,但这是一个良好的开端。然后试着理解这些代码在做什么,并试着实现它。

到目前为止,您做了哪些尝试?请把你的密码寄出去。