如何利用VBA快速的标记出缺失值

在日常的数据处理中,我们经常会遇到各种各样的缺失值,虽然简单的筛选就可以找出来缺失值,但是如果经常性的要对这些缺失值进行标注和处理,筛选工具就显得不太够用了。在这里我将遇到的关于筛选缺失值的一些案例分享一下,这里主要使用的是VBA。

1.函数准备

首先,先定义一个寻找相应列名称的函数,将数字转换为字母,方便下一步的数据选取

Function get_col(col_num) As Byte()
 '输出字母形式的列名称
    If col_num <= 26 Then
        col_str = Chr(64 + col_num)
    Else
        b_num = col_num \ 26
        e_num = col_num Mod 26
        col_str = Chr(64 + b_num) + Chr(64 + e_num)
    End If
    get_col = col_str
End Function

在调用的话,直接传入相应的数字,例如get_col(26),返回Z ;get_col(27),返回AA

2.基于列的数据空缺值标注

arr1:传入一个数组

check_list传入一个需要判断缺失列的序号

color_num:颜色值

Sub col_remark(arr1, check_list, color_num)
'空缺值位置标注列
Dim i, j, num As Integer
'进行颜色标注
For i = 0 To UBound(check_list)
    num = 0
    For j = 1 To UBound(arr1)
        '每个单元格进行颜色标注
        If arr1(j, check_list(i)) = "" Then
            num = num + 1
            Cells(j, check_list(i)).Interior.Color = color_num
        End If
     Next j
     '相应列进行颜色标注
     If num > 0 Then
        Cells(1, check_list(i)).Interior.Color = color_num
    End If
Next i
End Sub

3.基于行的数据空缺值标注,并生成新的工作表

各参数的用法同2

Sub index_remark(arr1, check_list, color_num)
'空缺值位置标注行
Dim i, j, x, num As Integer
Dim arr_miss()
'进行颜色标注
ReDim arr_miss(1 To UBound(arr1), 1 To UBound(arr1, 2))
k = 0  'arr_miss的行,将首行的标题写入新表
For i = 1 To UBound(arr1)
    num = 0  '记录每行缺失的个数
    '首行标题写入数组
    If k = 0 Then
        k = k + 1
        For x = 1 To UBound(arr1, 2)
            arr_miss(k, x) = arr1(i, x)
        Next x
    End If
     '每个单元格进行颜色标注
    For j = 0 To UBound(check_list)
        If arr1(i, check_list(j)) = "" Then
            num = num + 1
            Cells(i, check_list(j)).Interior.Color = color_num
        End If
     Next j
     '相应行进行颜色标注
     If num > 0 Then
        Cells(i, 1).Interior.Color = color_num
        '写入相应的数据再arr_miss
        k = k + 1
        For x = 1 To UBound(arr1, 2)
            arr_miss(k, x) = arr1(i, x)
        Next x
    End If
Next i
'删除缺失数据工作表
new_name = "缺失数据"
Application.DisplayAlerts = False
For Each sht In Sheets
    If sht.name = new_name Then sht.Delete
Next sht
Application.DisplayAlerts = True
'添加新工作表
Sheets.Add After:=Sheets(Sheets.Count)
Sheets(Sheets.Count).name = new_name
'写入数据
Sheets(new_name).Range("a1").Resize(k, UBound(arr_miss, 2)) = arr_miss
Range("1:1").Font.Bold = True
End Sub

4.完整代码及调用

在进行使用时,直接将代码拷贝,并直接运行main_func就可以了

Function get_col(col_num) As Byte()
 '输出字母形式的列名称
    If col_num <= 26 Then
        col_str = Chr(64 + col_num)
    Else
        b_num = col_num \ 26
        e_num = col_num Mod 26
        col_str = Chr(64 + b_num) + Chr(64 + e_num)
    End If
    get_col = col_str
End Function

Sub main_func()
Dim arr_missing(), check()
Dim i As Integer
'输入要处理的工作表名称
sheet_name = "Sheet1"
'输入需要寻找的缺失列序号
check = Array(2, 4)
'输入要标记的底框颜色,可以输入0-16777215,255为红色
mark_color = 25589
'是否要进行全部行处理
check_all = False
'将数据放入数组
Sheets(sheet_name).Select
row_num = [a65536].End(xlUp).Row
col_num = ActiveSheet.UsedRange.Columns.Count
col_str = get_col(col_num)
arr_missing = Range("a1:" & col_str & row_num)
If check_all Then
    ReDim check(1 To col_num)
    For i = 1 To col_num
        check(i) = i
    Next i
End If
'删除缺失数据工作表
new_name = "标记数据"
Application.DisplayAlerts = False
For Each sht In Sheets
    If sht.name = new_name Then sht.Delete
