在選擇中的每個單元格中,我需要替換字符組合。但在更換之前,我想計算每個組合。問題是 CountIf 函式根本不會計數。雖然替換執行。
這是一個代碼:
Option Explicit
Sub sdfsdf()
Dim rng As Range
Dim i As Integer
Dim Cell As Range
Dim Counter As Variant
'Application.Run "Personal.xlsb!Main"
Set rng = Selection
Set Cell = Cell
Let i = 1
For Each Cell In Selection
Let Counter = Application.WorksheetFunction.CountIf(Cell, i & "&")
Cell.Replace What:=i & "&", Replacement:=""
i = i 1
Next Cell
MsgBox Counter
End Sub
包含要洗掉的組合的列的片段。我需要洗掉 1&、2&、3& 等等。

uj5u.com熱心網友回復:
洗掉字符
Sub RemoveChars()
Const Criteria As String = "&"
Dim ws As Worksheet: Set ws = ActiveSheet ' improve!
Dim rg As Range
Set rg = ws.Range("U2", ws.Cells(ws.Rows.Count, "U").End(xlUp))
' Or:
'Set rg = Selection
Dim Cell As Range
Dim Position As Long
Dim cCount As Long
Dim cString As String
For Each Cell In rg.Cells
cString = CStr(Cell.Value)
Position = InStr(1, cString, Criteria)
If Position > 0 Then ' criteria found; replace with chars to the right
Cell.Value = Mid(cString, Position 1, Len(cString) - Position)
cCount = cCount 1
'Else ' criteria not found; do nothing
End If
Next Cell
MsgBox "Cells processed: " & cCount, vbInformation
End Sub
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/486704.html
