[英]Loop macro through rows
編輯:我清理了代碼,如下所示。 我試圖找到一種方法,只需在偏移量范圍內的行中加1即可執行代碼。 這可能嗎?? 在此處輸入圖片說明
我要完成的工作:
結束
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
結束子
您的代碼有點難以理解,因為您縮進得很少(我對此進行了編輯),並且沒有注釋。 因此,我不確定您要重復哪個部分。
通常,我建議執行Do循環。 並根據其內容而不是顏色來標識最后一行。
將ColAcell
設置為Cell A5,然后:
Do Until ColAcell.value = "Total"
... (your code to process the row) ...
Set ColAcell = ColAcell.offset(1,0)
Loop
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.