Next sht
Application.DisplayAlerts = True
'添加新工作表
Sheets.Add After:=Sheets(Sheets.Count)
Sheets(Sheets.Count).name = new_name
'写入数据
Sheets(new_name).Range("a1").Resize(UBound(arr_missing), UBound(arr_missing, 2)) = arr_missing
Range("1:1").Font.Bold = True
'调用数据处理
Call col_remark(arr_missing, check, mark_color)
Call index_remark(arr_missing, check, mark_color)
End Sub

Sub col_remark(arr1, check_list, color_num)
'空缺值位置标注列
Dim i, j, num As Integer
'进行颜色标注
For i = LBound(check_list) To UBound(check_list)
    num = 0
    For j = 1 To UBound(arr1)
        '每个单元格进行颜色标注
        If arr1(j, check_list(i)) = "" Then
            num = num + 1
            Cells(j, check_list(i)).Interior.Color = color_num
        End If
     Next j
     '相应列进行颜色标注
     If num > 0 Then
        Cells(1, check_list(i)).Interior.Color = color_num
    End If
Next i
End Sub
Sub index_remark(arr1, check_list, color_num)
'空缺值位置标注行
Dim i, j, x, num As Integer
Dim arr_miss()
'进行颜色标注
ReDim arr_miss(1 To UBound(arr1), 1 To UBound(arr1, 2))
k = 0  'arr_miss的行,将首行的标题写入新表
For i = 1 To UBound(arr1)
    num = 0  '记录每行缺失的个数
    '首行标题写入数组
    If k = 0 Then
        k = k + 1
        For x = 1 To UBound(arr1, 2)
            arr_miss(k, x) = arr1(i, x)
        Next x
    End If
     '每个单元格进行颜色标注
    For j = LBound(check_list) To UBound(check_list)
        If arr1(i, check_list(j)) = "" Then
            num = num + 1
            Cells(i, check_list(j)).Interior.Color = color_num
        End If
     Next j
     '相应行进行颜色标注
     If num > 0 Then
        Cells(i, 1).Interior.Color = color_num
        '写入相应的数据再arr_miss
        k = k + 1
        For x = 1 To UBound(arr1, 2)
            arr_miss(k, x) = arr1(i, x)
        Next x
    End If
Next i
'删除缺失数据工作表
new_name = "缺失数据"
Application.DisplayAlerts = False
For Each sht In Sheets
    If sht.name = new_name Then sht.Delete
Next sht
Application.DisplayAlerts = True
'添加新工作表
Sheets.Add After:=Sheets(Sheets.Count)
Sheets(Sheets.Count).name = new_name
'写入数据
Sheets(new_name).Range("a1").Resize(k, UBound(arr_miss, 2)) = arr_miss
Range("1:1").Font.Bold = True
End Sub



5.使用例子

在这里举一个简单的例子,便于理解,原数据如下:

现在只需要找出姓名和性别缺失就可以了,使用如下:

运行结束,效果如下:

如果需要对全部列进行处理,只用将check_all的值改为True就可以了!

写在最后:数据处理有多种方式,有时候换种方式来处理,也有不同的乐趣!根据数据量和数据形式的展现,使用不同的工具,往往可以达到意想不到的效果!

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
可以使用以下VBA代码来标记Sparkline的最大值和最小值: ``` Sub MarkSparklineMinMax() Dim sparklineRange As Range Dim cell As Range Set sparklineRange = Selection '选择包含Sparkline的单元格范围 For Each cell In sparklineRange If cell.SparklineGroups.Count > 0 Then '检查单元格是否包含Sparkline Dim sparklineGroup As SparklineGroup Set sparklineGroup = cell.SparklineGroups(1) '获取第一个SparklineGroup Dim values As Variant values = sparklineGroup.Points '获取Sparkline的数据点 Dim maxValue As Double Dim maxIndex As Integer maxValue = Application.WorksheetFunction.Max(values) '查找最大值 maxIndex = Application.WorksheetFunction.Match(maxValue, values, 0) '查找最大值的索引 Dim minValue As Double Dim minIndex As Integer minValue = Application.WorksheetFunction.Min(values) '查找最小值 minIndex = Application.WorksheetFunction.Match(minValue, values, 0) '查找最小值的索引 cell.FormatConditions.Delete '删除所有条件格式 '标记最大值 Dim maxRule As FormatCondition Set maxRule = cell.FormatConditions.Add(xlCellValue, xlEqual, maxValue) maxRule.Font.Color = vbRed maxRule.AppliesTo.Range("B1").Offset(maxIndex - 1).Font.Color = vbRed '标记最小值 Dim minRule As FormatCondition Set minRule = cell.FormatConditions.Add(xlCellValue, xlEqual, minValue) minRule.Font.Color = vbBlue minRule.AppliesTo.Range("B1").Offset(minIndex - 1).Font.Color = vbBlue End If Next cell End Sub ``` 此代码将选择的单元格中的Sparkline的最大值和最小值标记为红色和蓝色。请注意,此代码假定Sparkline在B1单元格中,并且Sparkline的数据点存储在SparklineGroup的Points属性中。您可以根据您的情况修改代码。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

theskylife

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值