[英]“Subscript out of range” error when I divide 1 Sub into 2 Sub's in VBA-Excel
I wrote a VBA-SQL code in a "Module" which reads a table from a sheet, then it sends the table to server via SQL-code. 我在“模块”中编写了VBA-SQL代码,该模块从工作表中读取表,然后通过SQL代码将表发送到服务器。 I am saving each column as an array
result1(), result2(), result3()
, and Col1, Col2
are just column names in the server. 我将每一列保存为数组
result1(), result2(), result3()
,而Col1, Col2
只是服务器中的列名。 it
is index for the arrays (same index for all arrays) The VBA-SQL code is something like this. it
是数组的索引(所有数组的索引都相同)。VBA-SQL代码是这样的。 And this Sub
works perfectly: 这个
Sub
完美地工作:
Sub Datasend_Click()
Dim result1() As Variant, result2() As Variant, result3() As Variant
Dim Col1 As String, Col2 As String, Col3 As String
Dim it As Integer
Set ValidSheet = Worksheets("Sheet2")
Set DataRange = ValidSheet.Range("C22:C81")
it = 1
For Each dataa In DataRange
ReDim Preserve result1(it)
result1(it) = dataa.Value
it = it + 1
Next
Set DataRange = ValidSheet.Range("D22:D81")
it = 1
For Each dataa In DataRange
ReDim Preserve result2(it)
result2(it) = dataa.Value
it = it + 1
Next
For it = 1 To 60
SQL = "INSERT INTO PaymentPattern (" & Col1 & ", " & Col2 & ", " & Col3 & ") "
SQL = SQL & "VALUES (" & Str(result1(it)) & ", " & Str(result2(it)) & ", " & Str(result3(it)) & ")"
dbclass.ExecuteSQL SQL
Next it
End Sub
However, I want to separate this Sub
into 2 Sub's
instead. 但是,我想将此
Sub
分成2个Sub's
。 The first Sub
will read and save the arrays from the sheet (then I can use the arrays for other Sub's
), and the second Sub
will call the first Sub
and run the SQL-code to send the arrays to the server. 第一个
Sub
将从表中读取并保存数组(然后我可以将数组用于其他Sub's
数组),第二个Sub
将调用第一个Sub
并运行SQL代码以将数组发送到服务器。 I wrote 2 Sub's
like below, but I get an error like "Subscript out of range" for the line SQL = SQL & "VALUES (" & Str(result1(it)) & ", " & Str(result2(it)) & ", " & Str(result3(it)) & ")"
. 我像下面这样写了2个
Sub's
,但是我在行SQL = SQL & "VALUES (" & Str(result1(it)) & ", " & Str(result2(it)) & ", " & Str(result3(it)) & ")"
收到类似“下标超出范围”的错误SQL = SQL & "VALUES (" & Str(result1(it)) & ", " & Str(result2(it)) & ", " & Str(result3(it)) & ")"
。 When I debug it, it shows that it=1
as normal, but it shows "Subcript out of range" for the result1(it)
. 当我调试它时,它表明
it=1
正常,但是它对result1(it)
显示了“ Subcript out of range”。 Can you say please what is wrong? 你能说出什么问题吗?
Sub arrayread()
Dim result1() As Variant, result2() As Variant, result3() As Variant
Dim it As Integer
Set ValidSheet = Worksheets("Sheet2")
Set DataRange = ValidSheet.Range("C22:C81")
it = 1
For Each dataa In DataRange
ReDim Preserve result1(it)
result1(it) = dataa.Value
it = it + 1
Next
Set DataRange = ValidSheet.Range("D22:D81")
it = 1
For Each dataa In DataRange
ReDim Preserve result2(it)
result2(it) = dataa.Value
it = it + 1
Next
End Sub
Sub Datasend_Click()
Dim result1() As Variant, result2() As Variant, result3() As Variant
Dim it As Integer
Dim Col1 As String, Col2 As String, Col3 As String
arrayread
For it = 1 To 60
SQL = "INSERT INTO PaymentPattern (" & Col1 & ", " & Col2 & ", " & Col3 & ") "
SQL = SQL & "VALUES (" & Str(result1(it)) & ", " & Str(result2(it)) & ", " & Str(result3(it)) & ")"
dbclass.ExecuteSQL SQL
Next it
End Sub
It comes down to the scope of the arrays (where they are declared). 它归结为数组的范围(在声明它们的地方)。 You are declaring them separately in each sub, so
result1
in Datasend_Click knows nothing about result1
from arrayread
. 您要在每个子项中分别声明它们,因此Datasend_Click中的
result1
对arrayread的result1
arrayread
。 Therefore, result1
and result2
will be empty in the second sub which causes your Subscript out of range
error, given you have no elements yet you are trying to access it. 因此,鉴于您还没有试图访问它的元素,第二个
result2
result1
和result2
将为空,这将导致您的下Subscript out of range
错误。
Here is a Microsoft article outlining VBA variable scope: https://support.microsoft.com/en-us/kb/141693 . 这是一篇Microsoft文章,概述了VBA变量范围: https : //support.microsoft.com/zh-cn/kb/141693 。 Chip Pearson also has a good article on this subject: http://www.cpearson.com/excel/scope.aspx
Chip Pearson对此主题也有不错的文章: http : //www.cpearson.com/excel/scope.aspx
You should move the declaration of result1
and result2
to the top of your Module
(above the Sub
s) so they they have Class Level scope (available to all methods in the Module). 您应该将
result1
和result2
的声明移到Module
的顶部(在Sub
上方),以便它们具有Class Level范围(可用于Module中的所有方法)。
Dim result1() As Variant, result2() As Variant, result3() As Variant
Sub arrayread()
Dim it As Integer
Set ValidSheet = Worksheets("Sheet2")
Set DataRange = ValidSheet.Range("C22:C81")
it = 1
For Each dataa In DataRange
ReDim Preserve result1(it)
result1(it) = dataa.Value
it = it + 1
Next
Set DataRange = ValidSheet.Range("D22:D81")
it = 1
For Each dataa In DataRange
ReDim Preserve result2(it)
result2(it) = dataa.Value
it = it + 1
Next
End Sub
Sub Datasend_Click()
Dim it As Integer
Dim Col1 As String, Col2 As String, Col3 As String
arrayread
For it = 1 To 60
SQL = "INSERT INTO PaymentPattern (" & Col1 & ", " & Col2 & ", " & Col3 & ") "
SQL = SQL & "VALUES (" & Str(result1(it)) & ", " & Str(result2(it)) & ", " & Str(result3(it)) & ")"
dbclass.ExecuteSQL SQL
Next it
End Sub
My preferred solution would be to be passing the results in and out of the methods rather than having module-level scope, as this can cause spaghetti code issues down the track. 我的首选解决方案是将结果传入和传出方法,而不要具有模块级范围,因为这可能会导致意粉代码问题不断出现。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.