0
我写了一个代码来删除Excel表中的行,但它给了我一个错误,如主题中所述。VbScript抛出一个错误“未知的运行时错误”
CODE
Sub ChildPidDelt(ob3,DeletArr)
Dim height,row,str,i
Dim dataArray
Dim d
height = objExcel1.Application.WorksheetFunction.CountA(ob3.Columns(1))
'MsgBox(height)
ReDim dataArray(height - 2, 0) ' -1 for 0 index, -1 for the first row as header row, excluded
str = ""
dataArray = ob3.Range(ob3.Cells(2, 1),ob3.Cells(height, 1)).Value
Set d = CreateObject("scripting.dictionary")
MsgBox(LBound(DeletArr) & ":" & UBound(DeletArr))
For i = LBound(DeletArr) To UBound(DeletArr)
If Not d.exists(DeletArr(i)) Then
d(DeletArr(i)) = 0
End If
Next
MsgBox(LBound(dataArray,1) & ":" & UBound(dataArray,1))
For i = LBound(dataArray, 1) To UBound(dataArray, 1)
If d.exists(dataArray(i, 1)) Then
str = str & (i+1) & ":" & (i+1) & ","
Else
'found = False
End If
Next
If Len(str) > 0 Then
MsgBox(Len(str))
str = Mid(str, 1, Len(str) - 1)
MsgBox(str)
ob3.Range(str).Delete
End If
End Sub
请看以下调试屏幕:
你能帮助我在这里说的是什么原因?
这样删除行,反正可以删除不需要的行,因为删除会导致行向上移动?有更快的过程吗?它会一个一个地删除对吗? –
好吧,显然你可以把它分解成大块......我更新以显示一个可能的方式来做到这一点。 –
不错的一个,只是为了确认它会是'UBOUND(rangesToRemove)'还是'UBOUND(rangesToRemove)-1'? –