Hi there,
I'm trying to send an email through VBA and want the body message to reference a cell in sheet that is based on a dynamic row & column. For example, one email to reference cell B2 if the header row is named ABC and first column is named XYZ. Another email would reference cell D5 if header row is named DEF and first column named UVW. Below is the code that I'm stuck on. Can someone guide me in the right direction? Any help would be appreciated.
Sub mail2()
Dim OutApp As Object
Dim OutMail As Object
Dim cell As Range
Dim FindRow As Range
Dim FindColumn As Range
On Error GoTo cleanup
Set OutApp = CreateObject("Outlook.Application")
For Each cell In Sheets("Sheet1").Range("D1", Range("D1").End(xlDown))
i = cell.row
Set OutMail = OutApp.CreateItem(0)
On Error GoTo cleanup
With Sheets("Sheet2").Rows(1)
Set FindColumn = .Find(Sheets("Sheet1").Range("G" & i).Value, LookAt:=xlPart, MatchCase:=False, SearchFormat:=False)
End With
With Sheets("Sheet2").Columns(1)
Set FindRow = .Find(Sheets("Sheet1").Range("F" & i).Value, LookAt:=xlPart, MatchCase:=False, SearchFormat:=False)
End With
'Stuck after here... I want to get the row # from FindRow and column # from FindColumn in order to create a cell to reference the text in the body of the each email loop
On Error GoTo cleanup
With Application
.EnableEvents = False
.ScreenUpdating = False
End With
On Error Resume Next
With OutMail
.Display
.To = 'emailaddress
.Subject = Hello
.HTMLBody = 'want to add the cell reference here & .HTMLBody
.Send
End With
On Error GoTo 0
Set OutMail = Nothing
Next cell
cleanup:
Set OutApp = Nothing
Application.DisplayAlerts = False
Application.DisplayAlerts = True
With Application
.EnableEvents = True
.ScreenUpdating = True
End With
End Sub
I'm trying to send an email through VBA and want the body message to reference a cell in sheet that is based on a dynamic row & column. For example, one email to reference cell B2 if the header row is named ABC and first column is named XYZ. Another email would reference cell D5 if header row is named DEF and first column named UVW. Below is the code that I'm stuck on. Can someone guide me in the right direction? Any help would be appreciated.
Sub mail2()
Dim OutApp As Object
Dim OutMail As Object
Dim cell As Range
Dim FindRow As Range
Dim FindColumn As Range
On Error GoTo cleanup
Set OutApp = CreateObject("Outlook.Application")
For Each cell In Sheets("Sheet1").Range("D1", Range("D1").End(xlDown))
i = cell.row
Set OutMail = OutApp.CreateItem(0)
On Error GoTo cleanup
With Sheets("Sheet2").Rows(1)
Set FindColumn = .Find(Sheets("Sheet1").Range("G" & i).Value, LookAt:=xlPart, MatchCase:=False, SearchFormat:=False)
End With
With Sheets("Sheet2").Columns(1)
Set FindRow = .Find(Sheets("Sheet1").Range("F" & i).Value, LookAt:=xlPart, MatchCase:=False, SearchFormat:=False)
End With
'Stuck after here... I want to get the row # from FindRow and column # from FindColumn in order to create a cell to reference the text in the body of the each email loop
On Error GoTo cleanup
With Application
.EnableEvents = False
.ScreenUpdating = False
End With
On Error Resume Next
With OutMail
.Display
.To = 'emailaddress
.Subject = Hello
.HTMLBody = 'want to add the cell reference here & .HTMLBody
.Send
End With
On Error GoTo 0
Set OutMail = Nothing
Next cell
cleanup:
Set OutApp = Nothing
Application.DisplayAlerts = False
Application.DisplayAlerts = True
With Application
.EnableEvents = True
.ScreenUpdating = True
End With
End Sub