如何用vba 判断、删除连号
发布网友
发布时间:2024-10-04 18:50
我来回答
共3个回答
热心网友
时间:2024-11-24 22:17
Sub iDel(n, iRng As Range) '公共程序
'清除iRng中连号n个的单元格内容
Dim c As Range, a, i, m, lhMax
iRng.Interior.ColorIndex = 0
For Each c In iRng
a = Split(c.Value, ",")
If UBound(a) + 1 >= n Then
m = 1
lhMax = 0
For i = LBound(a) + 1 To UBound(a)
If a(i) - a(i - 1) = 1 Then
m = m + 1
lhMax = IIf(lhMax < m, m, lhMax)
Else
m = 1
End If
Next
End If
If lhMax = n Then c.Value = ""
Next
End Sub
'-------------------
'删除2连号按钮,使用下面的语句:
iDel 2, Range("b7:f15")
'删除3连号按钮,使用下面的语句:
iDel 3, Range("b7:f15")
'删除3连号按钮,使用下面的语句:
iDel 4, Range("b7:f15")
……
------------
另外,如果一个单元格内,有多个连号,程序中以最大的连号为准,进行清除内容的动作
比如:
1,2,3,11,12,13,14,15
其中有连号3,也有连号5,这时,程序判断为连号5,按连号5操作
热心网友
时间:2024-11-24 22:18
Sub 删除二连号()
Dim rng As Range
For Each rng In Sheets(1).UsedRange
If Len(rng) = 3 Then
rng.ClearContents
End If
Next
End Sub
删除二连号的,不知能否满足LZ要求
热心网友
时间:2024-11-24 22:18
Sub 删连续()
a = 6 '删除6连号 (改几就删除几连)
Dim b As Boolean
Set Myrng = [b7:f15]
For Each rng In Myrng
If Len(rng) = 2*a - 1 Then
rnga = Split(rng, ",")
For xx = a - 1 To 1 Step -1
If rnga(xx) - 1 <> rnga(xx - 1) * 1 Then b = True: Exit For
Next
If Not b Then rng.Value = ""
End If
Next
End Sub