0

我对以下代码有疑问:

我的主要代码是“autofill_DSR”,我试图调用的子程序是“算法”。“autofill_DSR”在模块 1 中,“算法”在模块 4 中。之前,我没有将这两个代码分开,我只是在我写的那一行“算法”中有大部分内容:调用 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
4

1 回答 1

1

更改Sub algorithm()Sub algorithm(n as long)然后使用

Call Module4.algorithm(n)
于 2013-07-23T14:52:31.367 回答