Multiple email attachements based on variable value

dcharland

New Member
Joined
Mar 2, 2011
Messages
40
I need some help!!
I declared 4 variables as string file1, file2, file3 and file4

then I assign the value of each cell to the respective value
file1 = Cells(9, "C").Value ' Set the first file to attache
file2 = Cells(11, "C").Value ' Set the Second file to attache
file3 = Cells(13, "C").Value ' Set the Third file to attache
file4 = Cells(15, "C").Value ' Set the Fourth file to attache

Now I'm trying to attached the files to an email as an attachment like this:
.Attachments.Add (file2)
.Attachments.Add (file2)
.Attachments.Add (file3)
.Attachments.Add (file4)

It works well as long as there is a value in each cell. But if I choose not to put a value in anyone of the cell it doesn't work.

What I would like to have is to check each file# to see if there is a value and if yes to attached it to the email, if it's empty then move to the next one.

I tried:
For num = 1 To 4
filenum = file & num
If filenum <> "" Then
.Attachments.Add (CStr(file & num))
MsgBox (CStr(file & num)) ' just to debug
Else
End If
Next num

But i'm sure you guessed that it doesn't work as the (CStr(file & num)) return the file1 and not the actual path of the file.

Thanks in advance for your help! :)
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Try something like:
Code:
Dim row As Long
For row = 9 To 15 Step 2
    If Cells(row, "C").Value <> "" Then
        .Attachments.Add Cells(row, "C").Value
    End If
Next
 
Upvote 0
John_w,

Thanks it works perfectly....really appreciate the help as this thing has been driving me crazy for a few hours (I'm not a programmer and know very little about vba)

Regards
 
Upvote 0

Forum statistics

Threads
1,224,534
Messages
6,179,390
Members
452,909
Latest member
VickiS

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