John Doe
John Doe

Reputation: 31

excel vba script wont execute the intended msgbox?

I have an excel vba script (below) that I'm having trouble with displaying "match". I set my excel file up with the following information. column A5 to A10 has dates in the format of 3/1/2015, 3/2/2015, etc. (so A5 = 3/1/2015, A6 = 3/2/2015, etc.)

My worksheet has 7 tabs, the first (with the code on it) is the default sheet1. the 2nd through the 7th tab are labeled just as "1" for the 2nd tab, "2", "3", etc to "7". For now I was just testing out A5 to match and display something if it matches tab 2, but isn't working. The commented (out) msgboxes shows that the values should match, but when I try to set an if (they are equal), no bueno. this is a simplified version, so an answer like "why don't you just set the day equal to the day you want" wont work. Thanks!

Sub main()

'MsgBox Day(Worksheets("sheet1").Range("a5").Value)

'MsgBox Worksheets(2).Name

If Day(Worksheets("sheet1").Range("a5").Value) = Worksheets(2).Name Then

MsgBox "match"

End If

End Sub

Upvotes: 2

Views: 111

Answers (1)

BrakNicku
BrakNicku

Reputation: 5991

Day function returns an integer.

Worksheets(2).Name is a string property.

To compare those values, you have to convert them to the same type.

If CStr(Day(Worksheets("sheet1").Range("a5").Value)) = Worksheets(2).Name Then

You can read more about type conversion functions here

Upvotes: 2

Related Questions