编辑:我已经清理了代码,如下所示。我试图找到一种方法来执行代码,只需在偏移范围中添加1行。这可能吗?? enter image description here
我想要完成的任务:
结束
Sub Find_Data()
Dim datatoFind As String, MySheet As String, FV As String
Dim aSh As Worksheet, fSh As Worksheet
Dim firstResult As Range
Dim secondResult As Range
Dim rng As Range
Dim LeftCell As Range
Dim leftValue As String
Set rng = Cells.Find(What:="Wage QRE Exp", after:=ActiveCell, LookIn:=xlFormulas, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=True, SearchFormat:=False)
Set LeftCell = rng.Offset(0, -1)
leftValue = LeftCell.Value
If leftValue = "Ref." Then
Set findValue = rng.Offset(1, 0)
Set aSh = Sheet1
datatoFind = findValue
sheetCount = ActiveWorkbook.Sheets.Count
If Len(datatoFind) = 0 Or Not IsNumeric(datatoFind) Then Exit Sub
For counter = 1 To sheetCount
Sheets(counter).Activate
Set firstResult = Cells.Find(What:=datatoFind, after:=ActiveCell, LookIn:=xlFormulas, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=True, SearchFormat:=False)
Set secondResult = Cells.FindNext(after:=firstResult)
Debug.Print secondResult.Address
If Not firstResult Is Nothing Then
MySheet = IIf(InStr(secondResult.Parent.Name, "."), Split(secondResult.Parent.Name, ".")(0), Split(secondResult.Parent.Name)(0))
FV = MySheet & "." & pageNum(secondResult)
Else
End If
Next counter
With rng.Offset(1, -1)
.Value = FV
.Font.Name = "Times New Roman"
.Font.Bold = True
.Font.Size = "10"
.Font.Color = vbRed
.HorizontalAlignment = xlRight
.VerticalAlignment = xlCenter
End With
End If
End Sub
答案 0 :(得分:0)
你的代码有点难以理解,因为你没有非常缩进(我编辑过)并且没有评论。所以我不确定你想重复哪一节。
一般来说,我建议使用Do循环。并根据其内容确定最后一行,而不是其颜色。
将ColAcell
设置为Cell A5,然后:
Do Until ColAcell.value = "Total"
... (your code to process the row) ...
Set ColAcell = ColAcell.offset(1,0)
Loop