Running Specified code in workbook except few worksheets

sekar

New Member
Joined
Feb 2, 2009
Messages
33
Office Version
  1. 2010
Platform
  1. Windows
Hi.,

I would like to run specified set of codes in each worksheet except few worksheets in the workbook.

Lets Say i have worksheets named: Data, Output. I wan run the macro code in the rest of the wok sheets.
How to do that
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
Try with IF statement, like this

working with activesheet
VBA Code:
If activesheet.name <> "Data" and activesheet.name <>"Output" then
...main code...

or loop through sheets
VBA Code:
For each ws in sheets
     If ws.name <> "Data" and ws.name <>"Output" then
            ...main code...
     End if
Next
 
Upvote 0
Try with IF statement, like this

working with activesheet
VBA Code:
If activesheet.name <> "Data" and activesheet.name <>"Output" then
...main code...

or loop through sheets
VBA Code:
For each ws in sheets
     If ws.name <> "Data" and ws.name <>"Output" then
            ...main code...
     End if
Next
The code for loop through the sheets not working. it updates the current sheet only with "HG".
Can i share the excel Sheet to check.
 
Upvote 0
The code for loop through the sheets not working. it updates the current sheet only with "HG".
Can i share the excel Sheet to check.
It depends what your code do.
In your code, try to replace activesheet by ws, may be.
If not, share the sample sheet and your full code.
 
Upvote 0
Sheet Link



The code as follows.

VBA Code:
Sub sekar()
    
For Each ws In Sheets

     If ws.Name <> "Data" And ws.Name <> "Sum" Then
    
      With Range("D7", Range("D" & Rows.Count).End(xlUp))
      .Offset(, 4).Value = Evaluate(Replace("if(isnumber(match(@,{""RHS_Panel"",""Top_Panel"",""LHS_Panel"",""Bottom_Panel""},0)),""HG"","""")", "@", .Address))
   End With
        
     End If
Next
  
End Sub
 
Upvote 0
With each ws you need to identify ws with range, otherwhile it belongs to activesheet:
Instead of:
VBA Code:
With Range("D7", Range("D" & Rows.Count).End(xlUp))

should be
VBA Code:
With ws.Range("D7",ws.Range("D" & Rows.Count).End(xlUp))
 
Upvote 0
Hi.,

Did you checked the output.

1664348366835.png


the HG to be filled in Next opposite to the Highlighted area in Green in HG column. The red one's are placed mistakenly.

Could you check it out please
 
Upvote 0
Try again with other MATCHJ function:
VBA Code:
Sub sekar()
For Each ws In Sheets
    If ws.Name <> "Data" And ws.Name <> "Sum" Then
        With ws.Range("D7", ws.Range("D" & Rows.Count).End(xlUp))
            .Offset(, 4).Formula = "=IF(ISNUMBER(MATCH(D7,{""RHS_Panel"",""Top_Panel"",""LHS_Panel"",""Bottom_Panel""},0)),H$6,"""")"
            .Value = .Value ' remove this line if you want cell formula, not hardcoded value
        End With
    End If
Next
End Sub
 
Upvote 0
Thanks bebo. works fine

One last thing, the formula still exist in the cell.. could you check that alone. even the .Value line is retained.

1664350302152.png
 
Upvote 0
OK, I forgot to use.offset for the 2nd line within With...End With
By the way, try this in shorter reference:

VBA Code:
Sub sekar()
For Each ws In Sheets
    If ws.Name <> "Data" And ws.Name <> "Sum" Then
        With ws.Range("H7:H" & ws.Range("D" & Rows.Count).End(xlUp).Row)
            .Formula = "=IF(ISNUMBER(MATCH(D7,{""RHS_Panel"",""Top_Panel"",""LHS_Panel"",""Bottom_Panel""},0)),H$6,"""")"
            .Value = .Value ' remove this line if you want cell formula, not hardcoded value
        End With
    End If
Next
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,215,097
Messages
6,123,077
Members
449,094
Latest member
mystic19

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