0

The problem

I just want to the A2 cell to say Exam Mailers for (current month) (current year). Example: Exam Mailers for April 2020

if I do:

Range("a2").Value = "Exam Mailers for " + (months(i))

It works and I get

Exam Mailers for April

BUT if I do:

Range("a2").Value = "Exam Mailers for " + (months(i)) + " " + Year(Now)

it fails with

Run-time error `13`: Type mismatch

Here is the full code so far:

Sub datechange_1()
Dim months(11) As String
months(0) = "Janurary"
months(1) = "Feburary"
months(2) = "March"
months(3) = "April"
months(4) = "May"
months(5) = "June"
months(6) = "July"
months(7) = "August"
months(8) = "September"
months(9) = "October"
months(10) = "November"
months(11) = "December"

Dim nowMonth As Integer
nowMonth = Month(Now)

For i = 0 To 11
  If nowMonth = (i + 1) Then
     Range("a2").Value = "Exam Mailers for " + (months(i)) + " " + Year(Now)
  End If
Next
End Sub

Any ideas or suggests are appreciated. Thanks!

BigBen
  • 46,229
  • 7
  • 24
  • 40
  • 3
    change the `+` to `&` – Scott Craner Apr 21 '21 at 16:58
  • You can use + to concatenate two strings, but if one of the arguments is a numeric type, you'll get a type error because VB will interpret the + operator as numeric addition instead of string concatenation. – Nicholas Hunter Apr 21 '21 at 18:27
  • 2
    Which is why you should immediately just switch to `&` for all concatenation (repeating ad nauseam I know). – BigBen Apr 21 '21 at 18:34

1 Answers1

-1

May be just Str(Year(Now)). Looks like should work.

HaronDDC
  • 92
  • 3
  • OMG you can just Str() it like in python? hahaha that is awesome!! I am just learning vbscript so I don't know where to start. lol but now that I know it has Str() that will be awesome for me in the future. Thanks! –  Apr 21 '21 at 17:03
  • I have to wait 8 minutes to accept your answer! :( –  Apr 21 '21 at 17:03
  • 1
    `Str` in VBA is *not* the same as `str` in Python. `&` should be used to concatenate. – BigBen Apr 21 '21 at 17:04
  • 2
    @JatonJustice - don't use `Str`. Change the `+` to `&`. – BigBen Apr 21 '21 at 17:20
  • @BigBen Ah! Understandable. Thank you for explaining that and I will do that now! :) –  Apr 21 '21 at 23:42