【问题标题】:Loop macro through rows通过行循环宏
【发布时间】:2018-05-07 01:14:27
【问题描述】:

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

我想要完成的事情:

  1. 查找“工资 QRE Exp”列 一个。如果上述单元格与“Ref.”相邻。然后我想搜索他们的以下值
  2. 将宏应用于“QRE”列的灰色行之间的所有行。 一个。在左侧相邻单元格中填写页码引用 湾。直到单元格颜色 = Gray -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
    

    结束子

【问题讨论】:

  • 这是什么意思? Find Columns “Wage QRE Exp” a. If the above cell is left adjacent to “Ref.” then I want to search their below values
  • 插入工作表图像时,请确保行和列编号/字母可见。
  • dbmitch:我想在我的新图片中搜索显示为“Wage QRE Exp”的单元格,C4,并确保 B4 为 =“Ref”。但这已经在我当前的代码中解决了。现在我只需要它再次运行第 6 行及以后的宏。

标签: vba excel for-loop row


【解决方案1】:

您的代码有点难以理解,因为您缩进不多(我编辑了它)并且没有 cmets。所以我不确定你想重复哪个部分。

一般来说,我建议使用 Do 循环。并通过内容而不是颜色来识别最后一行。

设置ColAcell为单元格A5,则:

Do Until ColAcell.value = "Total"

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

   Set ColAcell = ColAcell.offset(1,0)

Loop

【讨论】:

    猜你喜欢
    • 2011-02-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-07
    • 1970-01-01
    相关资源
    最近更新 更多