[英]When assigning a variable to an array, why am I getting an array subscript out of range error?
I am trying to loop through a ledger of transactions and tag costs that correlate to certain account codes.我正在尝试遍历与某些帐户代码相关的交易分类帐和标签成本。 For example, account code 123 would be tagged as a 'Hardware/Software' cost.例如,帐户代码 123 将被标记为“硬件/软件”成本。 I first produced code that combed through the ledger via reading each cell (a very lazy solution).我首先生成了通过读取每个单元格来梳理分类帐的代码(一个非常懒惰的解决方案)。 This process took ~12-17 minutes to tag all costs in the ledger.这个过程需要大约 12-17 分钟来标记分类帐中的所有成本。 I'm now trying to implement an array solution to tag all costs by reading the account codes through one array and then tag the costs in another array if it meets the requirements of an If/Then Statement.我现在正在尝试通过一个数组读取帐户代码来实现一个数组解决方案来标记所有成本,然后在另一个数组中标记成本(如果它满足 If/Then 语句的要求)。
The code below is looping through account codes and tagging 'misc' costs, 'hardware' costs, and 'not expense' costs.下面的代码循环遍历帐户代码并标记“杂项”成本、“硬件”成本和“非费用”成本。
How can I make the code work so that I can go through a series of If/Then statements with the account code to specify the tagging based on what the account code is?如何使代码工作,以便我可以使用帐户代码执行一系列 If/Then 语句,以根据帐户代码指定标记? I keep getting an error ("array subscript out of range") when I try to assign the 'Not Expense' tag in the second If/Then statement in the code below:当我尝试在下面代码的第二个 If/Then 语句中分配“Not Expense”标签时,我不断收到错误(“数组下标超出范围”):
Sub arrayTest()
Dim arTesting() As Variant
Dim arTag1(1 To 1550) As Variant 'this is just a test range
Dim arTag2(1 To 1550) As Variant 'this is just a test range
Dim rng, cell As Range
Dim HWSWTag, miscTag, notExpenseTag As String
Dim x As Integer
Set rng = Range("G2:G1551")
miscTag = "Misc"
HWSWTag = "HW/SW"
notExpenseTag = "Not Expense"
x = 1
'Read in the range of account codes
For Each cell In rng
ReDim Preserve arTesting(x)
arTesting(x) = cell.Value
x = x + 1
Next cell
'Now tag the costs to arTag1 and arTag2
Dim i As Long
i = 1
For i = LBound(arTesting) To UBound(arTesting)
If arTesting(i) = 716 Then
arTag1(i) = miscTag
arTag2(i) = HWSWTag
End If
If arTesting(i) = 182 Or 160 Or 250 Or 258 Or 180 Then
arTag1(i) = notExpenseTag 'This is where I get the error
End If
'Debug.Print arTesting(i)
Next i
'Now paste the tags into the worksheet
Range("AL2:AL1551").Value = WorksheetFunction.Transpose(arTag1)
Range("AM2:AM1551").Value = WorksheetFunction.Transpose(arTag2)
End Sub
I expect the output to tag all costs with account code '716' as 'misc' and 'HW/SW', and tag costs with account code '182', '160', '250', '258', '180' as 'Not Expense'我希望输出将使用帐户代码“716”的所有成本标记为“misc”和“HW/SW”,并使用帐户代码“182”、“160”、“250”、“258”、“180”标记成本作为“非费用”
I hope this code helps as it is a small part of the overall code that combs through a bunch of other account codes.我希望这段代码能有所帮助,因为它只是梳理了一堆其他帐户代码的整体代码的一小部分。
The following should do what you seem to be trying to do.以下应该做你似乎想要做的事情。 It makes several changes:它做了几个改变:
Select Case
rather than an If
statement with a condition that doesn't mean what you probably thing.它使用Select Case
而不是If
语句,其条件并不意味着您可能的意思。 x = 1 Or 2 Or 3
means (x = 1) Or 2 Or 3
(which is almost never what you want) rather than the intended x = 1 Or x = 2 Or x = 3
x = 1 Or 2 Or 3
表示(x = 1) Or 2 Or 3
(这几乎不是您想要的)而不是预期的x = 1 Or x = 2 Or x = 3
Here is the code:这是代码:
Sub arrayTest()
Dim arTesting() As Variant
Dim arTag1(1 To 1550, 1 To 1) As Variant 'this is just a test range
Dim arTag2(1 To 1550, 1 To 1) As Variant 'this is just a test range
Dim rng As Range, cell As Range
Dim HWSWTag As String, miscTag As String, notExpenseTag As String
Dim i As Long
Set rng = Range("G2:G1551")
miscTag = "Misc"
HWSWTag = "HW/SW"
notExpenseTag = "Not Expense"
arTesting = rng.Value
For i = LBound(arTesting,1) To UBound(arTesting,1)
Select Case arTesting(i,1)
Case 716:
arTag1(i, 1) = miscTag
arTag2(i, 1) = HWSWTag
Case 182, 160, 250, 258, 180:
arTag1(i, 1) = notExpenseTag
End Select
Next i
Range("AL2:AL1551").Value = arTag1
Range("AM2:AM1551").Value = arTag2
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.