当我在VBA-Excel中将1 Sub划分为2 Sub时出现“下标超出范围”错误
我在“模块”中写了一个VBA-SQL代码,它从表格中读取表格,然后通过SQL代码将表格发送到服务器。 我将每列保存为数组result1(), result2(), result3()
和Col1, Col2
只是服务器中的列名。 it
是数组的索引(所有数组的索引都是相同的)VBA-SQL代码就像这样。 而这个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
不过,我想把这个Sub
分解成2个Sub's
。 第一个Sub
将读取并保存表单中的数组(然后我可以使用其他Sub's
数组),第二个Sub
将调用第一个Sub
并运行SQL代码将数组发送到服务器。 我写了如下所示的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)) & ")"
。 当我debugging它时,它显示it=1
正常,但是它显示结果result1(it)
“下标超出范围”。 你能说出什么问题吗?
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
它归结于数组的范围(它们被声明的地方)。 你在每个子文件中分别声明它们,所以Datasend_Click中的result1
对arrayread中的result1
arrayread
。 因此, result1
和result2
将在第二个子项中为空,导致您的下Subscript out of range
错误,因为您没有元素,但您正试图访问它。
以下是一篇概述VBAvariables作用域的Microsoft文章: https : //support.microsoft.com/en-us/kb/141693 。 Chip Pearson在这方面也有一篇很好的文章: http : //www.cpearson.com/excel/scope.aspx
您应该将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
我的首选解决scheme是将结果传入和传出方法,而不是具有模块级别的范围,因为这会导致意大利面代码出现问题。