Return 给定索引的数组中的字符串元素
Return string element from array given index
我有以下代码:
Dim CurrMonth As Integer
Dim MonthPos As Variant
Dim CurrPos As Integer
CurrMonth = Month(Date) - 1
MonthPos = Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December")
任何人都可以提供指导,告诉我如何在给定索引 CurrMonth 的情况下从 Array MonthPos 中获取(在本例中)字符串元素。
https://www.softwaretestinghelp.com/vba-array-tutorial/#One_Dimensional_Array 提供以下示例:
Private Sub arrayExample3()
Dim thirdQuarter(13 To 15) As String 'creates array with index 13,14,15
thirdQuarter(13) = "July"
thirdQuarter(14) = "Aug"
thirdQuarter(15) = "Sep"
MsgBox "Third Quarter in calendar " & " " & thirdQuarter(13) & " " &
thirdQuarter(14) & " " & thirdQuarter(15)
End Sub
所以对于你的情况应该是:
MonthPos(CurrMonth)
请使用下一个适合的方式:
Sub extractMonth()
Dim CurrMonth As Long, MonthPos As Variant, CurrPos As Integer, prevMonth
CurrMonth = Month(Date) - 1
MonthPos = Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December")
prevMonth = MonthPos(CurrMonth - 1) '- 1 because it is a zero based 1D array...
Debug.Print prevMonth 'the previous month (I could see Month(Date) - 1) and I supposed that this is needed.
'otherwise, you should simple use MonthPos(CurrMonth), for the current month
End Sub
你也可以使用
代替数组
Format$(date,"MMMM")
或者如果您需要之前的月份,那么
Format$(DateSerial(Year(Date),Month(Date)-1,1),"MMMM")
获取操作系统语言的月份名称。
假设Excel(一次性评估月份名称数组)以下方法允许输入任何增量或减量作为可选的第二个参数:
Function getMonth(currMonth As Long, Optional MonthOffset As Long = 0)
'a) build array of month names via evaluation (Excel)
Dim months(): months = [Text(Date(0,Column(A:L),1),"mmmm")]
'b) calculate wanted month
currMonth = (currMonth + MonthOffset + 12) Mod 12
If currMonth = 0 Then currMonth = 12
'c) return month name
getMonth = months(currMonth)
End Function
调用示例[=19=]
简单地传递一个可能的减量作为第二个参数:
Debug.Print getMonth(1) ' ~~> returns January
Debug.Print getMonth(1,-1) ' ~~> returns December
'assuming call as of 5/31 2022, i.e. equalling getMonth(5)
Debug.Print getMonth(Month(Date)) ' ~~> returns May
Debug.Print getMonth(Month(Date),-1) ' ~~> returns April
我有以下代码:
Dim CurrMonth As Integer
Dim MonthPos As Variant
Dim CurrPos As Integer
CurrMonth = Month(Date) - 1
MonthPos = Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December")
任何人都可以提供指导,告诉我如何在给定索引 CurrMonth 的情况下从 Array MonthPos 中获取(在本例中)字符串元素。
https://www.softwaretestinghelp.com/vba-array-tutorial/#One_Dimensional_Array 提供以下示例:
Private Sub arrayExample3()
Dim thirdQuarter(13 To 15) As String 'creates array with index 13,14,15
thirdQuarter(13) = "July"
thirdQuarter(14) = "Aug"
thirdQuarter(15) = "Sep"
MsgBox "Third Quarter in calendar " & " " & thirdQuarter(13) & " " &
thirdQuarter(14) & " " & thirdQuarter(15)
End Sub
所以对于你的情况应该是:
MonthPos(CurrMonth)
请使用下一个适合的方式:
Sub extractMonth()
Dim CurrMonth As Long, MonthPos As Variant, CurrPos As Integer, prevMonth
CurrMonth = Month(Date) - 1
MonthPos = Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December")
prevMonth = MonthPos(CurrMonth - 1) '- 1 because it is a zero based 1D array...
Debug.Print prevMonth 'the previous month (I could see Month(Date) - 1) and I supposed that this is needed.
'otherwise, you should simple use MonthPos(CurrMonth), for the current month
End Sub
你也可以使用
代替数组Format$(date,"MMMM")
或者如果您需要之前的月份,那么
Format$(DateSerial(Year(Date),Month(Date)-1,1),"MMMM")
获取操作系统语言的月份名称。
假设Excel(一次性评估月份名称数组)以下方法允许输入任何增量或减量作为可选的第二个参数:
Function getMonth(currMonth As Long, Optional MonthOffset As Long = 0)
'a) build array of month names via evaluation (Excel)
Dim months(): months = [Text(Date(0,Column(A:L),1),"mmmm")]
'b) calculate wanted month
currMonth = (currMonth + MonthOffset + 12) Mod 12
If currMonth = 0 Then currMonth = 12
'c) return month name
getMonth = months(currMonth)
End Function
调用示例[=19=]
简单地传递一个可能的减量作为第二个参数:
Debug.Print getMonth(1) ' ~~> returns January
Debug.Print getMonth(1,-1) ' ~~> returns December
'assuming call as of 5/31 2022, i.e. equalling getMonth(5)
Debug.Print getMonth(Month(Date)) ' ~~> returns May
Debug.Print getMonth(Month(Date),-1) ' ~~> returns April