【问题标题】:Find Next Red Cell in Column, Return Rownumber of that Cell在列中查找下一个红色单元格,返回该单元格的行号
【发布时间】:2021-06-26 12:09:34
【问题描述】:

我正在寻找一个函数/宏,它可以在我的工作表的 A 列中找到下一个红色单元格。基本上我想做的是循环通过A列,每次我找到一个空单元格时,跳到下一个红色单元格并在那里继续循环。我只需要下一个红色单元格的行# 就可以了。到目前为止我的工作是这样的:

'Loop through column A from top down starting in row 6 and create Chart for each row
For rownumber = 6 To LastRow Step 1

'If Cell is filled
If TPsheet.Cells(rownumber, 1) <> "" Then
'Create Chart
Call CreateChart()
Else
rownumber = rownumber + 2 (This is the problem area, just going down 2 rows sadly doesnt work as sometimes there are 3/4/5 consecutive empty rows, need to set this to rownumber of next red cell)
End If

Next rownumber

整个表格如下所示: 并且不同的红色表格部分之间的空行数量可能会有所不同,这就是为什么当它找到一个空行时(我目前的方法)只向下走一定数量的行不起作用。

为了找到一个红细胞,我在另一个宏中成功使用了它:

Do Until i = 1000
    If TPsheet.Range("A" & i).Interior.Color = RGB(255, 0, 0) Then
       '...  
    End If
i = i + 1
Loop

TLDR:需要列a中下一个红色单元格的行号,当单元格为空时循环该列以跳转到下一个红色表头

【问题讨论】:

    标签: excel vba loops find office365


    【解决方案1】:

    您可以使用内置的 Excel Find-函数来搜索格式。以下函数将返回具有特定背景颜色的下一个单元格,如果在某个点以下找不到单元格,则返回 Nothing

    Function FindColoredCell(ws As Worksheet, searchColor As Long, Optional afterCell As Range = Nothing) As Range
        If afterCell Is Nothing Then Set afterCell = ws.Cells(1, 1)
        ' Set the search parameter for the specific color.
        With Application.FindFormat.Interior
            .Pattern = xlSolid
            .color = searchColor
        End With
        ' Do the search
        Dim cell As Range
        Set cell = ws.Cells.Find(What:="", after:=afterCell, SearchDirection:=xlNext, SearchFormat:=True)
        If cell Is Nothing Then Exit Function           ' now cell found with color
        If cell.row < afterCell.row Then Exit Function  ' now cell below "afterCell", Search started at top
        Set FindColoredCell = cell
    End Function
    

    (如果它更符合您的需要,请将返回值更改为Long 并返回cell.Row

    注意:在您的代码中,您使用的是For-Loop。如果您要在循环中修改计数变量(在您的情况下为 rowNumber),请不要这样做,否则您将得到不可预测的结果。请改用 do Do While-Loop。

    【讨论】:

    • 非常感谢,这真是太聪明了!当对Function FindColoredCell(ws As Worksheet, searchColor As Long, Optional afterCell As Range = Nothing) As Long(长而不是范围)和Set FindColoredCell = cell.row进行上述更改时,我得到一个“需要对象”-cell.row 上的错误?任何想法如何解决这一问题?非常感谢
    • 只有当cell 不是什么都不是时,您才能阅读cell.row(换句话说,当找到彩色单元格时)(我的示例中有Exit Function-statements,在这种情况下为0将被退回)。
    • 将 .color 更改为 .colorIndex 并将 3 用于红色,现在它的工作就像一个魅力 :) 谢谢!
    【解决方案2】:

    如果您已经超过最后一条红线,这将需要很长时间。

    Function GetNextRedInteriorRowNumber() As Long
    
    Dim i As Long
    Dim c As Long
    Dim retVal As Long
    
    retVal = -1
    c = ActiveCell.Column
    
    For i = ActiveCell.Row + 1 To ActiveSheet.Rows.Count
        
        If ActiveSheet.Cells(i, c).Interior.Color = RGB(255, 0, 0) Then
            retVal = i
            Exit For
        End If
    Next
    
    GetNextRedInteriorRowNumber = retVal
    
    End Function
    

    【讨论】:

    • 注意:最好find the last row 而不是一直循环到工作表底部。
    • 谢谢!还实现了 Ben 的 Last-Row :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多