VBA to mail distribution

SupremeDr

New Member
Joined
Aug 15, 2019
Messages
34
Hello
I a working on a program to email invoices to a distribution list for my team.

The program filters by the reps BillingCode then by if the group gets emails or online notifcatons. It then runs a loop based on a subcount on the workbook (in B2).

The filters are working correctly but when the loop run it acts as if it's unfiltered.

Code:
Option Explicit

Sub DraftOutlookEmails()
    
'*******************************************************************
'Microsoft Outlook XX.X Object Library is required to run this code
'*******************************************************************

Dim objOutlook As Outlook.Application
Dim Mail As Outlook.MailItem
Dim lCounter As Long
Set objOutlook = Outlook.Application

    Rows("3:3").Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
    
Dim wsMLOG As Excel.Worksheet
Set wsMLOG = Sheets("Master Log")
Dim rngTable1 As Excel.Range
Set rngTable1 = wsMLOG.Range("A4").CurrentRegion
Dim rngLessHeader As Excel.Range, rngRows As Variant, rngRow1 As Long
Set rngLessHeader = gf_rngGet_TableData_Range(rngTable1)
Dim BACode As Variant
BACode = InputBox("Enter BA Code")

    rngTable1.AutoFilter Field:=5, Criteria1:=BACode
    rngTable1.AutoFilter Field:=7, Criteria1:="E-mail"
    
Set rngTable1 = wsMLOG.Range("A4").CurrentRegion
Set rngLessHeader = gf_rngGet_TableData_Range(rngTable1)
rngRows = wsMLOG.Range("B2")
rngRow1 = rngLessHeader.Rows(1).Count


Dim i As Long

    Do Until i = rngRows
    
        Set Mail = objOutlook.CreateItem(olMailItem)
        Mail.To = wsMLOG.Range("L" & i + 5).Value & ";" & wsMLOG.Range("M" & i + 5).Value & ";" & wsMLOG.Range("N" & i + 5).Value & ";" & wsMLOG.Range("O" & i + 5).Value & ";" & wsMLOG.Range("P" & i + 5).Value & ";" & wsMLOG.Range("Q" & i + 5).Value & ";" & wsMLOG.Range("R" & i + 5).Value & ";" & wsMLOG.Range("S" & i + 5).Value
        Mail.Subject = "Hello There"
        Mail.Body = "Hello There"
        Mail.Close (olSave)
        Set Mail = Nothing
        
     i = i + 1
        
     Loop
    
End Sub
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
Try this

Code:
  Do Until i = rngRows
[COLOR=#ff0000]    If Range("A" & i + 5).EntireRow.Hidden = False Then[/COLOR]
      Set Mail = objOutlook.CreateItem(olMailItem)
      Mail.To = wsMLOG.Range("L" & i + 5).Value & ";" & wsMLOG.Range("M" & i + 5).Value & ";" & wsMLOG.Range("N" & i + 5).Value & ";" & wsMLOG.Range("O" & i + 5).Value & ";" & wsMLOG.Range("P" & i + 5).Value & ";" & wsMLOG.Range("Q" & i + 5).Value & ";" & wsMLOG.Range("R" & i + 5).Value & ";" & wsMLOG.Range("S" & i + 5).Value
      Mail.Subject = "Hello There"
      Mail.Body = "Hello There"
      Mail.Close (olSave)
      Set Mail = Nothing
[COLOR=#ff0000]    End If[/COLOR]
    i = i + 1
  Loop
 
Upvote 0

Forum statistics

Threads
1,214,819
Messages
6,121,739
Members
449,050
Latest member
excelknuckles

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