为什么For ... Next循环在2次运行后中断?

时间:2017-03-18 15:28:22

标签: vba excel-vba loops iteration break

我在Excel 2016,VBA中遇到了我的代码问题。

请查看代码:

Private Sub Add_ProjectName()

Dim i As Integer
Dim iRowName As Integer
Dim iColName As Integer
Dim rFind As Range
Dim iFind As Long
Dim ws As Worksheet
Dim lRow As Integer

'Find last row in Master Sheet
Set ws = ThisWorkbook.Worksheets("Master")
ws.Activate

lRow = ws.Cells(ws.Rows.Count, 7).End(xlUp).Row

'Start adding project names per day
With ws
    For i = 6 To lRow

        Set rFind = .Range(.Cells(5, 14), .Cells(5, 378))           'Each cell in this range is a date ranging from Feb 1 to Dec 31
        rFind.NumberFormat = "mm-dd"                                'Change the Format of the Date Range from "dd" to "mm-dd"

        iFind = .Cells(i, 4).Value                                  'The Commencement date of the Project


        'Find the Column of the Date that is equal to the Commencement date of the Project on rFind
        iColName = rFind.Find(What:=Format(CDate(iFind), "mm-dd"), _
                                After:=.Range(.Cells(5, 14), .Cells(5, 14)), _
                                LookIn:=xlValues, _
                                LookAt:=xlWhole, _
                                SearchOrder:=xlByRows, _
                                SearchDirection:=xlNext, _
                                MatchCase:=True).Column

        'Set the Row of the Commencement date of the Project
        iRowName = .Cells(i, 10).Row

        'Adding the Project Name
        .Cells(iRowName, iColName).Value = .Cells(i, 10).Value

        Set rFind = Nothing

    Next i

End With


'Change the format of the whole range back to showing the Date only
rFind.NumberFormat = "dd"   

End Sub

因此,前两次迭代运行良好。然后当第三次迭代开始时,我得到的是"运行时错误' 91' - 对象变量或未设置块变量"。

调试后,系统说它是由

引起的
iColName = rFind.Find(What:=Format(CDate(iFind), "mm-dd"), _
                                After:=.Range(.Cells(5, 14), .Cells(5, 14)), _
                                LookIn:=xlValues, _
                                LookAt:=xlWhole, _
                                SearchOrder:=xlByRows, _
                                SearchDirection:=xlNext, _
                                MatchCase:=True).Column

我一次又一次地检查这个,并且找不到" With block not set"也没有取消对象。

有没有人知道如何解决这个问题?

非常感谢你。

Thi An。

2 个答案:

答案 0 :(得分:0)

我认为您尝试在包含format的范围内查找字符串(由dates返回)。我会试试

iColName = rFind.Find(What:=iFind,

代替。

答案 1 :(得分:0)

我今天再次尝试,并发现问题在于参数LookIn:=。我将其从LookIn:=xlValues更改为LookIn:=xlFormulas并且有效!

我不得不通过使用另一行(在本例中为Row(1))稍微调整一下,并将所有日期恢复为“常规”格式(例如2017年1月1日的42736)。

我添加了一个错误处理程序,以防rFind返回Nothing。

非常感谢你的帮助@iDevlop和@ A.S.H

这是工作代码。

Private Sub Add_ProjectName()

Dim i As Integer
Dim iRowName As Integer
Dim iColName As Integer
Dim rFind As Range
Dim iFind As Long
Dim ws As Worksheet
Dim lRow As Integer

'Find last row in Master Sheet
Set ws = ThisWorkbook.Worksheets("Master")
ws.Activate

lRow = ws.Cells(ws.Rows.Count, 7).End(xlUp).Row

'Start adding project names per day
With ws
    For i = 6 To lRow


        iFind = .Cells(i, 4).Value      'The Commencement date of the Project

        'This is 365 days in a year shown in "General" Format
        Set rFind = .Range(.Cells(1, 14), .Cells(1, 378)).Find(What:=iFind, _
                                                                    After:=.Cells(1, 13), _
                                                                    LookIn:=xlFormulas, _
                                                                    LookAt:=xlWhole, _
                                                                    SearchOrder:=xlByRows, _
                                                                    SearchDirection:=xlNext, _
                                                                    MatchCase:=True)

        If rFind Is Nothing Then
            GoTo NextIteration
        Else
            iColName = rFind.Column         'Set the Column of the Commencement date of the Project
            iRowName = .Cells(i, 10).Row    'Set the Row of the Commencement date of the Project

            'Adding the Project Name
            .Cells(iRowName, iColName).Value = .Cells(i, 10).Value
        End If
NextIteration:
    Next i

End With

End Sub