Changing the Format will not return the week number. VBA does not support the WEEKNUM function so you need a UDF. place this in a Standard module
'from Chip Pearson
'The FW parameter serves the same purpose here as it does in the WEEKNUM worksheet function.
'Set it to 1 to indicate that weeks begin on Sunday, or to 2 to indicate that weeks begin on Monday.
Function VBAWeekNum(D As Date, FW As Integer) As Integer
VBAWeekNum = CInt(Format(D, "ww", FW))
End Function
For the userform
Option Explicit
Private Sub CommandButton1_Click()
Dim w1 As Long
Dim w2 As Long
Dim w As Long
Dim X As Long
w1 = CLng(Me.txtStart.Value)
w2 = CLng(Me.txtEnd.Value)
X = w2 - w1
Application.ScreenUpdating = True
With Worksheets("Template")
For w = 2 To X
.Visible = xlSheetVisible
.Copy after:=Worksheets(Worksheets.Count)
ActiveSheet.Name = "Week " & w
.Visible = xlSheetVeryHidden
Next w
End With
Application.ScreenUpdating = False
End Sub
Private Sub txtEnd_Exit(ByVal Cancel As MSForms.ReturnBoolean)
With Me.txtEnd
If Len(.Value) > 1 Then .Value = VBAWeekNum(CDate(.Value), 1)
End With
End Sub
Private Sub txtStart_Exit(ByVal Cancel As MSForms.ReturnBoolean)
With Me.txtStart
If Len(.Value) > 1 Then .Value = VBAWeekNum(CDate(.Value), 1)
End With
End Sub
Bookmarks