Extract date format from sheet name

Vincent88

Active Member
Joined
Mar 5, 2021
Messages
382
Office Version
  1. 2019
Platform
  1. Windows
  2. Mobile
Hi Guys,
My sheet named as 2021(07) and I use below cell function code to extract its year, month and date to a cell C1.

=DATE(LEFT(MID(CELL("filename",A1),FIND("]",CELL("filename",A1))+1,255),4),MID(CELL("filename",A1),FIND("(",CELL("filename",A1))+1,FIND(")",CELL("filename",A1))-FIND("(",CELL("filename",A1))-1),1)

If my sheetname date format changed to 202107, what is the VBA code to extract the date form to 1-Jul to cell D1 and rest of the days (till 31st to cell AH1 and 30-Jun in C1).
Thanks.
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
If you want to apply this to all worksheets (that are named with 6 digits) then you could just run the code below from a standard module, rather than repeating it in every such worksheet.

VBA Code:
Sub Fill_Dates_v2()
  Dim ws As Worksheet
 
  For Each ws In Worksheets
    If ws.Name Like "######" Then
      With ws
        .Range("C1").Value = DateSerial(Left(.Name, 4), Right(.Name, 2), -4)
        .Range("C1").DataSeries xlRows, xlChronological, xlDay, 1, DateSerial(Left(.Name, 4), Right(.Name, 2) + 1, 0)
      End With
    End If
  Next ws
End Sub
Hi Peter SSs,
I just realized, while copying sheet, if the new month is less than 31 days, the last cell does not change (see image uploaded) to the date of month after next. ( If the month is February, then there will be more cells not showing the dates of March)
I tried to add autofill to Range("C1").DataSeries xlRows, xlChronological, xlDay, 1, DateSerial(Left(.Name, 4), Right(.Name, 2) + 1, 0) but it does not work. Please help.
 

Attachments

  • dateempty.png
    dateempty.png
    59.2 KB · Views: 6
Upvote 0
I just realized, while copying sheet, if the new month is less than 31 days, the last cell does not change (see image uploaded)
Ah, you didn't mention that you would be copying these sheets or that there might already be data in row 1. ;)

Try adding this line
Rich (BB code):
Sub Fill_Dates_v3()
  Dim ws As Worksheet
  
  For Each ws In Worksheets
    If ws.Name Like "######" Then
      With ws
        .Range("C1").Resize(, 36).ClearContents
        .Range("C1").Value = DateSerial(Left(.Name, 4), Right(.Name, 2), -4)
        .Range("C1").DataSeries xlRows, xlChronological, xlDay, 1, DateSerial(Left(.Name, 4), Right(.Name, 2) + 1, 0)
      End With
    End If
  Next ws
End Sub
 
Upvote 0
Ah, you didn't mention that you would be copying these sheets or that there might already be data in row 1. ;)

Try adding this line
Rich (BB code):
Sub Fill_Dates_v3()
  Dim ws As Worksheet
 
  For Each ws In Worksheets
    If ws.Name Like "######" Then
      With ws
        .Range("C1").Resize(, 36).ClearContents
        .Range("C1").Value = DateSerial(Left(.Name, 4), Right(.Name, 2), -4)
        .Range("C1").DataSeries xlRows, xlChronological, xlDay, 1, DateSerial(Left(.Name, 4), Right(.Name, 2) + 1, 0)
      End With
    End If
  Next ws
End Sub
Hi Peter SSs,
Additional code works -with blank cells after the last day of that month. If I want to show the dates of next month instead of blank, what should be amended.
 
Upvote 0
Hi Peter SSs,
If the blank cell is just one, then show the 1st day of next month but for the month of February with 28 days, then shows March 1, 2, and 3. In other word, blank cells between C1 to AL1 should only be found in AJ1, AK1 and AL1. Hope this helps you to figure out the resolution for me. Many thanks.
 
Upvote 0
Try
VBA Code:
Sub Fill_Dates_v4()
  Dim ws As Worksheet
  
  For Each ws In Worksheets
    If ws.Name Like "######" Then
      With ws
        .Range("C1").Resize(, 36).ClearContents
        .Range("C1").Value = DateSerial(Left(.Name, 4), Right(.Name, 2), -4)
        .Range("C1").DataSeries xlRows, xlChronological, xlDay, 1, .Range("C1").Value + 35
      End With
    End If
  Next ws
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,897
Messages
6,122,141
Members
449,066
Latest member
Andyg666

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