Changing the col. header

Guinaba

Board Regular
Joined
Sep 19, 2018
Messages
83
Hi guys,

I need to change the col. header from col.G to the last populated col. from first day of the week 01/06/2006 to Week Number/Year as shown below. Not sure why my code is not working. Any suggestion?

Before running the code
1/06/2020
8/06/2020
15/06/2020
After running the code23/202024/202025/2020


VBA Code:
Dim LastCol As Integer
Dim Header As Variant
Dim i As Integer
'Starting counting from col. G and row 11
LastCol = Sheets("Report").cells(11, Columns.Count).End(xlToLeft).Column
Dim rng As Range: Set rng = Application.Range(cells(11, 7), cells(11, LastCol))

   For i = 1 To rng.Columns.Count
         Header = rng.cells(11, i).Value
         Header = Sheets(1).Application.WorksheetFunction.WeekNum(Header, vbMonday) & "/" & Year(Header)
    Next
 

Some videos you may like

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.

Snakehips

Well-known Member
Joined
May 17, 2009
Messages
5,069
Office Version
  1. 2013
Platform
  1. Windows
@Guinaba
Try...
VBA Code:
For i = 1 To rng.Columns.Count
         Header = rng.cells(1, i).Value. '<<<<<<<<<<<<<<<<<<<<. 1 not 11
         Header = Sheets(1).Application.WorksheetFunction.WeekNum(Header, vbMonday) & "/" & Year(Header)
    Next
 

Watch MrExcel Video

Forum statistics

Threads
1,122,456
Messages
5,596,234
Members
414,048
Latest member
wnied1

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
Top