Hello,
i'm using the for each stamenent and offset for a macro to send via email different data ranges to vendors.
i am having trouble with the offset function:
after i extract the unique emails adress and the range from where they start to where they end (see the offset lines in code)
i would like to test if the coresponding value in column T is either 2template or 1template.
based on this i will send a different message.
i am using the offset function to test that in the code with an if but it doesn't seem to work. what am i missing?
i would really need your help to reach to a solution.
thank you in advance
Uploaded with ImageShack.us
like to test if the value from
i'm using the for each stamenent and offset for a macro to send via email different data ranges to vendors.
i am having trouble with the offset function:
after i extract the unique emails adress and the range from where they start to where they end (see the offset lines in code)
i would like to test if the coresponding value in column T is either 2template or 1template.
based on this i will send a different message.
i am using the offset function to test that in the code with an if but it doesn't seem to work. what am i missing?
i would really need your help to reach to a solution.
thank you in advance
Uploaded with ImageShack.us
Code:
Sub SendEmailRange()
With Application
.ScreenUpdating = False
.DisplayAlerts = False
.EnableEvents = False
End With
Dim cell As Range
Dim BankRng As Range
Dim HeaderBank As Range
Dim BankData As Range
Dim sumRng As Range
Dim LegalName As Range
Dim counter As Integer
Dim Outlook As Object ' Outlook.Application
Dim OutlookMsg As Object 'Outlook.MailItem
Set Outlook = CreateObject("Outlook.Application")
On Error Resume Next
With ActiveWorkbook.Sheets("Debit balances")
counter = .Cells(Rows.Count, "L").End(xlUp).Row
End With
'Clear range & filter the unique values
Range("O:T").ClearContents
Range("M1:M" & counter).AdvancedFilter xlFilterCopy, Range("O1:O" & counter), Range("O1"), Unique:=True
LR_negative = Range("O" & Rows.Count).End(xlUp).Row
LR_all = Range("D" & Rows.Count).End(xlUp).Row
'retrieves the coresponding template for each email adress
For p = 2 To LR_negative
If Range("O" & p) <> "" Then
Range("T" & p) = Application.VLookup(Cells(p, 15), Range("M2:N" & LR_all), 2, 0)
End If
Next p
'Delete the top row
Range("O1:T1").ClearContents
'add the "count" correspondent values for each cell that contains an email address
For Each cell In Columns("O").Cells.SpecialCells(xlCellTypeConstants)
If cell.Value Like "?*@?*.?*" Then
Set sumRng = Range("P1:P" & Cells(cell.Row, "P").Row - 1) 'change K1 to K2 if you have header row
cell.Offset(0, 1).Value = Application.WorksheetFunction.CountIf(Range("M1:M" & counter), Cells(cell.Row, "O").Value)
cell.Offset(0, 2).Value = Application.WorksheetFunction.Sum(sumRng) + 2
cell.Offset(0, 3).Value = Cells(cell.Row, "P").Value + (Cells(cell.Row, "Q").Value - 1)
cell.Offset(0, 4).Value = Application.WorksheetFunction.Sum(Range("K" & Cells(cell.Row, "Q").Value, "K" & Cells(cell.Row, "R").Value))
Set BankRng = Range(("A" & (Cells(cell.Row, "Q").Value)), ("K" & Cells(cell.Row, "R").Value))
Set HeaderBank = Range("A1:K1")
Set BankData = Union(HeaderBank, BankRng)
'Set LegalName = Range(("B" & (Cells(cell.Row, "Q").Value)))
'Create the email message for each line from column A
If Cells.Offset(0, 5).Value = "1tempalte" Then
Set OutlookMsg = Outlook.CreateItem(olMailItem)
With OutlookMsg
' set basic params
.Subject = "1tempalte"
.HTMLBody = "Sehr geehrte Damen und Herren, , " & "<br><br>" & _
" balance is :" & Cells(cell.Row, "S").Value & ". Da wir auch in der nächsten Zeit keine Rechnungen von Ihnen erwarten, " & _"<br><br>"
.To = cell.Value
.display
End With
Else
If Cells(0, 5).text = "2template" Then
With OutlookMsg
' set basic params
.Subject = "2template "
.HTMLBody = "Sehr geehrte Damen und Herren, , " & "<br><br>" & _
"auf unserer ; balance is :" & Cells(cell.Row, "S").Value & _
"asdfsdfs" & "<br>"
.To = cell.Value
.display
End With
End If
End If
End If
Next cell
End Sub