我想知道是否有人会在下面的脚本中建议对指定行进行一些更正。 它抛出"对象变量或With块变量未设置"报警。 我只能猜测这意味着" CellFound"范围没有设置,问题在于该行。 " CellFound"变量用于在DateRng中查找并存储cell.value< = 25的位置,以供以下条件使用
要重新迭代,整个脚本将执行以下任务:
找到位于包含特定字符串的两个单元格之间的范围(DateRng)
在此范围内为具有值
比较另外两个偏移到" i"
导出以" i"为中心的行范围。在上述条件的结果之前的不同表格。
感谢您的时间。
Sub ReportCells()
Dim LR As Long, i As Long
Dim j, k As Long
Dim StartDate, FinishDate As String
Dim Sh As Worksheet: Set Sh = Sheets("Full chart and primary cals")
Dim CellFound As Range
'Range Extraction Script
'Search location and values
LookupColumn = "B"
StartDate = "2013.01.02 20:00"
FinishDate = "2013.01.09 20:00"
'Find Lower Limit
For j = 1 To 30000
If Sh.Range(LookupColumn & j).Value = FinishDate Then FinishDateRow = j
Next j
'Find Upper Limit
For k = FinishDateRow To 1 Step -1
If Sh.Range(LookupColumn & k).Value = StartDate Then StartDateRow = k - 1
Next k
'Set Range once located
Dim DateRng As Range: Set DateRng = Sh.Range(LookupColumn & StartDateRow & ":" & LookupColumn & FinishDateRow)
MsgBox DateRng.Address
'Find Cell
With DateRng
LR = Range("B" & Rows.Count).End(xlUp).Row
For i = 1 To LR
** Set CellFound = .Find(Sh.Range("M:M").Value <= 25, LookIn:=xlValues) **
MsgBox CellFound.Address
If Not CellFound Is Nothing And CellFound.Offset(0, -5).Value < CellFound.Offset(-1, -5).Value Then .Offset(-3, 0).Resize(10, 1).EntireRow.Copy Destination:=Sheets("DownT").Range("A" & Rows.Count).End(xlUp).Offset(2)
If Not CellFound Is Nothing And CellFound.Offset(0, -5).Value > CellFound.Offset(-1, -5).Value Then .Offset(-3, 0).Resize(10, 1).EntireRow.Copy Destination:=Sheets("UpT").Range("A" & Rows.Count).End(xlUp).Offset(2)
Next i
End With
End Sub
编辑:单元格选择和复制块已修改为以下代码。似乎值&lt; = 25设置范围命令没有按原样执行。他们肯定会过滤数据,但我不确定哪一列。该块返回一系列正确大小的单元格。但只有一个范围(而不是大约20左右)。错误的行数范围:S我猜任何进展都是进步,无论它是对还是错
With Sheets("Full chart and primary cals")
LR = Range("B" & Rows.Count).End(xlUp).Row
'For i = Range("M" & Rows.Count).End(xlUp).Row To 1 Step -1
For i = 1 To LR
With DateRng.Range("M" & i)
If Range("M" & i).Value <= 25 Then Set CellFound = Sh.Range("M" & i)
If Not CellFound Is Nothing Then .Offset(-5, 0).Resize(10, 1).EntireRow.Copy Destination:=Sheets("DownT").Range("A" & Rows.Count).End(xlUp).Offset(2)
End With
Next i
End With
答案 0 :(得分:0)
从我的代码中我可以看出你误用了Range.Find()
函数,这很可能会导致它返回Nothing
而不是有意义的范围。
Sh.Range("M:M").Value
会抛出类型不匹配错误,因为您无法使用包含多个单元格的.Value
Range
属性。由于此错误包含在.Find函数的参数中,因此它可能只是被忽略但仍会导致.Find
返回Nothing
。Sh.Range("A1") <= 25
评估为True
或False
的情况(取决于A1的值),Find
函数也会搜索{{1}对于该范围内的DateRng
或True
的第一个实例。关于False
函数的工作方式,我建议使用further reading,因为它可能不适合您的任务。
答案 1 :(得分:0)
解决问题的方法........
'Loop through sheet looking for cells
LR = .Range("B" & Rows.Count).End(xlUp).Row
For i = 10 To LR
'Find cells in "M" and store thier reference in Cellref
If .Range("M" & i).Value <= 25 Then Set Cellref = .Range("M" & i) Else Set Cellref = .Range("Z15")
'Find if Cell ref is contained within DateRange and store result as bool
If Not Application.Intersect(DateRange, Cellref) Is Nothing Then iSect = True Else iSect = False
'Output cell ranges to the appropriate sheets
If iSect = True And Cellref.Offset(0, -5) < Cellref.Offset(-10, -5) Then _
Cellref.Offset(-3, 0).Resize(10, 1).EntireRow.Copy Destination:=Sheets("DownT").Range("A" & Rows.Count).End(xlUp).Offset(2)
If iSect = True And Cellref.Offset(0, -5) > Cellref.Offset(-10, -5) Then _
Cellref.Offset(-3, 0).Resize(10, 1).EntireRow.Copy Destination:=Sheets("UpT").Range("A" & Rows.Count).End(xlUp).Offset(2)
Next i