我想在Excel中右侧删除文本和连字符。
我的专栏示例:
Fresh Start Recovery, Inc. - Wayne
FSA Property Mgmt - Danny
Georgia Securing Svcs, Inc. - Katrina
Georgia - SFFS - Joe & Roxana
Greenwood - Lonnie
Griffis Recovery, LLC - Tyler (5% WC)
Hi Cap, Inc. - Doug
我需要它看起来像这样:
Fresh Start Recovery, Inc.
FSA Property Mgmt
Georgia Securing Svcs, Inc.
Georgia - SFFS
Greenwood
Griffis Recovery, LLC
Hi Cap, Inc.
我的代码会删除右边的文本和连字符,但如果我有两个连字符,我需要删除第二个,我的代码会删除第一个。
Dim iPos As String
Dim c As Range
For Each c In Range("a1:a" & Range("a" & Rows.Count).End(xlUp).Row)
iPos = InStr(1, c.Value, " - ")
If iPos > 0 Then
c.Value = Left(c.Value, iPos - 1)
End If
Next
End Sub
答案 0 :(得分:1)
InStrRev
通过向后查找找到最后一个字符串匹配。
此更新代码
InStrRev
查找最后一次出现的位置使用字符串函数Left$
而不是变体Left
Sub Test()
Dim rng1 As Range
Dim X
Dim lngrow As Long
Set rng1 = Range([a1], Cells(Rows.Count, "A").End(xlUp))
X = rng1.Value2
For lngrow = 1 To UBound(X)
If InStr(X(lngrow, 1), " -") Then X(lngrow, 1) = Left$(X(lngrow, 1), InStrRev(X(lngrow, 1), " -", , vbBinaryCompare) - 1)
Next
rng1.Value2 = X
End Sub
答案 1 :(得分:0)
使用InStrRev而不是InStr