簡體   English   中英

Excel VBA:錯誤調用子例程

[英]Excel VBA: Error Calling Subroutine

我的以下代碼有問題:

我的主要代碼是“ autofill_DSR”,我要調用的子例程是“ algorithm”。 “ autofill_DSR”在Module1中,“ algorithm”在Module4中。 以前,我沒有將這兩個代碼分開,而在我寫的那一行代碼中,我只有“算法”中的大部分內容:調用Module4.algorithm,程序執行了我想要的操作。

創建此子例程后,它僅經歷代碼的一次迭代,但是,因為它執行一次該子例程,但是它不返回,或者for循環中的迭代存在問題。 我不知道。

我使用“ Sheet2”激活命令是因為進入子程序時我在工作表之間進行切換,這可能與它有關,這可能是公共/私有變量聲明嗎?

任何幫助表示贊賞,謝謝。


Sub autofill_DSR()

' Variable Declarations:

Dim x_count As Integer
Dim n As Integer
Dim item_a As String
Dim item_b As String
Dim test_string As String

' Variable Initializations:

test_string = "NN"
x_count = 0
Process_Control_NumRows = 16

' Main Data Transfer Code:

Sheets(Array("Sheet1", "Sheet2")).Select        'Create Array of all Sheets

' Process Control Sheet:

    For n = 0 To (Process_Control_NumRows - 1)  'Cycle 16 times for each
                                                'item in process controls tab
        Sheets("Sheet2").Activate       'Choose specific sheet
        Range("D1").Select              'Choose specific cell

        Call Module4.algorithm      'Call on subroutine (see algorithm code)

    Next n                  'increment index to account for offset

End Sub

Sub algorithm()

        'If an "x" or "X" is marked in the "Yes" column,
        'at descending cells down the column offset by the for loop index, n

        If (ActiveCell.Offset(n, 0) = "x" Or ActiveCell.Offset(n, 0) = "X") Then

            item_a = ActiveCell.Offset(n, -3).Value     ' Store Letter value
            item_b = ActiveCell.Offset(n, -2).Value     ' Store number value
            x_count = x_count + 1                       ' increment the total x count

            If (x_count > 5) Then

                Sheets("Sheet3").Activate               ' Switch over to Sheet 1
                Range("A1").Select                      ' Choose "Item" column, first cell
                ActiveCell.Offset((x_count - 6), 0).Value = (item_a & item_b)

                'Insert cocatenated value of item_a and item_b
                '(for example "A" & "1" = "A1")
                'at the cells under the "Item" column, indexed by x_count

            Else

                Sheets("Sheet1").Activate
                Range("A1").Select
                ActiveCell.Offset((x_count - 1), 0).Value = (item_a & item_b)

            End If

        End If

End Sub

Sub algorithm()更改為Sub algorithm(n as long) ,然后使用進行調用

Call Module4.algorithm(n)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM