1

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.

4
  • what are you trying to achieve with WeekCurrent = "Week #" Week.Value ??? Also, try to avoid declaring a variable with the name of Week which is an Excel saved word Commented Feb 21, 2017 at 3:32
  • As I mentioned. I want to call on that string where week.value is a number and it outputs "Week #n" where n = week.value. Commented Feb 21, 2017 at 3:35
  • 1
    so you meant to use WeekCurrent = "Week #" & Week.Value Commented Feb 21, 2017 at 3:36
  • I knew it was something stupid I was missing. This was the fix. Thanks. Commented Feb 21, 2017 at 3:39

1 Answer 1

1

Another way of doing this may be:

If IsNumeric(Week) Then WeekCurrent = "Week #" & Week Else WeekCurrent = Week
Sign up to request clarification or add additional context in comments.

3 Comments

Nice! Like this, more efficient and flexible
I'm sure you may be aware you can refer to excel cells using Cells(row#,column#).
Then by using variables for the row and/or column numbers you can be a bit more dynamic on which cell you reference or easily construct code to loop through and process a range of cells.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.