WeekNum function - unable to insert into vba code

lYoung

New Member
Joined
Mar 1, 2020
Messages
35
Office Version
  1. 365
Platform
  1. Windows
my code reads

dim rowCount As Integer

rowCount = Range("J1").CurrentRegion.Rows.Count


For i = 1 To rowCount

Range("J:" & i) = Application.WeekNum(Range("K:" & i))



Next i


Note - Column K are date formatted and column J is number formatted.

Error message "Run-time error '1004'
Method 'Range' of object'_Global' failed
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
Range("J:" & i) and Range("K:" & i) should be Range("J" & i) and Range("K" & i).
You also haven't dimmed your i variable.
 
Upvote 0
Depending on the required method of week numbering, perhaps

VBA Code:
Dim c As Range, lRow As Long
lRow = Cells(Rows.Count, 11).End(xlUp).Row
For Each c In Range("J2:J" & lRow)
    c.Value = Format(c.Offset(, 1), "ww")
Next
 
Upvote 0
Depending on the required method of week numbering, perhaps

VBA Code:
Dim c As Range, lRow As Long
lRow = Cells(Rows.Count, 11).End(xlUp).Row
For Each c In Range("J2:J" & lRow)
    c.Value = Format(c.Offset(, 1), "ww")
Next
awesome - this worked. What function/purpose does the "ww" serve? Is it simply pulling out the # for the week date provided? So this was a work around to using weeknum? thank you!
 
Upvote 0
So this was a work around to using weeknum? thank you!
See post number 2, weeknum was fine. You were just using the wrong syntax for the ranges.

VBA Code:
rowCount = Range("J1").CurrentRegion.Rows.Count
is also not the best way to determine the last row especially as you are inputting the weeknum from J1 which is a bit odd as your currentregion starts there.
 
Upvote 0
It was an alternative, not a workaround. I personally try to avoid using worksheet functions in vba when and where possible. The vba format function has a couple of additional options that worksheet formats don't have, such as "w" for week number or "q" for quarter. There may be others but those are the only ones that I can think of.

I was originally just going to correct the typos in your original code but @MARK858 beat me to it in post 2.
 
Upvote 0

Forum statistics

Threads
1,215,065
Messages
6,122,944
Members
449,095
Latest member
nmaske

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