VBA returns wrong date string

Asked

Viewed 403 times

2

I made a code to return a string from the first day of the month acts:

Dia = "1/" & CStr(Month(Now)) & "/" & CStr(Year(Now))

then I glue it to a cell

Cells(rowx,colx).value = Dia

the value pasted change of position the day with the month, for some reason... Is there any other way to get this number, it return "1/2/2016"?

  • 1

    Tried to Cells(rowx, coly).NumberFormat = "dd/mm/aaaa" ?

  • tried, managed to solve otherwise, I had paste only the month and year, when it is pasted in excel it puts the 1/ in front alone

  • I got it. Post your solution as an answer to help the next victim. :)

  • First of all, are you sure there isn’t an error in your code? I say this because a function you use seems wrong (you are years, but I think it should be year). Second, the reason for automated choice is the Excel language you use. In American English the month is displayed first on a date. Here, in my Excel in Portuguese, your code (with year), works properly and displays: 1/2/2016.

  • I quickly redacted the code here, and I’m dyslexic, I didn’t realize I’d put the s there, I’ll edit to remove

1 answer

1


I got it with a gambiarra, by putting the variable only as

Dia = CStr(Month(Now)) & "/" & CStr(Year(Now))

excel places the date with cell formatting and assigned the correct value

Browser other questions tagged

You are not signed in. Login or sign up in order to post.