ID .To Address

princejay22

New Member
Joined
May 5, 2016
Messages
1
I have been working on this for a while now. I am trying to get a code where I do not have to keep editing the .to address to send an email through excel. I have a form on excel I fill out for work. I have inserted a command button on the form, so it can automatically send an email once I complete it. I have a database of emails on another workbook, and they all have IDs. I will like to place the ID on the form to indicate what email address it need to be sent to.

As of now, I always have to go in a change the .to adding the email addresses. I am tired of doing that. Is there any way to use my IDs to locate the email addresses on my database?
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
You would have to have code that tells VBA to go to the list of IDs in the other workbook, match the one you put on the form, then put the corresponding address in email as: .to = [address], with the address being either a variable representing the address of a reference to the workbook.Sheet.Range.Value where the address resides. I don't know if your form is a group of worksheet cells or a UserForm with textboxes, but assuming that it is a group of cells, and that you add cell G10 as the ID number and your IDs are listed in Workbook2, Sheet1, Column A, with addresses in column B, then the code would look something like this.
Code:
Dim fn As Range, wb As Workbook, sh As worksheet
Set wb = Workbooks(2) 'assumes it is already open
Set sh = wb.Sheets(1)
Set fn = sh.Range("A:A").Find(ActiveSheet.Range("G10").Value, , xlValues, xlWhole)
	If Not fn Is Nothing Then
		'Your With statement for email reference here
			.to = fn.Offset(, 1).Value
                'etc.
               'End With
	End If

The location of the If...Then statement lines can be varied depending on how your other code is written.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,250
Messages
6,171,036
Members
452,374
Latest member
keccles

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