如何在Excel VBA

时间:2019-07-30 12:16:18

标签: excel vba

我是Excel VBA的新手,我尝试设置一个宏,该宏将搜索突出显示的单元格,获取其单元格地址,并突出显示与原始突出显示的单元格相距指定行数的单元格。我试图创建一个循环,在其中循环浏览列中的每个突出显示的单元格(目前,我只是将每个值放在数组中,而不是突出显示所需的单元格以测试循环是否有效),但是该问题标题中提到的错误,“尚无法弄清为什么会发生这种情况”,将非常感谢您帮助您解决此错误。 谢谢

Dim cell As Range
Dim start As Range
Set start = Range("A1")
Dim address As Range
Do While counter < 2

Application.FindFormat.Clear
Application.FindFormat.Interior.Color = RGB(146, 208, 80)
Set cell = Range("A:A").Find("*", SearchFormat:=True, after:=start)`



Set address = Range(cell.address)

 Set start = Range(address)



If cell.Value = (28 / 1 / 2013) Then

 counter = counter + 1

End If


ReDim Preserve arr(i)
arr(i) = cell.Value
i = i + 1

1 个答案:

答案 0 :(得分:0)

这应该让您开始。

您将需要添加一项检查,以防止您多次找不到同一单元格。

Sub x()

Dim cell As Range
Dim start As Range
Dim counter As Long, i As Long, arr()

Application.FindFormat.Clear
Application.FindFormat.Interior.Color = RGB(146, 208, 80)

Set start = Range("A1")

Do While counter < 2
    Set cell = Range("A:A").Find("*", SearchFormat:=True, after:=start)
    If Not cell Is Nothing Then
        ReDim Preserve arr(i)
        arr(i) = cell.Value
        i = i + 1
        counter = counter + 1
        Set start = cell
    End If
Loop

End Sub