循环宏通过行

时间:2018-05-07 01:14:28

标签: excel vba excel-vba for-loop row

编辑:我已经清理了代码,如下所示。我试图找到一种方法来执行代码,只需在偏移范围中添加1行。这可能吗?? enter image description here

我想要完成的任务:

  1. 查找列“工资QRE Exp”  一个。如果上面的单元格与“Ref。”相邻,那么我想搜索它们的下面的值
  2. 将宏应用于“QRE”列的灰色行之间的所有行。  一个。填写左侧相邻单元格中的页码引用  湾直到细胞颜色=灰色-25%(。ThemeColor = xlThemeColorDark2)我相信
  3. 结束

    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

1 个答案:

答案 0 :(得分:0)

你的代码有点难以理解,因为你没有非常缩进(我编辑过)并且没有评论。所以我不确定你想重复哪一节。

一般来说,我建议使用Do循环。并根据其内容确定最后一行,而不是其颜色。

ColAcell设置为Cell A5,然后:

Do Until ColAcell.value = "Total"

   ... (your code to process the row) ...

   Set ColAcell = ColAcell.offset(1,0)

Loop