[英]VBA EXCEL formula in a String
Hi I would like to put into a STRING the following formula嗨,我想将以下公式放入字符串中
Dim stringAppoggio As String
Dim myMonth As String
myMonth = "January 2020"
stringAppoggio="=DAY(EOMONTH(DATEVALUE(01-'&myMonth&'-&YEAR(myMonth)),0))"
It doesn't really give me a syntax error, but I don't see the result of the formula in the String它并没有真正给我一个语法错误,但我没有在字符串中看到公式的结果
Thank you谢谢
Well, as per my comment, there are a few mistakes here:好吧,根据我的评论,这里有一些错误:
myMonth
variablemyMonth
变量周围的引号Keep in mind, your variable is not just a month but a string holding a month and year > "January 2020"
, therefor DATEVALUE
won't need the 01-
and YEAR(myMonth)
to work.请记住,您的变量不仅仅是一个月,而是一个包含月份和年份的字符串 >
"January 2020"
,因此DATEVALUE
不需要01-
和YEAR(myMonth)
来工作。 Let me explain:让我解释:
=DATEVALUE("January 2020")
Will return Integer
43831, or in other words: 1-1-2020
.将返回
Integer
43831,或者换句话说: 1-1-2020
。 Then secondly, EOMONTH
will return the end of that same month as an Integer
, whereas DAY
will return the number of that day.其次,
EOMONTH
将作为Integer
返回同月的结束日期,而DAY
将返回当天的编号。 So your formula would read:所以你的公式应该是:
=DAY(EOMONTH(DATEVALUE("January 2020"),0))
Now to write this in VBA:现在用 VBA 写这个:
Dim stringAppoggio As String
Dim myMonth As String
myMonth = "January 2020"
stringAppoggio = "=DAY(EOMONTH(DATEVALUE(""" & myMonth & """),0))"
You can check that it works:您可以检查它是否有效:
Debug.Print Evaluate("=DAY(EOMONTH(DATEVALUE(""" & myMonth & """),0))")
Note: See the triple quotes?注意:看到三重引号了吗? That's because we need to feed
DATEVALUE
a string within quotes to work, otherwise it wouldn't be a string and will return an error那是因为我们需要给
DATEVALUE
一个带引号的字符串才能工作,否则它不会是一个字符串并且会返回一个错误
Dim stringAppoggio As String
Dim myMonth As String
myMonth = Chr(34) & "January 2020" & Chr(34)
stringAppoggio = "=DAY(EDATE(" & myMonth & ",1)-DAY(" & myMonth & "))"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.