Formula in worksheet and VBA works differently

Viewed 641

Cell A1 contains the number 25, which is right-aligned, implying it's a number, not text.

D1 contains the formula:

="" & A1

The 25 in D1 is left-aligned, implying it's text. That is confirmed by the following formula, which returns 1:

=IF(D1="25",1,0)

The following VBA code puts 25 in F1 but the 25 is right-aligned and the IF test returns 0:

Range("F1") = "" & Range("A1")

Any explanation for the discrepancy?

E1 contains the following formula which gives a left-aligned 25 and the IF test returns 1:

TEXT(A1,"0")

However, the following code gives a right-aligned 25 and the IF test returns 0:

Range("F1") = Application.WorksheetFunction.Text(Range("A1"), "0")

Not that I have to use the TEXT function. I just wonder why it works differently than when in a worksheet.

Any rule that tells when or what worksheet functions won't work in VBA code, or more precisely, will give different results than when in worksheet?

4 Answers
Related