vba遍历字符串,使用VBA在工作表中搜索字符串

I am trying to search for a particular string "ERROR" in all the worksheets in the workbook and make it bold and color the found cell red.

I am able to parse through each worksheet. I am not able to use the Find function of VBA.

解决方案

Here's an example of using Find and formatting the found cells

Sub FindERROR()

Dim SearchString As String

Dim SearchRange As Range, cl As Range

Dim FirstFound As String

Dim sh As Worksheet

' Set Search value

SearchString = "ERROR"

Application.FindFormat.Clear

' loop through all sheets

For Each sh In ActiveWorkbook.Worksheets

' Find first instance on sheet

Set cl = sh.Cells.Find(What:=SearchString, _

After:=sh.Cells(1, 1), _

LookIn:=xlValues, _

LookAt:=xlPart, _

SearchOrder:=xlByRows, _

SearchDirection:=xlNext, _

MatchCase:=False, _

SearchFormat:=False)

If Not cl Is Nothing Then

' if found, remember location

FirstFound = cl.Address

' format found cell

Do

cl.Font.Bold = True

cl.Interior.ColorIndex = 3

' find next instance

Set cl = sh.Cells.FindNext(After:=cl)

' repeat until back where we started

Loop Until FirstFound = cl.Address

End If

Next

End Sub

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值