找到具有特定单元格值的特定范围的单元格

时间:2016-03-08 10:06:06

标签: excel vba excel-vba

我正在编写一个excel文档的脚本,该脚本将返回一个列中具有特定单元格值的单元格范围,这样我就可以依赖该范围内的另一列,其中有多少是绿色,琥珀色或红色。以下是excel文档的示例: -

Type    Reference   Department  Colour
Large   20                      Green
Large   21                      Green
Large   2                       Amber
Large   23                      Amber
Large   5                       Green
Medium  6                       Red
Medium  23                      Green
Medium  8                       Green
Medium  10                      Red
Small   11                      Amber
Small   12                      Red
Small   16                      Green
Small   17                      Green
Small   33                      Green
Small   36                      Amber
Small   55                      Amber

这是我的脚本,但它只找到带有该值的第二个单元格号。我需要找到所有' Large'' Medium'和'小'。

Set rngFindValue = ActiveSheet.Range("H22:H" & lastRow).Find(What:="Medium", After:=ActiveSheet.Range("H22"), LookIn:=xlFormulas)
            If Not rngFindValue Is Nothing Then
                MsgBox rngFindValue.Address
            End If

1 个答案:

答案 0 :(得分:0)

我已经解决了。这是我的新代码,我能够获得范围内的第一个单元格和最后一个单元格: -

Dim mediumStartRow As Integer
Dim mediumEndRow As Integer

        mediumStartRow = Range("H:H").Find(what:="Medium Project", after:=Range("H21")).Row
        mediumEndRow = Range("H:H").Find(what:="Medium Project", after:=Range("H21"), searchdirection:=xlPrevious).Row
        MsgBox "First and Last Row for Medium Projects: " & mediumStartRow & mediumEndRow

似乎正在运作