简体   繁体   English

Excel:有没有办法替换单元格字符串的特定部分?

[英]Excel: is there a way to replace a peculiar part of a cell string?

I have an excel export with a column containing staff previous training sessions in a string. 我有一个excel出口,其列中以字符串形式包含员工以前的培训课程。 Example: First Name|Last Name|Training title|January 2018 to February 2018 示例: First Name|Last Name|Training title|January 2018 to February 2018

Due to non obligatory form fields from which data was collected, many strings have no end month value, like this January 2018 to I'm trying to find a formula to find all strings ending with to (hence followed by nothing else) and delete this orphaned part of the string so it will look like January 2018 . 由于从中收集数据的非强制性表单字段,许多字符串都没有结束月份的值,例如January 2018 to我试图找到一个公式来查找所有以to 结尾的字符串(因此后面没有其他内容)并将其删除字符串的孤立部分,因此看起来像January 2018

Find and replace is useless as it also replaces the value when there's an end month/year. 查找和替换是无用的,因为当结束月份/年份存在时,它也会替换值。 Substitute seems like a good way to go but I've failed trying to find the proper structure to the formula. 替代似乎是一个不错的方法,但我未能尝试为公式找到合适的结构。

测试它是否在结束to右,如果是这样,然后用左忽略它:

=IF(RIGHT(A1,2) = "to",LEFT(A1,LEN(A1)-2),A1)

This is with VBA. 这是VBA。 Dont forget to check for extra spaces at the end. 不要忘记最后检查多余的空间。 if there are any use TRIM. 如果有使用TRIM。

Option Explicit

Sub Remove()

    Dim Lastrow As Long, i As Long

    'Change Sheet if needed
    With ThisWorkbook.Worksheets("Sheet1")

        Lastrow = .Cells(.Rows.Count, "A").End(xlUp).Row

        For i = 1 To Lastrow

            If Right(.Range("A" & i).Value, 2) = "to" Then
                .Range("A" & i).Value = Left(.Range("A" & i).Value, Len(.Range("A" & i).Value) - 3)
            End If
        Next i


    End With

End Sub

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

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