Excel VBA IF within If Question

robcor858

New Member
Joined
Feb 28, 2017
Messages
4
Hello,

I am trying to write a VBA that selects (from the ActiveWorkbook) each sheet that is not named "Engine" Or "Runs". After the sheet is selected, the code will find the word "Total" within my range and will highlight the row from column A:P and go to the next sheet not named "Engine" or "Runs" and perform the same function. I can get the code to work to do one or the other, but not both. If anyone out there knows what I am doing wrong and how to fix it, I would greatly appreciate the help.

Sub Find_Total_Cells()
Dim myRange As Range
Dim myCell As Range
Set myRange = Range("A6:A1000")
For Each myCell In myRange
Dim Sht As Worksheet 'new
For Each Sht In ActiveWorkbook.Worksheets 'new




If myCell Like "*Total*" And Sht.Name <> "Runs" Or Sht.Name <> "Engine" Then
Sht.Select
myCell.EntireRow.Font.Bold = True
myCell.Columns("D:P").Value = "Sum Formula Needed"
With myCell.Columns("A:P").Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorAccent4
.TintAndShade = 0.599993896298105
.PatternTintAndShade = 0
End With
End If


Next myCell
Next Sht


End Sub
 
untested, but does this make it work??

Dave

If myCell Like ("*Total*" And Sht.Name <> "Runs") Or myCell Like ("*Total*" And Sht.Name <> "Engine") Then
 
Upvote 0
ok again untested.

Code:
If (Sht.Name <> "Runs" Or Sht.Name <> "Engine") And myCell Like "*Total*" Then

?

Dave
 
Upvote 0
Can you please use code tags when posting code !
If your code was indented it would be easier to read AND debug

This will do what you want

Code:
Sub Find_Total_Cells()
Dim myRange As Range, Sht As Worksheet, myCell As Range
For Each Sht In Worksheets 'new
    If Sht.Name <> "Runs" And Sht.Name <> "Engine" Then
        Sht.Activate
        Set myRange = Range("A6:A1000")
        For Each myCell In myRange
            If myCell Like "*Total*" Then
                myCell.EntireRow.Font.Bold = True
                myCell.Columns("D:P").Value = "Sum Formula Needed"
                    With myCell.Columns("A:P").Interior
                        .Pattern = xlSolid
                        .PatternColorIndex = xlAutomatic
                        .ThemeColor = xlThemeColorAccent4
                        .TintAndShade = 0.599993896298105
                        .PatternTintAndShade = 0
                    End With
            End If
        Next myCell
    End If
Next Sht
End Sub
 
Upvote 0
@SQUIDD
Dave, using OR will select every sheet regardless of name in this case......you need to use AND instead.
And there is no need for the brackets...:beerchug:
Code:
If (Sht.Name <> "Runs" Or Sht.Name <> "Engine")
 
Upvote 0

Forum statistics

Threads
1,226,850
Messages
6,193,334
Members
453,790
Latest member
yassinosnoo1

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