Excel vba 代码要怎么写,问题见图 谢谢!
4个回答
展开全部
Sub 宏1()
Dim i As Integer, n As Integer
Dim y1 As Integer, m1 As Integer, d1 As Integer
Dim y2 As Integer, m2 As Integer, d2 As Integer
y1 = Year(Sheet1.Range("B1"))
m1 = Month(Sheet1.Range("B1"))
y2 = Year(Sheet1.Range("C1"))
m2 = Month(Sheet1.Range("C1"))
If Sheet1.Range("B1") > Sheet1.Range("C1") Then MsgBox "C1单元格日期早于B1单元格": Exit Sub
Sheet1.Columns(6).Clear
n = (y2 - y1) * 12 + m2 - m1
For i = 0 To n
If m1 + i > 12 Then y1 = y1 + 1: m1 = m1 - 12
Sheet1.Cells(i + 1, 6) = y1 & "年" & m1 + i & "月"
Next i
End Sub
上面的可能更简单,既然写了就贴出来。
Dim i As Integer, n As Integer
Dim y1 As Integer, m1 As Integer, d1 As Integer
Dim y2 As Integer, m2 As Integer, d2 As Integer
y1 = Year(Sheet1.Range("B1"))
m1 = Month(Sheet1.Range("B1"))
y2 = Year(Sheet1.Range("C1"))
m2 = Month(Sheet1.Range("C1"))
If Sheet1.Range("B1") > Sheet1.Range("C1") Then MsgBox "C1单元格日期早于B1单元格": Exit Sub
Sheet1.Columns(6).Clear
n = (y2 - y1) * 12 + m2 - m1
For i = 0 To n
If m1 + i > 12 Then y1 = y1 + 1: m1 = m1 - 12
Sheet1.Cells(i + 1, 6) = y1 & "年" & m1 + i & "月"
Next i
End Sub
上面的可能更简单,既然写了就贴出来。
追问
谢谢
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
Sub iMonth()
Dim iMonth As Long, i As Long
iMonth = Month(Range("C1")) - Month(Range("B1")) + 1
For i = 1 To iMonth
Range("F" & i) = Format(Range("B1") + 30 * i - 30, "yyyy年m月")
Next
End Sub
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
选中单元格C1,右键设置单元格式,选择日期-----XXXX年XX月就行了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询