LookuUp In VBA

karldugan

New Member
Joined
May 10, 2016
Messages
42
Hi all,

I have the below code, currently I have hard coded the email to address but I have a list on a "Variables" tab, range J2:K10 (also below) with where the report should go. Rather than updating the code, I was wondering if I could lookup to this list and return the right to person.

Thanks in advance - Karl


'Email report code
Const SCC = "DIRECTORY REMOVED"

'Turn off screen updating
Application.ScreenUpdating = False
'Make a copy of the active sheet and save it to a temporary file
'Selects the SCC sheet
Sheets("SCC").Select
ActiveSheet.Copy
Cells.Copy
Cells.PasteSpecial xlPasteValues

Set wb = ActiveWorkbook
Application.DisplayAlerts = False
wb.SaveAs SCC
Application.DisplayAlerts = True

'Create and show the outlook mail item
Set oMail = CreateObject("Outlook.Application").CreateItem(0)
With oMail 'Add a recipient
.To = "joe.bloggs@example.com"
.Subject = "SCC Report"
.Body = "Please find attached your report."
.Attachments.Add wb.FullName
.send
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
Hey

Can I just check how this runs overall please? AM I correct in saying that during the first cycle SCC is selected, all the variables in the code snippet refering to SCC (including the to email address) refer to that cycle, then once this is complete it moves onto NIR and everything that was SCC is now NIR...rinse and repeat until ENF?

Thanks
Dave
 
Upvote 0
Hey

Can I just check how this runs overall please? AM I correct in saying that during the first cycle SCC is selected, all the variables in the code snippet refering to SCC (including the to email address) refer to that cycle, then once this is complete it moves onto NIR and everything that was SCC is now NIR...rinse and repeat until ENF?

Thanks
Dave
Hi Dave,

Spot on - code is repeated for each category.

Thanks,
Karl
 
Upvote 0
In this case I would add a third column with the report name, at present you have this as CONST but that seems like you're going to repeat an awful lot.

1697827955275.png


This code will run through each line, one after the other and generate the report

VBA Code:
Sub KarlDugan()
Dim rptRng As Range
Dim rptCode As String
Dim rptTo As String
Dim rptName As String
'add your variables for OMail here

For Each rptRng In Sheet1.Range("J2:J10")

'Email report code, email to and name
rptCode = rptRng.Value 'report code from column J
rptTo = rptRng.Offset(, 1).Value ' email address from column K
rptName = rptRng.Offset(, 2).Value ' report name (formerly CONST) from L

'Turn off screen updating
Application.ScreenUpdating = False
Application.DisplayAlerts = False

'Make a copy of the active sheet and save it to a temporary file
'Selects the SCC sheet
Sheets(rptCode).Copy
Set wb = ActiveWorkbook
With wb
    With .Sheets(1).Cells
        .Value = .Value
    End With
    .SaveAs rptName
    rptName = .FullName
End With
Application.DisplayAlerts = True

'Create and show the outlook mail item
Set oMail = CreateObject("Outlook.Application").CreateItem(0)
With oMail 'Add a recipient
    .To = rptTo
    .Subject = rptCode & " Report"
    .Body = "Please find attached your report."
    .Attachments.Add rptName
    .send
End With
End Sub
 
Upvote 0
In this case I would add a third column with the report name, at present you have this as CONST but that seems like you're going to repeat an awful lot.

View attachment 100753

This code will run through each line, one after the other and generate the report

VBA Code:
Sub KarlDugan()
Dim rptRng As Range
Dim rptCode As String
Dim rptTo As String
Dim rptName As String
'add your variables for OMail here

For Each rptRng In Sheet1.Range("J2:J10")

'Email report code, email to and name
rptCode = rptRng.Value 'report code from column J
rptTo = rptRng.Offset(, 1).Value ' email address from column K
rptName = rptRng.Offset(, 2).Value ' report name (formerly CONST) from L

'Turn off screen updating
Application.ScreenUpdating = False
Application.DisplayAlerts = False

'Make a copy of the active sheet and save it to a temporary file
'Selects the SCC sheet
Sheets(rptCode).Copy
Set wb = ActiveWorkbook
With wb
    With .Sheets(1).Cells
        .Value = .Value
    End With
    .SaveAs rptName
    rptName = .FullName
End With
Application.DisplayAlerts = True

'Create and show the outlook mail item
Set oMail = CreateObject("Outlook.Application").CreateItem(0)
With oMail 'Add a recipient
    .To = rptTo
    .Subject = rptCode & " Report"
    .Body = "Please find attached your report."
    .Attachments.Add rptName
    .send
End With
End Sub
Hi Dave,

Thanks for the above & help so far, when I run, I am getting a compile error that says "For without Next"

Karl
 
Upvote 0
Change the end two lines to these three

VBA Code:
End With
Next rptRng
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,068
Messages
6,122,950
Members
449,095
Latest member
nmaske

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