How to make my VBA command run on several worksheets

teksiong

New Member
Joined
Aug 10, 2016
Messages
1
Hi,

I have one workbook with several worksheets, however when i run it, it only run on one worksheet. Can anyone help me to make this code also run for other worksheets in the same workbook?

Here is the code:

Private Sub Workbook_Open()
Dim LRow As Long
Dim LName As String
Dim LResponse As String
Dim LResponse1 As String
Dim LDiff As Long
Dim LDays As Long

LRow = 2 'start at row 2
LDays = 90 'Warning - Number of days to check for expiration

With Sheets("SIP01 Crew")
'Check the first 100 rows in column I
While LRow < 100
'Only check for expired certificate if value in column I is not blank
If IsDate(.Range("I" & LRow)) Then
LDiff = .Range("I" & LRow).Value2 - Date
If (LDiff >= 0) And (LDiff <= LDays) Then
'Get names
LName = .Range("B" & LRow).Value
LName1 = .Range("C" & LRow).Value
LName2 = .Range("E" & LRow).Value
LName3 = .Range("G" & LRow).Value
LResponse = LResponse & LName & " " & LName1 & " " & LName2 & " " & LName3 & " " & " will expire in " & LDiff & " days." & Chr(10)
End If
If (LDiff <= 0) Then
'Get names
LName = .Range("B" & LRow).Value
LName1 = .Range("C" & LRow).Value
LName2 = .Range("E" & LRow).Value
LName3 = .Range("G" & LRow).Value
LResponse = LResponse & LName & " " & LName1 & " " & LName2 & " " & LName3 & " " & " already expired for " & LDiff & " days." & Chr(10)
End If
End If
LRow = LRow + 1
Wend
If CBool(Len(LResponse)) Then _
MsgBox "These Certificate(s) are already due or nearing expiration:" & Chr(10) & LResponse, vbCritical, "Warning"
End With
End Sub

and i wish the the message box pop up for each worksheet.

Hopefully someone can help me

Agus
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
Try this:


Code:
Private Sub Workbook_Open()
Dim sh  As Worksheet
Dim LRow As Long
Dim LName As String
Dim LResponse As String
Dim LResponse1 As String
Dim LDiff As Long
Dim LDays As Long

LRow = 2 'start at row 2
LDays = 90 'Warning - Number of days to check for expiration


For Each sh In ActiveWorkbook.Worksheets

    With sh
    'Check the first 100 rows in column I
        While LRow < 100
    'Only check for expired certificate if value in column I is not blank
            If IsDate(.Range("I" & LRow)) Then
            LDiff = .Range("I" & LRow).Value2 - Date
                If (LDiff >= 0) And (LDiff <= LDays) Then
                'Get names
                    LName = .Range("B" & LRow).Value
                    LName1 = .Range("C" & LRow).Value
                    LName2 = .Range("E" & LRow).Value
                    LName3 = .Range("G" & LRow).Value
                    LResponse = LResponse & LName & " " & LName1 & " " & LName2 & " " & LName3 & " " & " will expire in " & LDiff & " days." & Chr(10)
                End If
                If (LDiff <= 0) Then
                'Get names
                    LName = .Range("B" & LRow).Value
                    LName1 = .Range("C" & LRow).Value
                    LName2 = .Range("E" & LRow).Value
                    LName3 = .Range("G" & LRow).Value
                    LResponse = LResponse & LName & " " & LName1 & " " & LName2 & " " & LName3 & " " & " already expired for " & LDiff & " days." & Chr(10)
                End If
            End If
        LRow = LRow + 1
        Wend
        
        If CBool(Len(LResponse)) Then _
            MsgBox "These Certificate(s) are already due or nearing expiration:" & Chr(10) & LResponse, vbCritical, "Warning"
    End With
Next

End Sub
 
Upvote 0

Forum statistics

Threads
1,213,497
Messages
6,113,998
Members
448,539
Latest member
alex78

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