Looping & Selection + Loop

Benzula

Board Regular
Joined
Feb 28, 2014
Messages
248
Okay guys. I got a simple question, and a Complex question.

Simple. I need to run this Macro on every sheet in a workbook. I have it so it works on the current active sheet.

So I assume a simple Loop with this Macro would allow me to do it, how would I structure so that it goes to the next tab regardless of what it is called.

Here is the current code

Code:
Sub Send_Selection_Or_ActiveSheet_with_MailEnvelope()
'Working in Excel 2002-2013
    Dim Sendrng As Range
    
    On Error GoTo StopMacro


    With Application
        .ScreenUpdating = False
        .EnableEvents = False
    End With


    'Note: if the selection is one cell it will send the whole worksheet
    Set Sendrng = Selection


    'Create the mail and send it
    With Sendrng


        ActiveWorkbook.EnvelopeVisible = True
        With .Parent.MailEnvelope


            ' Set the optional introduction field thats adds
            ' some header text to the email body.
            .Introduction = "Good Afternoon, " & vbNewLine & vbNewLine & "Below you will find your accounts. 


            With .Item
                .To = ActiveSheet.Name
                .CC = ""
                .BCC = ""
                .Subject = "Please find below a list of all of your accounts."
                .Send
            End With


        End With
    End With


StopMacro:
    With Application
        .ScreenUpdating = True
        .EnableEvents = True
    End With
    ActiveWorkbook.EnvelopeVisible = False


End Sub


Okay! Now for the the more complex problem. Below is a sample (No Real Data)

If I have multiple rows of customers, they are all sorted by smallest to largest on MDM - Available.

What i need is to have the macro above run, but select the header +all rows that have negative MDM - Available. So if there were 3 more in addition to the one below with -10, -3 , 7. It would only select to send the first one + the ones with -10 and -3 with the header.

This is variable number of records per tab. So tab 2 could have 10 records with negative MDM - Available, and tab 3 could have 3.

Also If there are no records with negative numbers, I would want it to SKIP.

Again would need this to loop to each tab.

SalesForce IDCustomerCompliance NotesOpen OpportunitiesOpen Opp Device LevelAccount Executive - AccountMDM - ActiveMDM - PurchasedMDM- AvailableAW Email ClientAW CLAW BRAW WSSF Account StatusHosting Model
123456789dsaABC Corp TRUE-UP - ABC Corp15John Rhodes8670-160900ActiveAW SaaS

<colgroup><col><col><col><col><col><col><col><col><col><col><col><col><col><col><col></colgroup><tbody>
</tbody>
 
So stepping through the code shows me that it sends 4 emails based on 1 sheet all at once. It doesn't loop through the code 4 times.

So I think maybe in the previous bit of the macro it basically counts the number of sheets. and then since I'm on the Active sheet it sends that one 4 times.

I tried creating another sheet just as a test, and it sends 5 now to me. So the number of emails is based off the number of tabs there.

I don't see it change to the next worksheet.
 
Upvote 0

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
couple things:
1) using worksheet as a variable is going to be confusing. shorten it to WS.

For Each Worksheet In Worksheets

becomes

For Each ws In Worksheets

2) your loop will execute for each worksheet, but you need to activate it. Right after the "For Each" you need the line:

ws.Activate
 
Upvote 0

Forum statistics

Threads
1,214,912
Messages
6,122,200
Members
449,072
Latest member
DW Draft

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