【发布时间】:2012-10-21 05:03:34
【问题描述】:
我在将变量从一个子程序传递到另一个子程序时遇到问题。我已将它们宣布为公开,但它似乎不起作用。它说子程序超出范围。我所需要的只是在我的第二个子例程中使用 varUnique(一个数组)和 firstIndex。我需要做什么才能做到这一点?
Public fistIndex As Integer
Public varUnique As Variant
Sub FindUnique()
Dim varIn As Variant
Dim iInCol As Long
Dim iInRow As Long
Dim iUnique As Long
Dim nUnique As Long
Dim isUnique As Boolean
Dim lastIndex As Integer
varIn = Range("List")
ReDim varUnique(1 To UBound(varIn, 1) * UBound(varIn, 2))
nUnique = 0
For iInRow = LBound(varIn, 1) To UBound(varIn, 1)
For iInCol = LBound(varIn, 2) To UBound(varIn, 2)
isUnique = True
For iUnique = 1 To nUnique
If varIn(iInRow, iInCol) = varUnique(iUnique) Then
isUnique = False
Exit For
End If
Next iUnique
If isUnique = True Then
nUnique = nUnique + 1
varUnique(nUnique) = varIn(iInRow, iInCol)
End If
Next iInCol
Next iInRow
'// varUnique now contains only the unique values.
'// Trim off the empty elements:
ReDim Preserve varUnique(1 To nUnique)
firstIndex = LBound(varUnique)
lastIndex = UBound(varUnique)
create:
If Not varUnique(firstIndex) = "Sub-Total" Then
Worksheets.Add.Name = varUnique(firstIndex)
Call Ledge(varUnique, firstIndex)
Else
End
End If
If Not firstIndex = lastIndex Then
firstIndex = firstIndex + 1
ActiveCell.Offset(1, 0).Select
GoTo create
Else
End If
End Sub
Sub Ledge(varUnique, firstIndex)
'
'
'Define Variables
Dim Account_type As String
Dim Debit As Long
Dim Credit As Long
'Select Journal and Cell B4
Sheets("Journal").Select
Range("B4").Select
Account_Search:
'Make that cell= account_type
Account_type = ActiveCell.Value
'If that cell= cash then save the values adjecent
If Account_type = varUnique(firstIndex) Then
ActiveCell.Offset(0, 1).Select
Debit = ActiveCell.Value
ActiveCell.Offset(0, 1).Select
Credit = ActiveCell.Value
'Then go back to where you began
ActiveCell.Offset(0, -2).Select
'Select Cash and Cell A2
Sheets(varUnique(firstIndex)).Select
Range("A2").Select
Search:
'If both cells are empy
If ActiveCell.Value = "" And ActiveCell.Offset(0, 1).Value = "" Then
'Then write values and indicate you have done so
ActiveCell.Value = Debit
ActiveCell.Offset(0, 1).Select
ActiveCell.Value = Credit
Else
'If they are not empty go down one cell and search again
ActiveCell.Offset(1, 0).Select
GoTo Search
End If
'Once it is recorded go to Journal again and go down one more cell
Sheets("Journal").Select
ActiveCell.Offset(1, 0).Select
'If it wasn't cash then go down one
Else
ActiveCell.Offset(1, 0).Select
End If
'Record that cell value and check to see if it is not sub-total
Account_type = ActiveCell.Value
If Not Account_type = "Sub-Total" Then
'Now see if it is cash
GoTo Account_Search
Else
End If
End Sub
【问题讨论】:
-
a) 使用函数返回值,而不是在
Sub中设置全局变量。好的代码没有全局变量。 b) 也许你应该在任何地方都称你为变量firstIndex,而不是有时称它为fistIndex。使用Option Explicit看看会发生什么。事实上,你真的应该在你编写的所有 VBA 代码中使用它。 -
@Tomalak 告诉人们好的代码没有全局变量是没有帮助的。或者是真的。取出全局变量并不会使坏代码变好,而放入全局变量并不会使好代码变坏。另外,您对
Option Explicit的看法是完全正确的。 -
@Seith 是的,关于全局变量的部分有点争议。无论如何,我认为避免将状态保存在全局变量中是一个好习惯,尤其是作为初学者,尤其是在上述情况下。对所有事情都使用全局变量太简单了,太诱人了,而且没有什么好处。
-
@Tomalak 我不认为这是争论。全局变量是一种副作用,而副作用通常被认为是一件坏事。也许将信息软化为“避免使用全局变量来存储状态;更喜欢将参数显式传递给函数”?
-
>> “它说子程序超出范围。” ???你确定这不是“下标超出范围”吗?它发生在哪一行?
标签: vba excel subroutine