Excel 下标越界怎么改
OptionExplicitDimarr,i&PrivateSubcmd查询_Click()Dimc%,a(),n%,b,j%IfComBox1.Text=""ThenM...
Option Explicit
Dim arr, i&
Private Sub cmd查询_Click()
Dim c%, a(), n%, b, j%
If ComBox1.Text = "" Then MsgBox "请选择查询方式!": ComBox1.SetFocus: Exit Sub
If ComBox1.Text = "车号" Then c = 2
If ComBox1.Text = "车队" Then c = 3
If ComBox1.Text = "发动机号" Then c = 8
b = Array("2", "3", "4", "5", "6", "8", "9", "10", "12")
For i = 2 To UBound(arr)
If arr(i, c) = Trim(TextBox1.Text) Then
n = n + 1: ReDim Preserve a(1 To UBound(b) + 1, 1 To n)
For j = 1 To 9
a(j + 1, n) = arr(i, b(j)) 这行提示小标越界什么该
Next
If n = 1 Then
With Sheet2
.Range("B2") = arr(i, 6)
.Range("F2") = arr(i, 7)
.Range("B3") = "'" & arr(i, 8)
.Range("F3") = arr(i, 9)
.Range("B4") = arr(i, 10)
End With
End If
End If 展开
Dim arr, i&
Private Sub cmd查询_Click()
Dim c%, a(), n%, b, j%
If ComBox1.Text = "" Then MsgBox "请选择查询方式!": ComBox1.SetFocus: Exit Sub
If ComBox1.Text = "车号" Then c = 2
If ComBox1.Text = "车队" Then c = 3
If ComBox1.Text = "发动机号" Then c = 8
b = Array("2", "3", "4", "5", "6", "8", "9", "10", "12")
For i = 2 To UBound(arr)
If arr(i, c) = Trim(TextBox1.Text) Then
n = n + 1: ReDim Preserve a(1 To UBound(b) + 1, 1 To n)
For j = 1 To 9
a(j + 1, n) = arr(i, b(j)) 这行提示小标越界什么该
Next
If n = 1 Then
With Sheet2
.Range("B2") = arr(i, 6)
.Range("F2") = arr(i, 7)
.Range("B3") = "'" & arr(i, 8)
.Range("F3") = arr(i, 9)
.Range("B4") = arr(i, 10)
End With
End If
End If 展开
1个回答
展开全部
数组下咐巧标是从0开始的衡祥键,
For j = 1 To 9
a(j + 1, n) = arr(i, b(j)) 这行提示小标越界什么该
Next
这里应宴埋该改为:
For j = 0 To 8
a(j + 1, n) = arr(i, b(j)) 这行提示小标越界什么该
Next
For j = 1 To 9
a(j + 1, n) = arr(i, b(j)) 这行提示小标越界什么该
Next
这里应宴埋该改为:
For j = 0 To 8
a(j + 1, n) = arr(i, b(j)) 这行提示小标越界什么该
Next
追问
谢谢你问题解决了
但是 可不可以解释一下为什么 1 TO 9
要改成0 TO8
追答
VBA中数组下标从0开始的,你定义的数组:
b = Array("2", "3", "4", "5", "6", "8", "9", "10", "12"),这里可以看出来数组b有9个元素。所以,其下标是0、上标是8,0~8就是9个元素,即b(0)、b(1)……b(8)。按照你的代码:
For j = 1 To 9
a(j + 1, n) = arr(i, b(j)) ,如果j=9,b(j)就是b(9),而数组中没有这个元素,所以上标越界错误。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询