Vba 为什么…下一个循环在2次运行后中断?
我在Excel 2016 VBA中的代码有问题 请看一下代码:Vba 为什么…下一个循环在2次运行后中断?,vba,excel,loops,iteration,break,Vba,Excel,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 = This
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
我反复检查,找不到“未设置块”或未设置对象
有人知道如何解决这个问题吗
事先非常感谢
这是一个。我认为您试图在包含
日期的范围内找到一个字符串(由格式
返回)。我会试试看
iColName = rFind.Find(What:=iFind,
相反。我今天再次尝试,发现问题出在参数LookIn:=
上。我把它从LookIn:=xlValues
改为LookIn:=xlFormulas
,它成功了
我不得不使用另一行(在本例中为第(1)行)对其进行了一些调整,并将所有日期恢复为“常规”格式(例如2017年1月1日的42736)
我添加了一个错误处理程序,以防rFind返回任何内容
非常感谢您对@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
哪条语句触发了错误?我必须找到日期列的部分,确切地说是iColName变量。我将编辑这个问题。感谢您指出这一点。这意味着Find
方法没有找到匹配项。尝试Debug.Print
ing在搜索之前查找的值。我将Debug.Print
放在iColName=
之前,得到3个值,即0、101、104
;在我遇到错误'91'
Debug.Print-if-ind
之前,我也尝试过,甚至在此之前。但是我无法运行它,并且发现它与rFind中的元素不匹配,因为我将rFind格式化为“mm-dd”
,因此在rFind.NumberFormat=“mm-dd”
和格式(CDate(iFind),“mm-dd”)中使用了匹配的格式
@ThiAn changingrFind.NumberFormat
不会更改其搜索值。肯定是@Ralph。正如你所指出的,由于我的声誉,我现在不能投票。但我将来会的。当然我不能接受我自己的答案,所以。。。谢谢你让我知道。