我的代码应该从列表框中获取项目并将它们分开,以便在工作表的表格中建立它。奇怪的部分并不是它不断抛出
订阅超出范围
它是针对随机特定项目抛出的,我尝试使用具有相似特征的不同项目,但它们仍然有用。所有这些物品都来自同一张纸。我希望有人能够查看我的代码,以确保它是我的代码,并且这不是我错过的单词中的一些特征
以下是发生错误的地方:
.Range("A" & (i + 1)).Value = strsplt(i)
这是整个代码:
Dim i As Long, j As Long, ii As Long
Dim found As Boolean
Dim str As String
Dim message, title, defaultval As String
Dim quantity As String
Dim strsplt() As String
ReDim strsplt(0 To i)
'Seperate Items and put them in table with quantity needed
With Me.selecteditems
ThisWorkbook.Sheets(9).Range("A:B").ClearContents
For i = 0 To .ListCount - 1
If .Selected(i) Then
found = True
For ii = 0 To .ColumnCount - 1
If str = "" Then
str = .List(i, ii) & vbTab
Else
If ii < .ColumnCount - 1 Then
str = str & .List(i, ii) & vbTab
Else
str = str & .List(i, ii)
End If
End If
Next ii
message = "How much" & vbCrLf & str & "?"
title = "Amount"
defaultval = "1"
quantity = InputBox(message, title, defaultval)
strsplt() = Split(str, " ")
End If
'On Error Resume Next
With ThisWorkbook.Sheets(9)
.Range("A" & (i + 1)).Value = strsplt(i)
.Range("B" & (i + 1)).Value = quantity
End With
'On Error GoTo 0
Next i
End With
答案 0 :(得分:1)
ReDim strsplt(0 To i)
当该指令运行时,i
的值为0
; <{1}}语句实际上是无用的/冗余的。
然后分配数组:
ReDim
括号是多余的,但这不是问题:问题是如果strsplt() = Split(str, " ")
使用str
分隔符不能Split
成i
个元素,那么这样:
" "
从数组中拉出一个越界值。
我很乐意帮助简化您的代码,但逻辑相当复杂,我不能花一个小时的时间。学习放置断点(F9),逐步执行代码(F8)并检查局部变量值(使用 Locals 工具窗口,或使用<{1}} / .Range("A" & (i + 1)).Value = strsplt(i)
语句em> immediate 窗格,或者只是将变量悬停在中断模式下),你应该快速找到解决方案。