|
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
自已ExcelDIY工具中的代码,里面CreateObject("ExDIY.F")相关的函数都是比较基础的,自己重新实现下就好。
- 'X:在文本中搜索的内容
- 'iIndexOfX:从第几个X处开始
- 'iNumToDelete:删除字符的数量
- 'bFromTop:删除的方向,向前或向后
- 'bReverseSearch:是否逆向搜索
- Sub RemoveTextByContents(WorkRange As Range, x$, iIndexOfX As Long, iNumToDelete As Long, bFromTop As Boolean, bReverseSearch As Boolean, _
- Optional bModifyFormula As Boolean, Optional bOmitHiddenCell As Boolean)
- Dim a As Range
- Dim CellCount As Long
- Dim TotalCells As Long
- Dim item As Range
- Dim BeginPosition As Long
- Dim iLenX As Long, iLenS As Long
- Dim iNum As Long
- iLenX = Len(x)
-
-
- Dim sht As Worksheet, arr
-
- For Each sht In ActiveWindow.SelectedSheets
- For Each item In CreateObject("ExDIY.F").GetValidRange(sht.Range(WorkRange.Address), bOmitHiddenCell).Areas
- If bModifyFormula Then
- arr = CreateObject("ExDIY.F").Range2Array(item, "Formula")
- Else
- arr = CreateObject("ExDIY.F").Range2Array(item)
- End If
- For i = LBound(arr, 1) To UBound(arr, 1)
- For j = LBound(arr, 2) To UBound(arr, 2)
- arr(i, j) = CStr(arr(i, j))
-
- iLenS = Len(arr(i, j))
-
- If iLenS = 0 Then GoTo Nexta
- '搜索X位置
- If bReverseSearch Then
- BeginPosition = CreateObject("ExDIY.F").InstrExRev(CStr(arr(i, j)), x, iIndexOfX)
- Else
- BeginPosition = CreateObject("ExDIY.F").InstrEx(CStr(arr(i, j)), x, iIndexOfX)
- End If
- '计算开始位置
- Dim iNumToDelete_ As Long
- If BeginPosition Then
- If bFromTop Then
- If BeginPosition < iLenS Then
- If iLenS - BeginPosition - iLenX - iNumToDelete + 1 < 0 Then
- iNumToDelete_ = iLenS - BeginPosition - iLenX + 1
- Else
- iNumToDelete_ = iNumToDelete
- End If
- arr(i, j) = Left(arr(i, j), BeginPosition + iLenX - 1) & Right(arr(i, j), iLenS - BeginPosition - iLenX - iNumToDelete_ + 1)
- End If
- Else
-
- If BeginPosition > 1 Then
- If BeginPosition - iNumToDelete - 1 < 0 Then
- iNumToDelete_ = BeginPosition - 1
- Else
- iNumToDelete_ = iNumToDelete
- End If
- arr(i, j) = Left(arr(i, j), BeginPosition - iNumToDelete_ - 1) & Right(arr(i, j), iLenS - BeginPosition + 1)
- End If
- End If
- End If
- Nexta:
- Next j
- Next i
- CreateObject("ExDIY.F").FillRange item, arr
- Next item
- Next sht
- End Sub
复制代码
|
|