BetaOp9
BetaOp9

Reputation: 71

Excel VBA - Set variable to text and cell value

Still working on my first VBA script, but I'm stuck. I want to set a variable to a cell value AND text but it only lets me set a variable to one or the other, not both.

Example:

Dim Week As Range
Dim WeekCurrent As String

Set Week = .Range("F4")

If Week.Value = "Initial Week" Then
    WeekCurrent = "Initial Week"
Else
    WeekCurrent = "Week #" Week.Value
End If

Desired Result: "Initial Week" or "Week #x" to call on later.

I can either set it to the "Week #" text or to Week.Value but not both. I'm sure I'm using the wrong declaration or something.

As of right now, my workaround involves changing any instances I want to use this variable into an IF THEN ELSE and doubles the amount of coding because I have to duplicate the code. Thanks for the feedback.

Upvotes: 1

Views: 11129

Answers (1)

Jeff Grove
Jeff Grove

Reputation: 26

Another way of doing this may be:

If IsNumeric(Week) Then WeekCurrent = "Week #" & Week Else WeekCurrent = Week

Upvotes: 1

Related Questions