文本后跟本月的周数

时间:2018-04-18 17:35:45

标签: excel vba excel-vba

我似乎无法破解如何获取静态文本“WEEK”,然后是当月的当前周数(截至4/18/18即3)。这是针对范围rngColK。哪个属性会显示当月的周数? Application.WorksheetFunction.WeekNum(Now)似乎没有这样做,我不知道是否允许在静态文本“WEEK”之后添加它。

对于范围rngColJ,这个有点偏离主题,我得到正确的输出我只需要全部大写,并使用两个部分“;>” Rng.Offset(0, xOffsetColumn).NumberFormat = "MMMM;>"中没有这样做。语法不正确吗?

Private Sub Worksheet_Change(ByVal Target As Range)
'Fill in Date.
Dim rngColG As Range
Dim rngColJ As Range
Dim rngColK As Range
Dim Rng As Range
Dim xOffsetColumn As Integer
Set rngColG = Intersect(Application.ActiveSheet.Range("A:A"), Target)
xOffsetColumn = 6

Application.EnableEvents = False
If Not rngColG Is Nothing Then
    For Each Rng In rngColG
        If Not VBA.IsEmpty(Rng.Value) Then
            Rng.Offset(0, xOffsetColumn).Value = Date - Weekday(Date, 3)
            Rng.Offset(0, xOffsetColumn).NumberFormat = "mmmm d, yyyy"
        Else
            Rng.Offset(0, xOffsetColumn).ClearContents
        End If
    Next
End If

Set rngColJ = Intersect(Application.ActiveSheet.Range("A:A"), Target)
xOffsetColumn = 9

If Not rngColJ Is Nothing Then
    For Each Rng In rngColJ
        If Not VBA.IsEmpty(Rng.Value) Then
            Rng.Offset(0, xOffsetColumn).Value = Now
            Rng.Offset(0, xOffsetColumn).NumberFormat = "MMMM;>"
        Else
            Rng.Offset(0, xOffsetColumn).ClearContents
        End If
    Next
End If

Set rngColK = Intersect(Application.ActiveSheet.Range("A:A"), Target)
xOffsetColumn = 10

If Not rngColK Is Nothing Then
    For Each Rng In rngColK
        If Not VBA.IsEmpty(Rng.Value) Then
            Rng.Offset(0, xOffsetColumn).Value = "WEEK" &" "& Int((6 + 
            Day(Date + 1 - Weekday(Date - 1))) / 7)
        Else
            Rng.Offset(0, xOffsetColumn).ClearContents
        End If

    Next
End If
Application.EnableEvents = True
End Sub

1 个答案:

答案 0 :(得分:0)

更改Application.WorksheetFunction.WeekNum(Now) Int((6 + Day(Date + 1 - Weekday(Date - 1))) / 7)以正确输出以星期一开头的当月当前周数。