Help with the VBA to convert dates to weeks

titoexcel

Board Regular
Joined
Mar 26, 2013
Messages
55
Hi, I am trying to convert dates to weeks. So from beginning of the year:

Week 1 (Jan 1st - 6th)
week 2 (jan 7th - Jan 13th)

My Code now and btw its very slow not sure if there is a better way to do this.

Code:
Sub UpdateData()  Dim X As Long, LR As Long
  Sheets("Data-Weeks").Select
  LR = Range("A" & Rows.Count).End(xlUp).Row
  For X = 2 To LR
    Cells(X, "Z").Value = DatePart("ww", Cells(X, "B"), 4, vbUseSystem)
  Next
End Sub

I am not sure how to set it up so it Starts from Jan 1st and it goes to Jan6th as week 1, and move along and review the dates and mark as Week 1 or Week 2 dependent on the dates.

Dates are in Column B and week ouput (week1,week 2) can be placed in Column AA. Worksheet name: "Data-Weeks".

thank you for anyone that can help as I am struggling.
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
Hi - is this any quicker?

Code:
Sub UpdateData()
Dim X As Long, V
V = Sheets("Data-Weeks").Range("B2:B" & Range("A" & Rows.Count).End(xlUp).Row)
For X = 1 To UBound(V)
    V(X, 1) = DatePart("ww", V(X, 1), 4, vbUseSystem)
Next
Range("AA2").Resize(UBound(V)).Value = V
End Sub
 
Upvote 0

That was not very well qualified, please try this version.

Code:
Sub UpdateData()
Dim X As Long, V
With Sheets("Data-Weeks")
    V = .Range("B2:B" & .Range("A" & .Rows.Count).End(xlUp).Row)
    For X = 1 To UBound(V)
        V(X, 1) = DatePart("ww", V(X, 1), 4, vbUseSystem)
    Next
    .Range("AA2").Resize(UBound(V)).Value = V
End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,651
Messages
6,120,744
Members
448,989
Latest member
mariah3

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top