Quantcast
Channel: Active questions tagged excel - Stack Overflow
Viewing all articles
Browse latest Browse all 91196

Date formatting - For Each loop changing outputs

$
0
0

The age old date conundrum in Excel. I am looking for advice/ help on the below please!

I have a set of dates:

enter image description here

Important to note about these dates: They are output from an array using start_date = Format$("01-"& sheet_date, "\ dd\/mm\/yyyy\") and end_date = Format$(DateSerial(Year(DateValue(sheet_date)), Month(DateValue(sheet_date)) + 1, 0), "\ dd\/mm\/yyyy\") respectively.

The reason for this is to hold UK format. If I use any other way the Vendor Start Dates default to US.

Now with the output dates (as above) I am trying to run through the range of dates, and "trim" the space while holding the same format. I say "trim" because the simple method does not yield the desired UK format, and the dates format to US.

Now for the interesting part. I have tried a couple of simple things in testing how to maintain the UK format and have come up with the below (testing on one cell):

Sub test()

    Dim start_date As Date

    start_date = ActiveCell.Value
    ActiveCell.Value = start_date

End Sub

This maintains the date format correctly, as seen below:

enter image description here

Now, when I run this in a For Each loop it does not maintain the UK format for some reason. I use the following code:

Sub explicit_date_clean()

    Dim date_range As Range, cell As Range
    Dim temp_date As Date


    With ActiveSheet
        'set range
        Set date_range = .Range("1:1").Find(What:="**Vendor Start Date", LookAt:=xlWhole).Offset(1, 0).Resize(.UsedRange.Rows.Count, 2)

        For Each cell In date_range
            temp_date = cell.Value
            cell.Value = temp_date
        Next cell

    End With

End Sub

This is now the output running it through the For Each loop:

enter image description here

Any ideas on this SO?


Viewing all articles
Browse latest Browse all 91196

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>