Excel VBA呼叫功能(主要是多个SUB):)

时间:2018-07-06 07:55:31

标签: excel vba excel-vba

我正在尝试为我的任务创建一个vba。但是我担心它不会起作用,只会返回“过程太大”消息。因此,在完成之前(我仅会执行50个步骤中的4个),我想知道是否有一种方法可以划分我的工作,例如使用“调用”功能。

但是我的问题是,我使用了“ FOR-NEXT”功能和“ IF-ELSE”功能。这是我的代码的一部分。

For R = 10 To LROW

If ws.Cells(R, 2).Value = "ASIA UNITED BANK" And ws.Cells(R, 3).Value = "C/A# 001-01-002838-0" Then
    ws1.Range("A1").AutoFilter Field:=1, Criteria1:=Years & "-" & Months & "-" & Days
    ADD = WorksheetFunction.Sum(ws1.Range("E2:E1500").SpecialCells(xlCellTypeVisible))
    ws.Cells(R, 5).Value = ADD
    ws1.Cells.AutoFilter

    ws1.Range("A1").AutoFilter Field:=1, Criteria1:=Years & "-" & Months & "-" & Days
    MIN = WorksheetFunction.Sum(ws1.Range("D2:D1500").SpecialCells(xlCellTypeVisible))
    ws.Cells(R, 11).Value = MIN
    ws1.Cells.AutoFilter

ElseIf ws.Cells(R, 2).Value = "ASIA UNITED BANK" And ws.Cells(R, 3).Value = "C/A# 001-01-003813-3" Then
    ws2.Range("A1").AutoFilter Field:=1, Criteria1:=Years & "-" & Months & "-" & Days
    ADD = WorksheetFunction.Sum(ws2.Range("E2:E1500").SpecialCells(xlCellTypeVisible))
    ws.Cells(R, 5).Value = ADD
    ws2.Cells.AutoFilter

    ws2.Range("A1").AutoFilter Field:=1, Criteria1:=Years & "-" & Months & "-" & Days
    MIN = WorksheetFunction.Sum(ws2.Range("D2:D1500").SpecialCells(xlCellTypeVisible))
    ws.Cells(R, 11).Value = MIN
    ws2.Cells.AutoFilter

如您所见,我使用变量“ R”来寻址行号。我将再使用近50个“ else If”功能。

我想拥有一个“ MAIN”功能,然后放置“ FOR-NEXT”功能,然后放置“ IF-ELSE”,但是包含“ IF-ELSE”功能的唯一过程是“ CALL”功能。

但是变量“ R”将不再起作用。而且我不知道如何将“ IF ELSE”功能中的“ R”匹配到下一个SUB。

希望有人能理解这篇长文章。谢谢!

1 个答案:

答案 0 :(得分:0)

您实际上不需要过滤-您可以使用SUMIF

以下示例中的ReturnMyNumbers函数根据E列中的日期添加KA列中的数字。然后,这两个数字都返回到调用过程。
我已经在函数参数中包括了银行名称 identifier -我不确定在确定要从哪个表中添加时如何使用它们,所以我想d留在那儿。

Test过程是您在ws上逐行浏览的地方,在每次循环迭代时将参数传递给函数。

Public Sub Test()

    Dim MyResults As Variant

    With ThisWorkbook

        'Can put results directly into cells.
        .Worksheets("Sheet1").Range("A1:B1") = ReturnMyNumbers( _
            Bank:="Asia United Bank", _
            Account:="C/A# 001-01-002838-0", _
            AccountSheet:=.Worksheets("Sheet2"), _
            ActiveDate:=DateSerial(2018, 9, 13))

        'Or pass results back to variable.
        MyResults = ReturnMyNumbers("Asia United Bank", "C/A# 001-01-002838-0", .Worksheets("Sheet3"), DateSerial(2019, 4, 15))
        'And then paste into cells.
        .Worksheets("Sheet1").Range("A2:B2") = MyResults
        'Or use the two results separately.
        MsgBox MyResults(0) & " and " & MyResults(1)

    End With

End Sub

Public Function ReturnMyNumbers(Bank As String, Account As String, _
    AccountSheet As Worksheet, ActiveDate As Date) As Variant

    Dim rLastCell As Range
    Dim Add As Double
    Dim Min As Double

    With AccountSheet
        Set rLastCell = .Cells(.Rows.Count, 1).End(xlUp)
        Add = WorksheetFunction.SumIf(.Range("A1", rLastCell), ActiveDate, .Range(.Cells(1, 5), rLastCell.Offset(, 5)))
        Min = WorksheetFunction.SumIf(.Range("A1", rLastCell), ActiveDate, .Range(.Cells(1, 11), rLastCell.Offset(, 11)))
    End With

    ReturnMyNumbers = Array(Add, Min)

End Function