Hi all,
So just to be up front my VBA knowledge is still basic - I'm learning as I go so please bear that in mind in any responses !
So, I have a worksheet called 'Contents'.
Col C from Row 7 down, shows all of the visible worksheet tab names.
All of the tabs are named automatically using the same criteria '01-07-2022 - sample text1', '11-12-2024 - sample text2' and so on basically they will all start with a 'date'
In the adjacent rows in Col B I have extracted the first 10 characters of the tab names in Col C for example using the sample tab names above B7 will show 01-07-2022 and B8 will show 11-12-2024.
What I'm trying to accomplish is to be able to 'convert' B7 ect into a date format and then sort the rows into oldest to newest but no matter what I try using the code below I can not get the values in Col B to be recognised as a date it seems to be seeing it as numbers and sorting in numerical order.
I did change the 'NumberFormat' to 'DateFormat' and tried using Cdate but it kept erroring with type mismatch. I have thought about referencing Col B values to another row to try and see if I could convert the values to a date that way.
Does anyone have any suggestions on how I could accomplish this as I'm all out of ideas
So just to be up front my VBA knowledge is still basic - I'm learning as I go so please bear that in mind in any responses !
So, I have a worksheet called 'Contents'.
Col C from Row 7 down, shows all of the visible worksheet tab names.
All of the tabs are named automatically using the same criteria '01-07-2022 - sample text1', '11-12-2024 - sample text2' and so on basically they will all start with a 'date'
In the adjacent rows in Col B I have extracted the first 10 characters of the tab names in Col C for example using the sample tab names above B7 will show 01-07-2022 and B8 will show 11-12-2024.
What I'm trying to accomplish is to be able to 'convert' B7 ect into a date format and then sort the rows into oldest to newest but no matter what I try using the code below I can not get the values in Col B to be recognised as a date it seems to be seeing it as numbers and sorting in numerical order.
I did change the 'NumberFormat' to 'DateFormat' and tried using Cdate but it kept erroring with type mismatch. I have thought about referencing Col B values to another row to try and see if I could convert the values to a date that way.
Does anyone have any suggestions on how I could accomplish this as I'm all out of ideas
VBA Code:
Sub ExtractDate()
Dim lastRow As Long
Dim i As Long
Call UnprotectSheet
lastRow = Cells(Rows.Count, "C").End(xlUp).Row
For i = 7 To lastRow
Cells(i, "B").Value = Left(Cells(i, "C").Value, 10)
Cells(i, "B").NumberFormat = "mm-dd-yyyy"
Next i
Call ProtectSheet
End Sub