Excel vba 代码要怎么写,问题见图 谢谢!

2024-11-22 15:43:33
推荐回答(4个)
回答1:

不需要VBA都可以的,只要公式

F1=IF(DATE(YEAR($B$1),MONTH($B$1)+ROW(A1)-1,1)>DATE(YEAR($C$1),MONTH($C$1),1),"",DATE(YEAR($B$1),MONTH($B$1)+ROW(A1)-1,1))

往下填充,然后将F列的单元格格式改为酱紫的:

回答2:

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
上面的可能更简单,既然写了就贴出来。

回答3:

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

回答4:

选中单元格C1,右键设置单元格式,选择日期-----XXXX年XX月就行了