简体   繁体   English

根据另一个单元格中的文本替换字符串的一部分

[英]Replace part of a string based on text in another cell

5:00AM 5:13AM 5:00 AM 5:13 AM
6:10AM 6:26AM 上午6:10上午6:26
6:40AM 6:56AM 上午6:40上午6:56
7:25AM 7:41AM 上午7:25上午7:41
8:15AM 8:28AM 上午8:15上午8:28
9:10AM 9:24AM 9:10 AM 9:24 AM
10:10AM 10:23AM 上午10:10上午10:23
11:10AM 11:26AM 11:10 AM 11:26 AM
12:02PM 12:15PM 下午12:02下午12:15
12:50PM 1:06PM 12:50 PM 1:06 PM

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 我正在尝试创建一个宏,如果上面的单元格具有AM或PM,则需要用没有AM和PM的时间替换当前单元格中的AM或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. 例如,第一行中的时间保持不变,第二行中的行仅显示类似于6:10和6:26的时间,因为上述单元格具有AM。 Similarly for the PM row as well. 对于PM行也是如此。 The first row containing PM remains same but the later rows just shows numbers. 包含PM的第一行保持不变,但随后的行仅显示数字。

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 我当前的代码是这个,它似乎不起作用..我的excel文件崩溃了,不得不重新启动EXCEL 必需的终端表

'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) 1)您的代码崩溃了,因为您没有增加内部循环的计数器( j ),因此代码陷入了一个无限循环中(使用调试器时很容易弄清楚)

2) I think the replace doesn't accept the or -Clause. 2)我认为替换不接受or条款。 Instead, you have to issue 2 separate replace statements. 相反,您必须发出2条单独的replace语句。

3) There is no reason to loop over all the data. 3)没有理由遍历所有数据。 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. 4)也许更好的解决方案是从字符串中创建实际日期并正确设置其格式。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 用部分原始文本替换单元格范围中的字符串 - replace string in cell range with part of the original text 在文本字符串中查找文本字符串,并用另一个单元格中的红色文本字符串替换部分原始文本字符串 - Find Text String Within a Text String and Replace Part of Original Text String With Red Text String From Another Cell 使用VBA用另一个单元格中的文本替换单元格中的部分文本 - Replace part of text in a cell with text from another cell using VBA 用另一个字符串替换包含一个字符串的单元格中的文本 - Replace text in a cell containing a string with another string Excel:根据另一个单元格(字符串)删除部分单元格(字符串) - Excel: Remove part of cell (String) based on another cell (String) 根据另一个单元格中的值编辑一个单元格中的部分文本的公式 - Formula to edit part of a text in one cell based on an values in another cell VBA:如何从一个单元格复制一部分字符串,并用它替换另一单元格的一部分? - VBA: How to copy part of the string from one cell and replace part of another cell with it? 替换和删除部分单元格文本 - Replace and delete part of a cell text 如何将一个单元格中的一部分字符串替换为另一单元格中的字符串? - How do I replace part of a string from one cell from a string in another cell? 如何使用格式化文本查找和替换单元格的一部分 - How to find and replace part of a cell with formatted text
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM