对象变量或With块变量未设置为中间循环

时间:2017-10-24 09:22:44

标签: excel-vba runtime-error vba excel

我在这里遇到的问题是

iRow = PullRange.Find(What:=a(x, 0)).Row - 1

在循环中间抛出上述错误。事情是它适用于x的第一次迭代,但是在x = 11 / iRow = 30时它会中断并且我不知道为什么。关于那个特定的行并没有什么独特之处,但它崩溃了,有没有人知道为什么?

如果需要更多信息,请告诉我。

Public Sub DataDump(Sql As String, PullColumn As String)
Dim a() As Variant
Dim iRow As Integer
Dim PullRange As Range
Dim DumpRange As Range
Dim x As Integer
Dim y As Integer

Set PullRange = Range(PullColumn & ":" & PullColumn)
Set DumpRange = Range("H1")


Set recs = New ADODB.Recordset

recs.Open Sql, con, adOpenDynamic


a = recs.GetRows

'a(y, x)


For x = 0 To UBound(a, 2)
  If IsNull(a(0, x)) Then GoTo none
    iRow = PullRange.Find(What:=a(0, x)).Row - 1
        If iRow = 0 Then GoTo none
    For y = 1 To UBound(a, 1)
        Debug.Print a(y, x)
        If IsNull(a(y, x)) Then GoTo Err
        DumpRange.Offset(iRow, y) = a(y, x)
Err:
    Next y
none:
Next x



End Sub

1 个答案:

答案 0 :(得分:0)

@Rory是对的。不清洁的数据导致循环中断,我无法看到差异,因为差异很小,在从自己修复的同一个源中重新插入数据集之后。学过的知识。