2017-03-18 30 views
0

我在Excel 2016,VBA中遇到了問題。爲什麼For ... Next Loop在2次運行後中斷?

請看看代碼:

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 

引起我一次又一次的檢查並不能找到「隨着未設定塊」也未設置對象。

有沒有人有任何想法如何解決這個問題?

非常感謝您提前。

你好。

+2

哪種說法是觸發錯誤? – trincot

+0

我必須找到日期列的部分,iColName變量是確切的。我將編輯該問題。感謝您指出了這一點。 –

+2

這意味着'Find'方法找不到匹配項。在搜索之前嘗試'Debug.Print'找到您要查找的值。 –

回答

0

我認爲你試圖找到含有dates範圍內的字符串(由format返回)。我會嘗試

iColName = rFind.Find(What:=iFind, 

改爲。

+0

我試過甚至在此之前,但我無法讓它運行,並發現它與rFind中的元素不匹配,因爲我將rFind格式化爲「mm-dd」,因此我使用的匹配格式'rFind.NumberFormat =「mm-dd」'和'Format(CDate(iFind),「mm-dd」)'。 –

+0

@ThiAn改變'rFind.NumberFormat'不會改變它的搜索值。 –

0

我今天再次嘗試和揣摩的問題是與參數LookIn:=。我將它從LookIn:=xlValues更改爲LookIn:=xlFormulas,它工作!

我不得不使用另一行(在這種情況下是行(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 
+0

絕對@Ralph。正如你所指出的,我現在不能投票。但我會在未來。當然,我不能接受我自己的答案,所以...謝謝你讓我知道。 –