英文:
How can I find word in Excel and delete the digit Before?
问题
我想拆分这个词。如果我找到了ACC,那么ACC之前的词将在VBA代码中被删除。
我找到了这段代码,但它会删除找到的文本之后的词语。
Set myRange = ActiveDocument.Content
myRange.Find.Execute FindText:="blue", _
Forward:=True
If myRange.Find.Found = True Then
myRange.SetRange (myRange.End + 1), ActiveDocument.Content.End
myRange.Delete
如果可能的话,可以使用公式函数吗? 请提出建议。
英文:
I would like to split the word. If i found ACC, The word before ACC is delete in vba code
I found this code but it will delete the word after the found text
Set myRange = ActiveDocument.Content
myRange.Find.Execute FindText:="blue", _
Forward:=True
If myRange.Find.Found = True Then
myRange.SetRange (myRange.End + 1), ActiveDocument.Content.End
myRange.Delete
If it possible can use formula function ? Please suggest
答案1
得分: 1
=MID(A1,FIND("ACC",A1),LEN(A1))
英文:
You can use this formula (instead of VBA):
=MID(A1,FIND("ACC",A1),LEN(A1))
Btw: the code you posted is for Word-VBA - not for Excel-VBA
答案2
得分: 1
=IFERROR(RIGHT(A1,LEN(A1)-FIND("ACC",A1)+1),"")
如果你想使用公式来完成,你可以使用上述公式。Mid函数现在更常用,因为它更简洁,但我提供给你不同的选择 :p
如果你想使用VBA:
Sub test()
Dim ws As Worksheet, rng As Range, arr, lRow As Long
Dim srchStr As String
srchStr = "ACC"
Set ws = ThisWorkbook.Worksheets("Sheet1")
lRow = ws.Range("D" & Rows.Count).End(xlUp).Row
Set rng = ws.Range("D1:D" & lRow)
arr = rng.Value
Dim i As Long
For i = 1 To UBound(arr, 1)
If InStr(arr(i, 1), srchStr) Then
arr(i, 1) = Mid(arr(i, 1), InStr(arr(i, 1), srchStr))
Else
arr(i, 1) = ""
End If
Next i
rng.Offset(, 1).Value = arr
End Sub
这段VBA代码也可以完成相同的任务。
英文:
If you'd want to do it with a formula, you can use
=IFERROR(RIGHT(A1,LEN(A1)-FIND("ACC",A1)+1),"")
(though Mid is less typing and more used nowadays because of it, just giving you options :p)
and if you'd want to use VBA:
Sub test()
Dim ws As Worksheet, rng As Range, arr, lRow As Long
Dim srchStr As String
srchStr = "ACC"
Set ws = ThisWorkbook.Worksheets("Sheet1")
lRow = ws.Range("D" & Rows.Count).End(xlUp).Row
Set rng = ws.Range("D1:D" & lRow)
arr = rng.Value
Dim i As Long
For i = 1 To UBound(arr, 1)
If InStr(arr(i, 1), srchStr) Then
arr(i, 1) = Mid(arr(i, 1), InStr(arr(i, 1), srchStr))
Else
arr(i, 1) = ""
End If
Next i
rng.Offset(, 1).Value = arr
End Sub
That should do it as well.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论