[英]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.