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

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
You could do this:

VBA Code:
Sub EnterDates()

Dim i As Long, myDate As Date

With ActiveSheet
    If IsNumeric(.Name) And Len(.Name) = 6 Then
        .Range("C1:AH1").ClearContents
        myDate = DateSerial(Left(.Name, 4), Right(.Name, 2), 1) - 1
        For i = 0 To Day(WorksheetFunction.EoMonth(myDate, 1))
            .Cells(1, 3 + i) = myDate + i
        Next
    End If
End With
    
End Sub
 
Upvote 0
.. or you could try this without looping

VBA Code:
Sub Fill_Dates()
  With ActiveSheet
    .Range("C1").Value = DateSerial(Left(.Name, 4), Right(.Name, 2), 0)
    .Range("C1").DataSeries xlRows, xlChronological, xlDay, 1, DateSerial(Left(.Name, 4), Right(.Name, 2) + 1, 0)
  End With
End Sub
 
Upvote 0
Solution
Hi Steve the fish & Peter SSs,
I realized the codes also extract the last day of previous month. Is it possible to extract last five days of previous month if the start day of the month is in Column H. (Color C to G is last five days of previous month).
Can I put this as worksheet module instead of standard module ?
 
Upvote 0
To me your request is unclear. Could we have some sample data and expected results with XL2BB and further explanation in relation to that sample data?
 
Upvote 0
Hi, I mean based on the sheet name 202107, extract the days from June 25 to July 31 from cell C1 to AL1. Your code extract June 30 to July 31.
 

Attachments

  • calendar.png
    calendar.png
    12.6 KB · Views: 10
Upvote 0
Hi, I mean based on the sheet name 202107, extract the days from June 25 to July 31 from cell C1 to AL1. Your code extract June 30 to July 31.

If you want the last 5 days then you want from Jun 26 not 25.
@Peter_SSs ' s code is easily modifiable.

The first line gives the starting value in C1. To include 5 days in June (which is from 26 Jun) just subtract 4 from the days position in the DateSerial function in that starting value line.

VBA Code:
Sub Fill_Dates()
  With ActiveSheet
    .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 Sub
 
Upvote 0
Hi Alex, Thanks, it works. Is it necessary to put this in worksheet module instead of standard module since each sheet should have this ?
 
Upvote 0
Is it necessary to put this in worksheet module instead of standard module since each sheet should have this ?
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
 
Upvote 0

Forum statistics

Threads
1,215,497
Messages
6,125,155
Members
449,208
Latest member
emmac

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