简体   繁体   中英

Excel Macro or Function to clear cell content if not a date

Issue

Garbage gets erroneously entered into date fields and screws up calculations.

Goal

Using a macro or function to check the contents in a range of cells, determine if they are dates and if not, delete the non-date contents.

What I've Tried

Using Search and Replace function to locate and delete offending items. Takes forever and still doesn't clear everything out.

Any ideas, thanks.

You could try this

Sub DeleteNonDates()
    Dim r As Range, a As Range, cl As Range
    Set r = ActiveSheet.UsedRange.SpecialCells(xlCellTypeConstants)
    For Each cl In r.Cells
        If TypeName(cl.Value) <> "Date" Then
            cl.ClearContents
        End If
    Next
End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM