WeekNum, Month and Year

Agatha1226

New Member
Joined
Oct 18, 2016
Messages
10
Hi,

I have the following code and would like to get the week number based on the date on column E. I tried to run the code but nothing happens. Basically, it checks each cell in column S if it is blank. If the cell is blank, it should get the corresponding week number based on the date in column E. Can anyone check and correct?

Code:
Sub CleanUp()


Dim lRow As Long
'Dim lCol As Long
Dim i As Long


lRow = Cells(Rows.Count, 1).End(xlUp).Row
'lCol = Cells(1, Columns.Count).End(xlToLeft).Column


For i = 4 To lRow
  If Cells(i, 19) <> "" Then
    Cells(i, 19).Value = Application.WorksheetFunction.WeekNum(Cells(i, 5))
  Else
      Exit For
  End If
Next i


End Sub
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
This line:
Code:
[COLOR=#333333] If Cells(i, 19) <> "" Then
[/COLOR]    [COLOR=#333333]Cells(i, 19).Value = Application.WorksheetFunction.WeekNum(Cells(i, 5))[/COLOR]
means only get the weeknum if column "S" HAS a value which is the opposite of what you stated. Going off your description it should read
Code:
[COLOR=#333333] If Cells(i, 19) [/COLOR][B][COLOR=#ff0000]= [/COLOR][/B][COLOR=#333333]""[/COLOR]
 
Upvote 0
Just comment the "Else Exit For" part because if the first cell doesn't meet the specified criteria it will exit the loop
 
Upvote 0
Hi @gallen....would it be possible to extend this so that it will also check columns T and U and update cells under these 2 columns with the month and year values from the date entered under column E? I tried replicating the For loop by changing the corresponding column numbers, (20 and 21 in place of 19) but it gives me "Run time error 438, Objecy doesn't support this property or method"
 
Upvote 0
Can you post the full code you have now that works as you want for column S?

Changing the column numbers should be fine so something else is stopping it.
 
Upvote 0

Forum statistics

Threads
1,214,834
Messages
6,121,876
Members
449,056
Latest member
ruhulaminappu

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