我有这个代码,有90-95%的时间可以使用,但有一段时间它只会删除我要删除的部分内容。
While InStr(fnd, SCHFR) <= 0 And c <= 300
c = c + 1
fnd = Sheet1.Cells(c, 8)
Wend
If c >= 300 Then
Sheet2.Cells(r, cmn) = "Not found"
Else
cleanup = InStr(1, fnd, ": ")
finalString = Right(fnd, Len(fnd) - cleanup - 1)
Sheet2.Cells(r, cmn) = finalString
End If
scnm = scnm + 1
cmn = cmn + 1
c = 1
Wend
scnm
只是一个计数器,用于跟踪字符串中正在搜索的内容以清理它。 SCHFR
的定义如下:
Select Case scnm
Case 1
SCHFR = "First Name:"
Case 2
SCHFR = "Last Name:"
Case 3
SCHFR = "Phone:"
Case 4
SCHFR = "Seconday Phone:"
Case 5
SCHFR = "Email:"
Case 6
SCHFR = "SecondaryEmail:"
Case 7
SCHFR = "Country:"
End Select
结果回来像“名字:莎拉”,我想删除“名字:”部分,所以它只是“莎拉”,但偶尔我会得到一个像“rst名称”的单元格: Sarah“相反,而周围的所有其他人都是正确的。
答案 0 :(得分:1)
为什么不使用InStr
来查找:的索引,然后使用它来删除该位置左侧的所有内容。
示例数据:
Sub SomeSub()
Dim MyRange As Range
Dim index As Integer
Dim CellValue As String
Dim CapturedValue As String
Set MyRange = ThisWorkbook.ActiveSheet.UsedRange.Columns(1)
For Each cell In MyRange.Cells
CellValue = cell.Value
' +1 caters for the sapce between the : and then data
index = InStr(1, CellValue, ":") + 1
If Len(CellValue) > index Then
CapturedValue = Right(CellValue, Len(CellValue) - index)
Else
'No Data has been inseted after the :
CapturedValue = ""
End If
cell.Value = CapturedValue
Next cell
End Sub
答案 1 :(得分:0)
这个简单的代码对我很有用(假设你有&#34;名字:Sarah&#34;在A栏;它只显示&#34; Sarah&#34;在B栏):
Sub remove_until()
Dim i, lrowA, remChar As Long
Dim mString As String
lrowA = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row
For i = 1 To lrowA
mString = Cells(i, 1).Value
remChar = InStr(mString, ":") + 1
Cells(i, 2).Value = Right(mString, Len(mString) - remChar)
Next
End Sub