5:00AM 5:13AM
6:10AM 6:26AM
6:40AM 6:56AM
7:25AM 7:41AM
8:15AM 8:28AM
9:10AM 9:24AM
10:10AM 10:23AM
11:10AM 11:26AM
12:02PM 12:15PM
12:50PM 1:06PM
I am trying to create a macro where I need to replace the word AM or PM in the current cell with just the time without AM and PM if the above cell has AM or PM
For example the times in first row remains the same, the rows from 2nd row just shows times like 6:10 and 6:26 because the above cell has AM. Similarly for the PM row as well. The first row containing PM remains same but the later rows just shows numbers.
My current code is this and it doesn't seem to be working.. my excel file just crashes and have to restart EXCEL Required end table
'Remove additional AM and PM
Dim i as Long, j as Long
Dim celltxt as String
For i= 2 To 200
j=3
Do While j<50
celltxt = Cells(i-1,j).Text
If InStr( 1, celltext,("AM" Or "PM")) Then
Cells(i,j).Replace What:="AM" Or "PM", Replacement:=" ", SearchOrder:=xlByColumns, MatchCase:=True
Else
End If
Loop
Next i
End Sub
I think you need to try this code:
'Remove additional AM and PM
Dim i as Long, j as Long
Dim celltxt as String
For i= 2 To 200
For j = 3 To 49
celltxt = Cells(i-1,j).Text
If InStr( 1, celltext,("AM" Or "PM")) Then
Cells(i,j).Replace What:="AM" Or "PM", Replacement:=" ", SearchOrder:=xlByColumns, MatchCase:=True
Else
End If
Next j
Next i
End Sub
Hope this help
1) Your code crashed as you don't increase the counter ( j
) of your inner loop, so the code is trapped in an endless loop (really easy to figure out when you use the debugger)
2) I think the replace doesn't accept the or
-Clause. Instead, you have to issue 2 separate replace statements.
3) There is no reason to loop over all the data. You can replace all strings at once (maybe you have to adapt the adress of the range):
Dim r As Range
Set r = Range("C1:AW200")
r.Replace What:="AM", Replacement:="", LookAt:=xlPart, MatchCase:=False
r.Replace What:="PM", Replacement:="", LookAt:=xlPart, MatchCase:=False
4) Maybe a much better solution is to create real dates out of your strings and format them correctly.
I think i figured it out
'Remove additional AM and PM
For m = 100 To 2 Step -1
For n = 3 To 30
celltxt = Cells(m - 1, n).Text
If celltxt Like "*AM*" Then
Cells(m, n).Replace What:="AM", Replacement:=" "
Else
End If
Next n
Next m
For m = 100 To 2 Step -1
For n = 3 To 30
celltxt = Cells(m - 1, n).Text
If celltxt Like "*PM*" Then
Cells(m, n).Replace What:="PM", Replacement:=" "
Else
End If
Next n
Next m
For m = 100 To 2 Step -1
For n = 3 To 30
celltxt = Cells(m - 1, n).Text
If celltxt Like "*-*" Then
Cells(m, n).Replace What:="PM", Replacement:=" "
Else
End If
Next n
Next m
For m = 100 To 2 Step -1
For n = 3 To 30
celltxt = Cells(m - 1, n).Text
If celltxt Like "*-*" Then
Cells(m, n).Replace What:="AM", Replacement:=" "
Else
End If
Next n
Next m
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.