vba 获取下个月的名称

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19782222/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 00:15:50  来源:igfitidea点击:

Get name of next month

excelexcel-vbavba

提问by Leon Smith

Does anyone have any examples of how I could cycle one month forwards in VBA?

有没有人有任何关于我如何在 VBA 中向前循环一个月的例子?

Currently I have a source sheet that has the month value "October". I have a macro button embedded in the worksheet that creates a new sheet and I need it to take the value from the cell that current has this month value and place it in the new sheet but one month forwards, i.e. "November" in this example.

目前我有一个源表,其月份值为“October”。我在工作表中嵌入了一个宏按钮,用于创建一个新工作表,我需要它从当前具有本月值的单元格中获取值并将其放置在新工作表中但向前一个月,即本例中的“十一月” .

回答by Tommy

You could either make a reusable function that returns what you are looking for or try a combination of Month/MonthName functions to get what you are looking for.

您可以创建一个可重用的函数来返回您要查找的内容,也可以尝试组合使用 Month/MonthName 函数来获取您要查找的内容。

Function GetNextMonth(Byval currentMonth as string) As string
   Select Case currentMonth
      Case "January"
          GetNextMonth = "February"
      Case "February"
          GetNextMonth = "March"
      Case "March"
          GetNextMonth = "April"
      Case "April"
          GetNextMonth = "May"
      Case "May"
          GetNextMonth = "June"
      Case "June"
          GetNextMonth = "July"
      Case "July"
          GetNextMonth = "August"
      Case "August"
          GetNextMonth = "September"
      Case "September"
          GetNextMonth = "October"
      Case "October"
          GetNextMonth = "November"
      Case "November"
          GetNextMonth = "December"
      Case "December"
          GetNextMonth = "January"
    End Select
End Function

Even shorter combination method:

更短的组合方法:

Function GetNextMonth(ByVal currentMonth as string) As String
    GetNextMonth = MonthName(Month(DateValue("01-" & currentMonth & "-2000"))+1)
End Function

Give VBA a date using your current month name, have it convert it, then grab the month number from it, add one and return the month name. However, you may need to add a check to see if the current Month is 12 (edge case).

使用您当前的月份名称为 VBA 指定一个日期,让它进行转换,然后从中获取月份编号,添加一个并返回月份名称。但是,您可能需要添加检查以查看当前月份是否为 12(边缘情况)。

回答by chris neilsen

As User Defined Function

作为用户定义的函数

Function NextMonth(m As String) As String
    NextMonth = Format(DateAdd("m", 1, DateValue("1 " & m & " 2000")), "mmmm")
End Function

or as an Excel Formula (where D1contains the Month you want to offset from)

或作为 Excel 公式(其中D1包含要从中抵消的月份)

=TEXT(EDATE( DATEVALUE("1 " & D1 & " 2000"),1), "mmmm")

回答by T.M.

Even shorter udf without explicit DateAddfunction

没有显式DateAdd函数的更短的 udf

This function accepts both strings as well as numbers, so the currMonthargument could be "January", "Jan" or 1.

此函数接受字符串和数字,因此currMonth参数可以是“January”、“Jan”或 1。

Function NextMonth$(currMonth)
    NextMonth = Format(CDate(currMonth & "/2 0") + 30, "mmmm")
End Function

Note

笔记

So the CDatefunction would transform the above input in a date of Jan 2nd 1900(=year 0), the next month is reached for sure by adding 30 days (average month). The Formatfunction via argument mmmmreturns the next month's full name, e.g. "February".

因此,该CDate函数将在1900 年 1 月 2 日(= 0 年)的日期转换上述输入,通过添加 30 天(平均月份)肯定会到达下个月。在Format通过参数函数mmmm返回下个月的全名,如"February"