Regular Next witout For Errors; am I missing something?

steveh8204

Board Regular
Joined
Aug 20, 2018
Messages
151
A few times I've had this error when it always seems the case that I have a For statement. To make sure I'm not missing something I've stripped the code down to the bare bones but still getting it with the following code:

Code:
Sub mysearch()
Dim v As String, i As Integer


For i = 1 To 10
    v = Cells(i, 1)
If v = "Steve" Then
Cells(i, 1).Font = Bold

Next i
    
End Sub

How is there a no For statement error?

I'm relatively new to VBA and while I've had plenty of joy learning, struggling with the basics like this can be disheartening.

Is there something obvious I'm regularly likely to be missing that is causing this error.

If I put an Endif in before the Next it gets worse, I get a Runtime 438 error "Object doesnt support this property or method".
 
Last edited:

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
You're missing the End If, but also you are setting the font to bold incorrectly, it should be
Code:
Sub mysearch()
Dim v As String, i As Integer


For i = 1 To 10
   v = Cells(i, 1)
   If v = "Steve" Then
      Cells(i, 1).Font.Bold = True
   End If
Next i
    
End Sub
 
Upvote 0
Thanks, seems to have sorted it. Thought I'd set the Bold correctly earlier but must have got mixed up cutting and changing the code.

Root cause seems to be the EndIf statement. Do you always have to use this as I'm surely I regularly don't and get away with it.

Thanks again.
 
Upvote 0
You need the End If whenever the If statement is on more than one line. So you could use this instead
Code:
For i = 1 To 10
   v = Cells(i, 1)
   If v = "Steve" Then Cells(i, 1).Font.Bold = True
Next i
 
Upvote 0
Root cause seems to be the EndIf statement. Do you always have to use this as I'm surely I regularly don't and get away with it.
Rule is this:
- If you put the ENTIRE If ... Then on a single line of code, you do not need an End If, i.e.
Code:
[B]If [/B][I]some condition[/I] [B]Then[/B] [I]do something[/I]
- If you split up the If into multiple lines (many times you may want to do more than one thing), then you need an End If, i.e.
Code:
[B]If [/B][I]some condition[/I] [B]Then[/B] 
[I]    do something
[/I]    [I]do something[/I]
[B]End If[/B]
 
Upvote 0
You don't really need the IF here at all really, your comparison returns a TRUE/FALSE result which can be used in your .Font.Bold line:

Code:
Sub mysearch()
Dim i As Integer
For i = 1 To 10
    Cells(i, 1).Font.Bold = (Cells(i, 1) = "Steve")
Next i
End Sub
 
Last edited:
Upvote 0
Ah, that makes sense (about the one line thing) thanks for the replies.

Thanks too Scott for that little bit of code, handy to know.
 
Upvote 0
You don't really need the IF here at all really, ...
Actually, the OP doesn't even need a loop...
Code:
[table="width: 500"]
[tr]
	[td]Sub MakeSteveBold()
  Application.ReplaceFormat.Clear
  Application.ReplaceFormat.Font.Bold = True
  Range("A1:A10").Replace "Steve", "", SearchFormat:=False, ReplaceFormat:=True
  Application.ReplaceFormat.Clear
End Sub[/td]
[/tr]
[/table]
 
Last edited:
Upvote 0
Nice bit of code, just need a way to make "Steve" into a variable and the range Dynamic though as that's where I was heading.

Thanks though, much appreciated.
 
Upvote 0

Forum statistics

Threads
1,223,889
Messages
6,175,223
Members
452,620
Latest member
dsubash

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