No Value found in range go to Next i in Loop

CdL10

New Member
Joined
Jan 13, 2022
Messages
2
Office Version
  1. 365
Platform
  1. Windows
Hi All,

I have created the following:
Code:
Sub CodeChange()
'
' CodeChange Macro
'

  For i = [Run_Start] To [Run_End]
 [Number] = i

 
CodetoChange = [ code ]
NewValueReplace = [NewValue]
Dim ws As Worksheet
Dim RA As Range

        Set RA = Cells.Find(What:=[ code ], After:=ActiveCell, LookIn:=xlFormulas2, _
        LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
        MatchCase:=False, SearchFormat:=False)
     
 
        RA.Activate

 
    
         With Selection.Interior
        .Pattern = xlSolid
        .PatternColorIndex = xlAutomatic
        .Color = 15773696
        .TintAndShade = 0
        .PatternTintAndShade = 0
    End With

    ActiveCell.Offset(0, 13).Select
    ActiveCell.Value = [NewValue]
With Selection.Interior
        .Pattern = xlSolid
        .PatternColorIndex = xlAutomatic
        .Color = 15773696
        .TintAndShade = 0
        .PatternTintAndShade = 0
    End With
 
 Next i
 


End Sub

But if the [ code ] value is not found in the range it does not want to do to the next item in the loop.

Kindly assist

EDIT: Moderator added spaces to beginning and end of [ code ] variable, as it was conflicting with code tags used to post the code.
 
Last edited by a moderator:

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
It would help to know what you want your code to do.

This code will raise an error on this line of code if the value is not found.
VBA Code:
        RA.Activate
Are you getting an error?

I would do this (with enhanced indentation):

Rich (BB code):
Sub CodeChange()
'
' CodeChange Macro
'

   For i = [Run_Start] To [Run_End]
 
      [Number] = i
   
      CodetoChange = [ code ]
      NewValueReplace = [NewValue]
      Dim ws As Worksheet
      Dim RA As Range
   
      Set RA = Cells.Find(What:=[ code ], _
                          After:=ActiveCell, _
                          LookIn:=xlFormulas2, _
                          LookAt:=xlPart, _
                          SearchOrder:=xlByRows, _
                          SearchDirection:=xlNext, _
                          MatchCase:=False, _
                          SearchFormat:=False)
   
      If Not RA Is Nothing Then
   
         RA.Activate
      
         With Selection.Interior
            .Pattern = xlSolid
            .PatternColorIndex = xlAutomatic
            .Color = 15773696
            .TintAndShade = 0
            .PatternTintAndShade = 0
         End With
      
         ActiveCell.Offset(0, 13).Select
         ActiveCell.Value = [NewValue]
      
         With Selection.Interior
            .Pattern = xlSolid
            .PatternColorIndex = xlAutomatic
            .Color = 15773696
            .TintAndShade = 0
            .PatternTintAndShade = 0
         End With
   
      End If
 
   Next i

End Sub
 
Upvote 0
EDIT: Moderator added spaces to beginning and end of [ code ] variable, as it was conflicting with code tags used to post the code.
I believe that if you use VBA code tags instead of vanilla CODE tags this problem won't occur. Took me a while to figure it out.

VBA Code:
      [Number] = i
   
      CodetoChange = [code]
      NewValueReplace = [NewValue]
 
Upvote 0
Hi and welcome to MrExcel.

Try the following code a bit improved:

VBA Code:
Sub CodeChange()
  Dim RA As Range
  Dim i As Long
  
  Application.ScreenUpdating = False
  For i = [Run_Start] To [Run_End]
    [Number] = i
    
    Set RA = Cells.Find([ code ], , xlFormulas, xlPart, xlByRows, xlNext, False)
    If Not RA Is Nothing Then
      With Union(RA, RA.Offset(0, 13)).Interior
        .Pattern = xlSolid
        .PatternColorIndex = xlAutomatic
        .Color = 15773696
        .TintAndShade = 0
        .PatternTintAndShade = 0
      End With
      RA.Offset(0, 13).Value = [NewValue]
    End If
  Next i
  Application.ScreenUpdating = True
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,223,911
Messages
6,175,324
Members
452,635
Latest member
laura12345

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