Excel VBA - 将变量设置为文本和单元格值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42358102/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Excel VBA - Set variable to text and cell value
提问by BetaOp9
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.
我可以将它设置为"Week #"
文本或设置为Week.Value
但不能同时设置。我确定我使用了错误的声明或其他东西。
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 并将编码量加倍,因为我必须复制代码。感谢您的反馈。
采纳答案by Jeff Grove
Another way of doing this may be:
另一种方法可能是:
If IsNumeric(Week) Then WeekCurrent = "Week #" & Week Else WeekCurrent = Week