切换工作表vba时while循环出错1004

时间:2014-07-11 19:04:32

标签: excel vba excel-vba

所以我对vba很新,并且只学习了大约一个月的工作。我一直遇到这个问题,我得到错误1004"应用程序定义,或对象定义错误"当我尝试运行我的宏。我想要做的是浏览一个数据列表,并查明一行是否附加了特定的代码。如果是这样,我想将该行上的日期移动到新工作表。

Sub k0Pop()

Dim source As Long
Dim servR As Integer
Dim servC As Integer
Dim patID As Integer
Dim spot1 As Integer
Dim spot2 As Integer

Dim sht1 As Worksheet
Dim sht2 As Worksheet

Set sht1 = Sheets("Sheet2")
Set sht2 = Sheets("Sheet4")

servR = 3

For source = 3 To 100000
    servC = 2
    While sht2.Cells(source, 2) < sht2.Cells(patID, 15)
        source = source + 1
    Wend

    While Cells(source, 2) = Cells((source + 1), 2)
        If Cells(source, 6) = "K045A" Then
            spot = source
        End If
        source = source + 1
    Wend

    If Cells(source, 6) = "K045A" Then
        spot = source
        source = source + 1
    End If

    spot2 = source
    source = spot1

    While (Cells(spot, 5) - Cells(source, 5)) < 365
        source = source - 1
    Wend

    While source < spot
        sht1.Cells(servR, servC) = sht2.Cells(source, 5)
        source = source + 1
        servC = servC + 1
    Wend

    sht1.Cells(servR, 14) = sht2.Cells(spot, 5)

    source = spot2
    servR = servR + 1
    patID = patID + 1
Next

End Sub

1 个答案:

答案 0 :(得分:3)

看起来patID为0,这是一个无效的列。使用F8进行单循环。将鼠标悬停在变量上可查看其值。使用debug.print&#39;来显示中间数据。