将变量设置为文本和单元格值

时间:2022-06-19 22:23:53

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.

我还在编写我的第一个VBA脚本,但是我被卡住了。我想将一个变量设置为一个单元格值和文本,但它只允许我将一个变量设置为一个或另一个,而不是两个。

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.

期望的结果:“最初的一周”或“第x周”以后再打电话。

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.

到目前为止,我的解决方案包括将我想要使用这个变量的任何实例更改为IF THEN ELSE,并将编码量增加一倍,因为我必须复制代码。谢谢你的反馈。

1 个解决方案

#1


1  

Another way of doing this may be:

另一种方法是:

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

#1


1  

Another way of doing this may be:

另一种方法是:

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