获取两个输入日期之间的月份名称,并将其放入数组中 [英] Get monthnames between 2 input dates and put them into an array
问题描述
这是我要执行的操作:单元格B2:开始日期和单元格B3:结束日期
示例:
B2-> 2019年1月1日
B3-> 2019年1月5日
示例
这是我的代码,因此远:
Sub GetUniqueMonths()
Dim StartDate as Date,EndDate As Date
With ThisWorkbook.Sheets( Foglio1)'记住要完全限定您的范围,包括工作簿
StartDate = .Range( B2)
EndDate = .Range( B3)
以
结尾以Dim StartWeek作为字符串,以EndWeek作为字符串
StartWeek = MonthName(Month(StartDate))
EndWeek = MonthName(Month(EndDate))
Dim arr()作为字符串
Dim i as Long
ReDim arr(StartWeek To EndWeek)
For i = StartWeek至EndWeek
arr (i)= i
接下来的
结束子
I想要循环查看给定的两个日期: 01/01/2019 和 01/05/2019 获取月份名称并将其放入数组 arr ,但我得到的只是一个错误-> ReDim arr(StartWeek To EndWeek)
上的类型不匹配,有什么建议吗? p>
我的输出将是 arr(1月,2月,3月,4月,5月)
您可以按照今天上午在您的其他问题上回答的方式进行回答。一个>。另一种尝试:
< img src = https://i.stack.imgur.com/WJqEX.png alt =在此处输入图片描述>
Sub Test()
Dim StrtD尽可能长,EndD一样长
Dim arr作为变量
With Sheets( Foglio1)
StrtD = Month(.Cells(1,2).Value)
EndD = Month(.Cells(2,2).Value)
arr = Application.Transpose(.Evaluate( TEXT(DATE(2019,ROW(& StrtD&:& EndD&),1), mmmm)))
结尾为
End Sub
如果跨度超过某年,您需要知道两个日期之间有多少个月。在您的示例 1-12-2019
和 1-3-2020
中。现在我在 .Evaluate
中使用的公式将需要从12-15范围内。为此,我们可以使用 DateDiff
函数,
子测试()
Dim StrtD尽可能长,EndD一样长
Dim arr像变型
With Sheets( Foglio1)
StrtD =月(。 Cells(1,2).Value)
EndD = StrtD + DateDiff( m,.Cells(1,2).Value,.Cells(2,2).Value)
arr =应用程序.Transpose(.Evaluate( TEXT(DATE(2019,ROW(& StrtD&:& EndD&),1), mmmm)))
结尾为
结束子
您可能会想但是现在您输入公式a范围是12:15?既然不超过12,那该如何返回正确的月份?好吧,好的事情是,该公式将知道以13表示一月,以14表示二月,依此类推。因此,将正确的数组返回给变量。 :)
Here is what I'm trying to do: Cell B2: Start Date and Cell B3: End Date
Example:
B2 --> 01/01/2019
B3 --> 01/05/2019
Example
Here is my code so far:
Sub GetUniqueMonths()
Dim StartDate As Date, EndDate As Date
With ThisWorkbook.Sheets("Foglio1") 'remember to fully qualify your ranges, including the workbook
StartDate = .Range("B2")
EndDate = .Range("B3")
End With
Dim StartWeek As String, EndWeek As String
StartWeek = MonthName(Month(StartDate))
EndWeek = MonthName(Month(EndDate))
Dim arr() As String
Dim i As Long
ReDim arr(StartWeek To EndWeek)
For i = StartWeek To EndWeek
arr(i) = i
Next
End Sub
I would like to loop thought the given two dates: 01/01/2019 and 01/05/2019 get the month names and put them in the array arr, but all I get it's an error --> type mismatch on ReDim arr(StartWeek To EndWeek)
, any suggestions?
My output would be arr(January, February, March, April, May)
You could answer this in the same fashion as I did this morning on your other question. Just as an alternative try:
Sub Test()
Dim StrtD As Long, EndD As Long
Dim arr As Variant
With Sheets("Foglio1")
StrtD = Month(.Cells(1, 2).Value)
EndD = Month(.Cells(2, 2).Value)
arr = Application.Transpose(.Evaluate("TEXT(DATE(2019,ROW(" & StrtD & ":" & EndD & "),1), ""mmmm"")"))
End With
End Sub
If you go past a span of a certain year you need to know how many months are between two dates. In your example 1-12-2019
and 1-3-2020
. Now the formula I used in .Evaluate
will need to get a range from 12-15. We can use DateDiff
function for that, see below:
Sub Test()
Dim StrtD As Long, EndD As Long
Dim arr As Variant
With Sheets("Foglio1")
StrtD = Month(.Cells(1, 2).Value)
EndD = StrtD + DateDiff("m", .Cells(1, 2).Value, .Cells(2, 2).Value)
arr = Application.Transpose(.Evaluate("TEXT(DATE(2019,ROW(" & StrtD & ":" & EndD & "),1), ""mmmm"")"))
End With
End Sub
You might wonder "but now you feed the formula a range of 12:15? How would that return the right month since it doesn't go past 12?" Well, the nice thing is that the formula will know that with 13 we meant January, with 14 we meant february etc. Therefor the correct array is returned to the variable. :)
这篇关于获取两个输入日期之间的月份名称,并将其放入数组中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!