永发信息网

excel 某列中 连续出现50个以上的空格 染色

答案:2  悬赏:0  手机版
解决时间 2021-03-10 20:04
  • 提问者网友:浮克旳回音
  • 2021-03-10 11:49
excel 某列中 不停的出现数字0和空格 连续出现50个以上的空格 染红色,连续出现40个以上50个以下的空格,其空格染成黄色,请问如何操作

既然这样 能不能 体现出40期中出现了多少个0 比如A1到A40 出现了3个0 就在B40体现3 A41到A80 出现了5个0 就在B80体现5 依次类推
最佳答案
  • 五星知识达人网友:第四晚心情
  • 2021-03-10 13:01
还是针对楼主最初染色的需求来说吧:

我虽然用VBA基本上达到了这个效果,但是始终感觉还是比较麻烦。不知道有没有更简便的办法,比如用公式?我对Excel公式不是很熟,熟悉的朋友可以再琢磨下。

用VBA实现的步骤如下:

1、新建一个Excel文件;

2、键入Alt+F11打开VBA编辑环境,然后选择菜单“插入 - 模块”,在代码编辑区粘贴如下代码:

Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)
UpdateSelection Target
End Sub

Private Sub UpdateSelection(oSelection As Range)

Dim oArea As Range
Dim nColumnIndex As Long, nMaxColumn As Long, nMaxRow As Long
Dim oColumnDict As Object
Dim vKey As Variant

Set oColumnDict = CreateObject("Scripting.Dictionary")

' Collect all columns that need to be updated
For Each oArea In oSelection.Areas
nMaxColumn = Min(ActiveSheet.UsedRange.Column + ActiveSheet.UsedRange.Columns.Count, oArea.Column + oArea.Columns.Count - 1)
nMaxRow = Min(ActiveSheet.UsedRange.Row + ActiveSheet.UsedRange.Rows.Count, oArea.Row + oArea.Rows.Count - 1)
For nColumnIndex = oArea.Column To nMaxColumn
oColumnDict(nColumnIndex) = nColumnIndex
Next
Next

For Each vKey In oColumnDict.Keys
UpdateColumn CInt(vKey)
Next

End Sub

Private Sub UpdateColumn(nColumnIndex As Long)

Dim oFirstCell As Range, oLastCell As Range, oNextCell As Range, oCalculateBand As Range
Dim nBlankCellsCount As Long
Dim vCellColor As Variant

Set oFirstCell = Cells(1, nColumnIndex)
Set oLastCell = Cells(ActiveSheet.UsedRange.Row + ActiveSheet.UsedRange.Rows.Count - 1, nColumnIndex)

' Firstly clear the interior color of the existing cells
With Range(oFirstCell, oLastCell).Interior
.Pattern = xlNone
End With

Do While True
' Find the end cell of one single band (the next nonblank cell after the band start cell)
Set oNextCell = Cells.Find(What:=nColumnIndex - 1, After:=Cells(oFirstCell.Row, nColumnIndex), LookIn:=xlValues, LookAt:= _
xlWhole, SearchOrder:=xlByColumns, SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)

If oNextCell Is Nothing Then
' Not found, move to the end cell
Set oNextCell = oLastCell
ElseIf (oNextCell.Column <> nColumnIndex) Or (oNextCell.Row <= oFirstCell.Row) Then
' Not found after the band end cell in this column, also move to the end cell
Set oNextCell = oLastCell
Else
' Found, move to the cell right above it
Set oNextCell = Cells(oNextCell.Row - 1, nColumnIndex)
End If

' Check to see if the start cell in the band needs to be included
If oFirstCell.Text = "" Then
' First cell is empty, should be counted
Set oCalculateBand = Range(oFirstCell, oNextCell)
Else
' First cell is not empty, skip it
Set oCalculateBand = Range(Cells(oFirstCell.Row + 1, nColumnIndex), oNextCell)
End If

' Calculate the band size
nBlankCellsCount = oCalculateBand.Rows.Count

With oCalculateBand.Interior
If nBlankCellsCount < 40 Then
.Pattern = xlNone
ElseIf (nBlankCellsCount >= 40) And (nBlankCellsCount < 50) Then
.Color = vbYellow
Else
.Color = vbRed
End If
End With

' Move to the next band in this column
Set oFirstCell = Cells(oNextCell.Row + 1, nColumnIndex)

' Check to see if reached the last cell
If oFirstCell.Row >= oLastCell.Row Then Exit Do
Loop

End Sub

Private Function Min(x As Long, y As Long)
Min = (x + y - Abs(x - y)) / 2
End Function

3、键入Alt+Q返回Excel主窗口,保存;

4、现在可以在当前工作簿里面试验一下,比如在第一列随机间隔输入若干0,检查两个相邻的0之间空格数量分别为小于40、40-50、50以上三种情况时,空格染色是否正确。在第二列、第三列、第四列……分别测试输入数字1、2、3、……的情况。

请注意,因为启用了宏,所以需要在“工具 - 宏 - 安全性”里面选择“中”或者“低”,并且在下次打开这个文档时要确认启用宏。

请楼主看看是不是自己想要的效果,如果有问题可以补充提问或者和我联系。
全部回答
  • 1楼网友:等灯
  • 2021-03-10 13:42
添加选矿。 ctrl+g 锁定空白单元格。 里面有这个功能。 还会有n多种功能在离面 都很实用的 vba 现成的工具。其次。 第二。 然后把空白筛选出来。 然后f5或者。 然后删除。 然后右击鼠标删除。 上网去下载个 excel 完美工具箱。选中你所有b列数据的区域。 第三。 第一。 你可以用好多多种方法来解决第一
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