Need to delete all text in cells with time format in them??

daniels012

Well-known Member
Joined
Jan 13, 2005
Messages
5,219
I have a timesheet that I protect. I would like to write code that will:
Unprotect the sheet then...
delete all the times in cells that have borders.

Is this even possible?

Michael
 
Can I do something to this code for borders??

Sub ClearYellowCells()
Dim cl As Range
Sheets("Worksheet").Unprotect
For Each cl In ActiveSheet.UsedRange
If cl.Interior.ColorIndex = 6 Then cl.MergeArea.ClearContents
Next cl
Sheets("Worksheet").Protect
End Sub

Michael
 
Upvote 0

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
tried jindon's code with no success.
I need it to do it to the active sheet. Can I add the code to the active sheet instead of Sheet 1
 
Upvote 0
I use this code to add a new timesheet:

Code:
Sub AddSheetNew()
'
ActiveSheet.Unprotect
ActiveSheet.Shapes("Button 4").Visible = False
ActiveSheet.Select
ActiveSheet.Copy after:=Worksheets(Worksheets.Count - 1)
Range("C4") = Range("R2")
ActiveSheet.Name = Range("R1").Value
ActiveSheet.Shapes("Button 4").Visible = True
Range("TimeArea").Select
    Selection.ClearContents
Range("D6").Select
End Sub

Can I add something to clear any cell, that has a time in that cell?

Michael
 
Upvote 0
Hi
Daniel
Did I post any code here?
How about

Code:
Sub AddSheetNew()
Dim r As Range
With ActiveSheet
   .Unprotect
   .Shapes("Button 4").Visible = False
   .Copy after:=Worksheets(Worksheets.Count - 1)
End With
With ActiveSheet
   .Range("C4") = .Range("R2")
   .Name = .Range("R1").Value
   .Shapes("Button 4").Visible = True
   .Range("TimeArea").ClearContents
   For Each r In .UsedRange
      With CreateObject("VBScript.RegExp")
         .Pattern = "^\d{1,2}:\d{1,2}(:\d{1,2})?$"
         If IsDate(r) And .test(r.Text) Then
            If Int(r.Borders(xlEdgeTop).LineStyle = xlNone) + _
               Int(r.Borders(xlEdgeBottom).LineStyle = xlNone) + _
               Int(r.Borders(xlEdgeLeft).LineStyle = xlNone) + _
               Int(r.Borders(xlEdgeRight).LineStyle = xlNone) > -4 Then
               r.CelarContents
            End If
         End If
      End With
   Next
End With
End Sub
 
Upvote 0
jindon,
Thank you for responding!!!!
My times are not being erased. Should I change IsDate to IsTime?

Michael
 
Upvote 0
All of my times are with in the range
C6:I190

There are formulas with totals included in this range also this is why I want to erase the times not the formulas.

I will post a sample maybe.

Michael
 
Upvote 0
Upvote 0
That is why I asked how your cells are formatted...
How about this one?
Code:
Sub AddSheetNew()
Dim r As Range
With ActiveSheet
   .Unprotect
   .Shapes("Button 4").Visible = False
   .Copy after:=Worksheets(Worksheets.Count - 1)
End With
With ActiveSheet
   .Range("C4") = .Range("R2")
   .Name = .Range("R1").Value
   .Shapes("Button 4").Visible = True
   .Range("TimeArea").ClearContents
   For Each r In .UsedRange
      With CreateObject("VBScript.RegExp")
         .Pattern = "^\d{1,2}:\d{1,2}\s(AM|PM)$"
         If IsDate(r) And .test(r.Text) Then
            If Int(r.Borders(xlEdgeTop).LineStyle = xlNone) + _
               Int(r.Borders(xlEdgeBottom).LineStyle = xlNone) + _
               Int(r.Borders(xlEdgeLeft).LineStyle = xlNone) + _
               Int(r.Borders(xlEdgeRight).LineStyle = xlNone) > -4 Then
               r.CelarContents
            End If
         End If
      End With
   Next
End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,968
Messages
6,122,509
Members
449,089
Latest member
RandomExceller01

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top