loop through worksheets

ummjay

Board Regular
Joined
Oct 1, 2010
Messages
193
how can i loop through worksheets within a workbook. I want to exclude the first two worksheets (RUN, and Results) Tried my loop below, but seems to only be looping on sheet1 ("Run").

Code:
Sub GOEmail()
Dim myOlApp As Object 'Outlook.Application
Dim MyItem As Object 'Outlook.MailItem
Dim strBody As String
Dim strLink As String
Dim strLinkText As String
Dim strNewText As String
Dim strCurrentTime As String
Dim strNewTime As String
Dim strTimeZone As String
Dim strSenderTarget As String
Dim strCompany As String
Dim strToEmails As String
Dim strblank As String
Dim celHome As Range, rg As Range, rgCopy As Range
Dim iCol As Long, n As Long
Dim r As Range
Dim rng As Range
Dim wksTemp As Worksheet
Dim msgDoc As Object 'outlook wordprocessing editor


    Set myOlApp = CreateObject("Outlook.Application")
    
    'start to loop
    
        
        Set wksTemp = ActiveSheet
        
        Set rng = Range("B1", Range("B" & Rows.Count).End(xlUp))

        For Each r In rng   'loop through each work effort
        
            Set MyItem = myOlApp.CreateItem(olMailItem)
            With MyItem
                .CC = "ecsfutglobal"
                .BCC = ""
                strBody = .HTMLBody
        
                'STRING VALUES
                strSenderTarget = Cells(r.Row, "C")
                strCurrentTime = Cells(r.Row, "F")
                strCompany = Cells(r.Row, "B")
                strTimeZone = Cells(r.Row, "G")
        
                'What to Replace
                .HTMLBody = "body of text"
                    
                .Subject = "subj"
                .SentOnBehalfOfName = "addy"
        
                    .To = r.Value
                    .Importance = 2
                    Set msgDoc = MyItem.GetInspector.WordEditor
        msgDoc.Select
        msgDoc.Windows(1).Selection.Copy
        
        MyItem.BodyFormat = olFormatRichText
        
        msgDoc.Range.Paste

                    .Display
                    '.BodyFormat = 3
            End With
            Wait
        Next r

    Set MyItem = Nothing
    Set myOlApp = Nothing
    
    Worksheets("RUN").Activate
    MsgBox "Macro Completed. All Emails composed for review", vbOKOnly, "Macro Completed"
    
    Application.DisplayAlerts = False
    Application.DisplayAlerts = True


End Sub
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
Maybe

Code:
Sub GOEmail()
Dim myOlApp As Object 'Outlook.Application
Dim MyItem As Object 'Outlook.MailItem
Dim strBody As String
Dim strLink As String
Dim strLinkText As String
Dim strNewText As String
Dim strCurrentTime As String
Dim strNewTime As String
Dim strTimeZone As String
Dim strSenderTarget As String
Dim strCompany As String
Dim strToEmails As String
Dim strblank As String
Dim celHome As Range, rg As Range, rgCopy As Range
Dim iCol As Long, n As Long
Dim r As Range
Dim rng As Range
Dim wksTemp As Worksheet
Dim msgDoc As Object 'outlook wordprocessing editor
Dim i As Long


    Set myOlApp = CreateObject("Outlook.Application")
    
    'start to loop
    
For i = 3 To Worksheets.Count
        Set wksTemp = Worksheets(i)
        With wksTemp
        
        Set rng = .Range("B1", Range("B" & Rows.Count).End(xlUp))

        For Each r In rng   'loop through each work effort
        
            Set MyItem = myOlApp.CreateItem(olMailItem)
            With MyItem
                .CC = "ecsfutglobal"
                .BCC = ""
                strBody = .HTMLBody
        
                'STRING VALUES
                strSenderTarget = .Cells(r.Row, "C")
                strCurrentTime = .Cells(r.Row, "F")
                strCompany = .Cells(r.Row, "B")
                strTimeZone = .Cells(r.Row, "G")
        
                'What to Replace
                .HTMLBody = "body of text"
                    
                .Subject = "subj"
                .SentOnBehalfOfName = "addy"
        
                    .to = r.Value
                    .Importance = 2
                    Set msgDoc = MyItem.GetInspector.WordEditor
        msgDoc.Select
        msgDoc.Windows(1).Selection.Copy
        
        MyItem.BodyFormat = olFormatRichText
        
        msgDoc.Range.Paste

                    .Display
                    '.BodyFormat = 3
            End With
            Wait
        Next r
    End With
    Next i

    Set MyItem = Nothing
    Set myOlApp = Nothing
    
    Worksheets("RUN").Activate
    MsgBox "Macro Completed. All Emails composed for review", vbOKOnly, "Macro Completed"
    
    Application.DisplayAlerts = False
    Application.DisplayAlerts = True


End Sub
 
Upvote 0

Forum statistics

Threads
1,214,601
Messages
6,120,460
Members
448,965
Latest member
grijken

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