0
votes

I am new to this forum and also new to programming. I am trying to create a If condition in VBA where if the excel file has the required value then the file should get attached with a specific email or else no file should be attached ( in case the cell value is blank) and a different email should appear in the Email body.

I am trying to use "NULL" to represent the blank cell value. Is it correct?

Sample of my code -

if sheets("Hello").range("A2").value = Null, then .attachments.remove else .attachments.add "C:\filename.xlsx End if

1
No, you should use either If Worksheets("Hello").Range("A2").Value = "" Then or, more appropriately, If IsEmpty(Worksheets("Hello").Range("A2").Value) Then - YowE3K
Thanks a lot for your help.. :) I used If IsEmpty(Worksheets("Hello").Range("A2").Value) Then and it worked perfectly. Sorry not sure how to vote in the comments provide by you.. so wrote a new comment thanking for your help.. :D - Indira

1 Answers

0
votes
If Sheets("Hello").Range("A2").Value = Null Then

is not the correct way to check for an empty cell.

One of the ways to test for an empty cell would be:

If Worksheets("Hello").Range("A2").Value = "" Then

which tests whether the cell, when converted to a String, is an empty string. (An equivalent statement to that would be

If Worksheets("Hello").Range("A2").Value = vbNullString Then

which can save you a few bytes of memory.)

A better way to test would be to use the actual IsEmpty function:

If IsEmpty(Worksheets("Hello").Range("A2").Value) Then

which tests to see whether the cell's value has a data type of Variant/Empty.