简体   繁体   English

通过行循环宏

[英]Loop macro through rows

EDIT: I have cleaned up the code as seen below. 编辑:我清理了代码,如下所示。 I am trying to find a way to execute the code with simply adding 1 to row in the offset ranges. 我试图找到一种方法,只需在偏移量范围内的行中加1即可执行代码。 Is this possible?? 这可能吗?? enter image description here 在此处输入图片说明

What I am trying to accomplish: 我要完成的工作:

  1. Find Columns “Wage QRE Exp” a. 查找“工资QRE Exp”列。 If the above cell is left adjacent to “Ref.” then I want to search their below values 如果上面的单元格与“ Ref。”相邻,那么我要搜索其下面的值
  2. Apply the macro to all lines in between the gray rows of the 'QRE' column. 将宏应用于“ QRE”列的灰色行之间的所有行。 a. 一种。 Fill in the page number references in the left adjacent cells b. 在左侧相邻的单元格b中填写页码参考。 Do until cell color = Gray -25% (.ThemeColor = xlThemeColorDark2) I believe 我相信直到单元格颜色=灰色-25%(.ThemeColor = xlThemeColorDark2)
  3. End 结束

     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 结束子

Your code is a little hard to follow because you don't indent very much (I edited that) and there are no comments. 您的代码有点难以理解,因为您缩进得很少(我对此进行了编辑),并且没有注释。 So I'm not sure what section you want to repeat. 因此,我不确定您要重复哪个部分。

Generally, I suggest a Do loop. 通常,我建议执行Do循环。 And identify the last row by its contents, not its color. 并根据其内容而不是颜色来标识最后一行。

Set ColAcell to be Cell A5, then: 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM