+ Reply to Thread
Results 1 to 7 of 7

Scanning columns that have dates in them and change format

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    09-04-2013
    Location
    USA
    MS-Off Ver
    Excel 365
    Posts
    266

    Scanning columns that have dates in them and change format

    Below is an example of a macro that I use to convert dates in a spreadsheet from ddmmmmyyyy format to mm/dd/yyyy. In this case, I know which columns have dates in them (starting in row 2 on down, as row 1 as the column header). However, I routinely receive other spreadsheets with dates in columns that I do not know of in advance. Is there a macro that can "scan" for any date values in row 2, and then format the entire column where these dates are found to mm/dd/yyyy? Thanks.


    Sub SelectColumns()
    Range("A:B,O:Q,BJ:BK").Select
    Selection.NumberFormat = "mm/dd/yyyy"
    End Sub

  2. #2
    Forum Expert
    Join Date
    08-12-2012
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2010
    Posts
    5,636

    Re: Scanning columns that have dates in them and change format

    are the cells actually in a date format? or are they text fields that look like a date?
    If you are satisfied with the solution(s) provided, please mark your thread as Solved.
    Select Thread Tools-> Mark thread as Solved. To undo, select Thread Tools-> Mark thread as Unsolved.

  3. #3
    Forum Contributor
    Join Date
    09-04-2013
    Location
    USA
    MS-Off Ver
    Excel 365
    Posts
    266

    Re: Scanning columns that have dates in them and change format

    They are in a date format..

  4. #4
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    Win10/MSO2016
    Posts
    12,994

    Re: Scanning columns that have dates in them and change format

    Option Explicit
    Sub DateTest()
        Dim TestRow As Range, _
            TestCell As Range
        Set TestRow = Cells(2, Columns.Count).End(xlToLeft)
        For Each TestCell In TestRow
            If IsDate(TestCell.Value) Then
                TestCell.EntireColumn.NumberFormat = "mm/dd/yyyy"
            End If
        Next TestCell
    End Sub
    Ben Van Johnson

  5. #5
    Forum Contributor
    Join Date
    09-04-2013
    Location
    USA
    MS-Off Ver
    Excel 365
    Posts
    266

    Re: Scanning columns that have dates in them and change format

    I tried the macro, and it did not work. However, when I copy the columns with dates to a new spreadsheet and run the macro, it does work. Strange.

    Dates are formatted "Custom" as [$-409]ddmmmyyyy. I can also right-click on them and convert to mm/dd/yyyy. Spreadsheet is downloaded from SAS.. Any ideas?

    Attached is a sample of the spreadsheet with the dates...
    Attached Files Attached Files
    Last edited by Brawnystaff; 03-31-2016 at 03:11 AM.

  6. #6
    Forum Expert
    Join Date
    08-12-2012
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2010
    Posts
    5,636

    Re: Scanning columns that have dates in them and change format

    changes proton's code slightly as it was only picking the last column

    Option Explicit
    Sub DateTest()
        Dim TestRow As Range, _
            TestCell As Range
        Set TestRow = Range(Cells(2, 1), Cells(2, Columns.Count).End(xlToLeft))
        For Each TestCell In TestRow
            If IsDate(TestCell.Value) Then
                TestCell.EntireColumn.NumberFormat = "mm/dd/yyyy"
            End If
        Next TestCell
    End Sub
    Last edited by humdingaling; 03-31-2016 at 07:15 PM.

  7. #7
    Forum Contributor
    Join Date
    09-04-2013
    Location
    USA
    MS-Off Ver
    Excel 365
    Posts
    266

    Re: Scanning columns that have dates in them and change format

    Thanks humdingaling. That worked.. Will mark as SOLVED.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 4
    Last Post: 11-26-2015, 09:23 AM
  2. Scanning for shifted values in different columns
    By luinox86 in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 06-14-2011, 08:04 AM
  3. scanning and looping columns
    By omgeokid in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-22-2011, 12:05 PM
  4. VBA: scanning through rows & columns for value
    By tesfamichael in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-26-2009, 09:32 AM
  5. Change format of dates
    By John Ploughman in forum Excel General
    Replies: 3
    Last Post: 01-06-2009, 01:43 PM
  6. how can i change dates from US to UK format
    By kuju in forum Excel General
    Replies: 1
    Last Post: 11-09-2005, 09:55 AM
  7. Can I change a column of dates from USA to UK format?
    By Michelle, Mencap in forum Excel General
    Replies: 3
    Last Post: 03-02-2005, 12:06 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1