excelvbahijri

Hijri Date Formatting


I have some data that is downloaded from a website, one column of this data is containing Hijri date. In order to have this column as proper Date column I applied the below formatting:

enter image description here

but the issue it will not be considered as date and be aligned to the Right unless I enter the cell (by double click or F2) and then press Enter

Because the number of rows is big the way I'm using is not practical

I tried the following:

Sub HijriDateEnforce()
  Dim cel As Range
  Dim selectedRange As Range
  Set selectedRange = Application.Selection
  For Each cel In selectedRange.Cells
    Selection.NumberFormat = "[$-1970000]B2dd/mm/yyyy;@"
    SendKeys "{F2}~"
  Next cel
End Sub

but the code for unknown reason if the number of rows is very big it stops after some time and I have to run it again for the remaining cells


Solution

  • I hate suggesting .Select but what I suggested earlier doesn't work in your scenario. So try this

    Code

    Sub HijriDateEnforce()
        Dim acell As Range
        Dim selectedRange As Range
        
        Set selectedRange = Application.Selection
        selectedRange.NumberFormat = "[$-1970000]B2dd/mm/yyyy;@"
        
        For Each acell In selectedRange
            acell.Select
            Application.SendKeys "{F2}"
            Application.SendKeys "{ENTER}"
            DoEvents
        Next acell
    End Sub
    

    In Action

    enter image description here