Run code only on Tabs containg Keyword

chungym

New Member
Joined
Jun 27, 2014
Messages
3
Hi,

I'm trying to learn VBA, and I'm getting stuck on the code that I have. Sorry that it is really amateur. I'd like to run this code that changes all blank cells to zeros in a specific range only on the worksheets who have a specific keyword in the tab name - I'd like the code to run on all tabs containing the word "Finance" as part of the worksheet name. What I have so far only works on the active sheet. It doesn't go through the entire workbook.

Code:
Sub AddZero2()
Dim cell As Range
Dim wks As Excel.Worksheet
For Each wks In ThisWorkbook.Worksheets
    If InStr(wks.Name, "Finance") <> 0 Then
    For Each cell In Range("E87:AJ98")
        If Len(cell.Value) = 0 Then
        cell.Value = 0
        End If
    Next cell
 End If
Next wks
End Sub

any help would be appreciated...
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
Try,

Code:
Sub AddZero2()
Dim cell As Range
Dim wks As Excel.Worksheet
For Each wks In ThisWorkbook.Worksheets
[COLOR=#ff0000]    wks.Select[/COLOR]
    If InStr([COLOR=#ff0000]LCase([/COLOR]wks.Name[COLOR=#ff0000])[/COLOR], "finance") <> 0 Then
    For Each cell In Range("E87:AJ98")
        If Len(cell.Value) = 0 Then
        cell.Value = 0
        End If
    Next cell
 End If
Next wks
End Sub
 
Upvote 0
Your inner For-Loop wasn't searching through each worksheet. Try:
Code:
Sub AddZero2()

Dim cell As Range
Dim wks As Worksheet

Application.ScreenUpdating = False

For Each wks In ActiveWorkbook.Sheets
    With wks
        If InStr(.name, "Finance") Then
            For Each cell In .Range("E87:AJ98")
                If Lenb(cell.Value) = 0 Then cell.Value = 0
            Next cell
        End If
    End With
Next wks

Application.ScreenUpdating = True

End Sub
 
Last edited:
Upvote 0
This version removes the inner loop so should run faster:
Code:
Sub AddZero2()

Dim cell As Range
Dim wks As Worksheet

Application.ScreenUpdating = False

For Each wks In ActiveWorkbook.Sheets
    With wks
        If InStr(.name, "Finance") Then
            On Error Resume Next
            .Range("E87:AJ98").SpecialCells(xlCellTypeBlanks).Value = 0           
           On Error Goto 0
        End If
    End With
Next wks

Application.ScreenUpdating = True

End Sub
 
Upvote 0
That worked perfectly! Thank you so much!!!



This version removes the inner loop so should run faster:
Code:
Sub AddZero2()

Dim cell As Range
Dim wks As Worksheet

Application.ScreenUpdating = False

For Each wks In ActiveWorkbook.Sheets
    With wks
        If InStr(.name, "Finance") Then
            On Error Resume Next
            .Range("E87:AJ98").SpecialCells(xlCellTypeBlanks).Value = 0           
           On Error Goto 0
        End If
    End With
Next wks

Application.ScreenUpdating = True

End Sub
 
Upvote 0

Forum statistics

Threads
1,215,491
Messages
6,125,099
Members
449,205
Latest member
ralemanygarcia

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