VBA: Loop until Find function fails

liquid81

New Member
Joined
Sep 5, 2008
Messages
5
Hi,

Beginner question here.

Say I have a VBA script that finds the value 100 in a worksheet and replaces is with 1. I want it to run until there is no cell with value 100 in the worksheet anymore.

How can I do this? Do I use a Loop function?

Thanks in advance.
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
Hello,

try

Code:
Sub Macro1()
    Cells.Replace What:="100", Replacement:="1", LookAt:=xlWhole, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False
End Sub

don't really see the need for a loop.
 
Upvote 0
Lifted straight from help, untetested

Code:
With Worksheets(1).Range("a1:a500")
    Set c = .Find(100, lookin:=xlValues)
    If Not c Is Nothing Then
        firstAddress = c.Address
        Do
            c.Value = 1
            Set c = .FindNext(c)
        Loop While Not c Is Nothing And c.Address <> firstAddress
    End If
End With
 
Upvote 0
Hello,

try

Code:
Sub Macro1()
    Cells.Replace What:="100", Replacement:="1", LookAt:=xlWhole, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False
End Sub

don't really see the need for a loop.

Thanks for the quick reply. You're right, the scenario I gave doesn't need one.

But let's say I have a whole script (not just replacing 100 with 1 but with lots of other changes to the sheet) that starts with finding the value 100. What I want to do is to run this script over and over until it cannot find a cell with value 100 anymore.

Thanks!
 
Upvote 0
Hello,

how about

Code:
Sub Macro2()
   Cells.SpecialCells(xlCellTypeConstants, 23).Select
   For Each CELL In Selection
    Select Case CELL
        Case 100
            CELL.Value = 1
        Case 1
            CELL.Value = 10
    End Select
    Next CELL
End Sub

add further

Code:
Case
CELL.value =

before the End Select
 
Upvote 0

Forum statistics

Threads
1,214,606
Messages
6,120,479
Members
448,967
Latest member
visheshkotha

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top