Help with 'If Not c is Nothing' loop

Nooblet

Board Regular
Joined
Feb 18, 2010
Messages
56
Hi all,

I have a macro that works fine, except it performs the steps one time too many.

As far as I can figure it, once it realises that If Not c is Nothing is no longer true (i.e. c is Nothing), it still performs all of the steps afterwards one more time before ending the loop.

Can anyone help with adjusting the code (below) so that it stops doing this?

Code:
Sub Macro1()
'
' Macro1 Macro
'

Set SrchRng = ActiveSheet.Range("A1", ActiveSheet.Range("A65536").End(xlUp))
    Do
        Set c = SrchRng.Find("Bob:", LookIn:=xlValues)
        If Not c Is Nothing Then c.Select
        Selection.Cut
        ActiveCell.Offset(-1, 1).Select
        ActiveSheet.Paste
        Application.CutCopyMode = False
    Loop While Not c Is Nothing
        
'
End Sub

Thanks for your assistance
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
Hi,

Maybe this

Code:
Sub Macro1()
'
' Macro1 Macro
'
    Dim srchRng As Range, c As Range
    Set srchRng = ActiveSheet.Range("A1", ActiveSheet.Range("A65536").End(xlUp))
    Set c = srchRng.Find("Bob:", LookIn:=xlValues)
    Do While Not c Is Nothing
        c.Select
        Selection.Cut
        ActiveCell.Offset(-1, 1).Select
        ActiveSheet.Paste
        Application.CutCopyMode = False
        Set c = srchRng.Find("Bob:", LookIn:=xlValues)
    Loop
End Sub

HTH

M.
 
Upvote 0
Improved

Code:
Sub Macro2()
'
' Macro2 Macro
'
    Dim srchRng As Range, c As Range
    Set srchRng = ActiveSheet.Range("A1", ActiveSheet.Range("A65536").End(xlUp))
    Set c = srchRng.Find("Bob:", LookIn:=xlValues)
    Do While Not c Is Nothing
        c.Cut c.Offset(-1, 1)
        Set c = srchRng.Find("Bob:", LookIn:=xlValues)
    Loop
    
    Application.CutCopyMode = False
End Sub

M.
 
Upvote 0

Forum statistics

Threads
1,224,583
Messages
6,179,672
Members
452,937
Latest member
Bhg1984

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