首页 > 解决方案 > VBA将一个子的范围传递给另一个子

问题描述

我想将范围:将 Sub FoundVisit() 的 cell = bd.Range(FoundColumn & StartRow & ":" & FoundColumn & LastRow) 设置为另一个 Sub SumVisit(),然后对所有访问求和。


Sub FoundVisit(StartRow As Long, LastRow As Long, FoundColumn As String, StringToFind As String, ResultRange As Range, cell As Range)

    Dim bd As Worksheet: Set bd = Sheets("BDD")
    Dim rf As Worksheet: Set rf = Sheets("REF")

    StringToFind = rf.Range("D2").Value

    Set ResultRange = bd.Cells.Find(What:=StringToFind, LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByColumns)

    If ResultRange Is Nothing Then
        MsgBox "Nothing"
    Else
        'Get the row after the header
        StartRow = ResultRange.Row + 1
        'Column of the header
        FoundColumn = Split(Cells(, ResultRange.Column).Address, "$")(1)
        'Last row under that header
        LastRow = bd.Range(FoundColumn & bd.Rows.Count).End(xlUp).Row
        'The range that I need
        Set cell = bd.Range(FoundColumn & StartRow & ":" & FoundColumn & LastRow)

    End If

End Sub

Sub SumVisit()

    With Sheets("SUMMARY")

    Sheets("SUMMARY").Label1 = Application.WorksheetFunction.Sum(FoundVisit)

    End With

End Sub

标签: vbasubroutine

解决方案


推荐阅读