Excel Exits Macro on Font Color Change Line

ransomedbyfire

Board Regular
Joined
Mar 9, 2011
Messages
121
I have a macro that contains the following within a longer loop:
Rich (BB code):
If InStr(Cells(Row, Column), "-") Then
    With Cells(Row, Column).Font
    .ColorIndex = 3
    End With
Else: With Cells(Row, Column).Font
    .ColorIndex = 0
    End With
End If
For some reason, the second time Excel hits the line in bold, the macro stops and starts over from the beginning; thus, it never loops past the second line or so. The only error handler I have sends the code to a msgbox line; so, I don't think it's that.

What could be causing this, and how can I fix it?

Thanks!
 
Last edited:

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
1st, I recommend NOT using Row and Column as variable names.
It's not good to use VBA built in properties as variables.

That said, perhaps

Rich (BB code):
With Cells(MyRow, MyColumn)
    If Instr(.Value, "-") >0 Then
        .Font.ColorIndex = 3
    Else
        .Font.Colorindex = 0
    End If
End With
 
Upvote 0
Rich (BB code):
With Cells(MyRow, MyColumn)
    If Instr(.Value, "-") >0 Then
        .Font.ColorIndex = 3
    Else
        .Font.Colorindex = 0
    End If
End With
Please allow me to apologize in advance... the only excuse I can give you for what follows is "I'm bored!" :eek:

The code snippet you posted above can be replaced by anyone of the following alternatives (I'm sure there are others as well)...

Code:
With Cells(MyRow, MyColumn)
  .Font.ColorIndex = 3 * UBound(Split(.Value, "-", 2))
End With
Code:
With Cells(MyRow, MyColumn)
  .Font.ColorIndex = -3 * (InStr(.Value, "-") > 0)
End With
Code:
With Cells(MyRow, MyColumn)
  .Font.ColorIndex = Choose(1 - (InStr(.Value, "-") > 0), 0, 3)
End With
Code:
With Cells(MyRow, MyColumn)
  .Font.ColorIndex = 3 - 3 * (InStr(.Value, "-") = 0)
End With
Code:
With Cells(MyRow, MyColumn)
  .Font.ColorIndex = Mid("03", 1 - (InStr(.Value, "-") > 0), 1)
End With
Code:
With Cells(MyRow, MyColumn)
  .Font.ColorIndex = -3 * (.Value Like "*-*")
End With
Code:
With Cells(MyRow, MyColumn)
  .Font.ColorIndex = Choose(1 - (.Value Like "*-*"), 0, 3)
End With
Code:
With Cells(MyRow, MyColumn)
  .Font.ColorIndex = 3 - 3 * (Not .Value Like "*-*")
End With
Code:
With Cells(MyRow, MyColumn)
  .Font.ColorIndex = Mid("03", 1 - (.Value Like "*-*"), 1)
End With

Each of these was chosen because they are easily converted in one-liners by removing the With..End With block and distributing the Cells object to the dotted properties.
 
Upvote 0
Thanks, poster1. I tried what you suggested but am still having the problem. Poster2, thanks for the suggestions. I will have to come back to this when I have more time; but it looks like it could definitely make my code more efficient. I tried eliminating the with statement because I know it seems unnecessary; but for some reason, the computer I am writing this macro for doesn't like the one-liner. It has Excel 2003 and a few other weird issues.
 
Upvote 0
It sounds like your problem might be in another section of your code then.

Here is how to restructure my first alternative to eliminate the With..End With block...

Code:
Cells(MyRow, MyColumn).Font.ColorIndex = 3 * UBound(Split(Cells(MyRow, MyColumn).Value, "-", 2))
Note that all I did was put Cells(MyRow, MyColumn) in front of each property that starts with a 'dot'.
 
Upvote 0
If it's still not working, we'll need to see the whole code.

And of coarse the whole thing can be replaced with NO vba code at all..
Just using a simple conditional formatting formula

=SEARCH("-",A1)
 
Upvote 0

Forum statistics

Threads
1,224,503
Messages
6,179,134
Members
452,890
Latest member
Nikhil Ramesh

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