未找到项目时代码崩溃

时间:2014-11-13 20:53:25

标签: vba

我正在尝试编写一个代码来查找一个单词然后根据它们进行下面的格式化。问题是单词并不总是在数据中。

理想我想只搜索"实施记录类型:"然后循环遍历单元格A1:Q1000并对具有该单词的所有单元格执行相同的格式化。

  Columns("A:A").Select
On Error Resume Next
Cells.Find(What:="Implementation Record Type: ^Parent Record", After:=ActiveCell, LookIn:=xlValues, LookAt:= _
    xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False _
    , SearchFormat:=False).Activate
        ActiveCell.Offset(1, 0).Resize(1, 17).Interior.ColorIndex = 23
        ActiveCell.Font.ColorIndex = 2
        ActiveCell.Interior.ColorIndex = 23

Columns("A:A").Select
On Error Resume Next
Cells.Find(What:="Implementation Record Type: Child Record", After:=ActiveCell, LookIn:=xlValues, LookAt:= _
    xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False _
    , SearchFormat:=False).Activate
        ActiveCell.Offset(1, 0).Resize(1, 17).Interior.ColorIndex = 23
        ActiveCell.Font.ColorIndex = 2
        ActiveCell.Interior.ColorIndex = 23

Columns("A:A").Select
On Error Resume Next
Cells.Find(What:="Grand Totals", After:=ActiveCell, LookIn:=xlValues, LookAt:= _
    xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False _
    , SearchFormat:=False).Activate
        ActiveCell.Offset(1, 0).Resize(1, 17).Interior.Color = 5296274
        ActiveCell.Interior.Color = 5296274

1 个答案:

答案 0 :(得分:0)

设置范围变量并搜索字符串

如果发现了某些内容,请测试范围

如果找到范围,请使用您的格式条件进行格式化

Dim strSearch As String
Dim rngTemp As Range

strSearch = "Implementation Record Type: ^Parent Record"

On Error Resume Next
Set rngTemp = Columns(1).Find(What:=strSearch, LookAt:=xlPart)
On Error GoTo 0

If Not rngTemp Is Nothing Then
    'Your format command goes here
End If

strSearch = "Implementation Record Type: Child Record"

On Error Resume Next
Set rngTemp = Columns(1).Find(What:=strSearch, LookAt:=xlPart)
On Error GoTo 0

If Not rngTemp Is Nothing Then
    'Your format command goes here
End If

strSearch = "Grand Totals"

On Error Resume Next
Set rngTemp = Columns(1).Find(What:=strSearch, LookAt:=xlPart)
On Error GoTo 0

If Not rngTemp Is Nothing Then
    'Your format command goes here
End If
相关问题