2013-07-23 52 views
0

我有下面的代码有问题:Excel的VBA:错误调用子程序

我的主要代码是“autofill_DSR”,我试图调用子程序是“算法”。 “autofill_DSR”是在模块1,“算法”是单词数。 之前,我并没有将二者分开代码和我刚做了什么的“算法” 在那里我写的行批量:呼叫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 

回答

1

变化Sub algorithm()Sub algorithm(n as long),然后用

Call Module4.algorithm(n) 
+0

呀称呼它,是工作,我也只好包括x_count,谢谢。 – user2608147