首页 > 解决方案 > 根据数组删除匹配的 SSN

问题描述

我有一个私人子,我在代码末尾调用它。其目的是删除其 SSN 与正在运行的数组匹配的任何个人。我有两张纸可以搜索,第二张效果很好,但是第一张似乎不起作用。

我试图调试它,它找到了我关心的一个人与数组匹配的正确数字,但If statement似乎并没有影响它。这是我所拥有的,任何和所有的帮助将不胜感激。同样的问题是第一个If statement工作不正常。出于安全原因,我显然更改了 SSN。再次感谢!

Private Sub Delete_Deceased()

    Dim tw As Workbook: Set tw = ThisWorkbook
    Dim visa As Worksheet: Set visa = tw.Worksheets("Visa")
    Dim nav As Worksheet: Set nav = tw.Worksheets("Navigator")
    Dim rwCnt_visa As Long: rwCnt_visa = visa.Cells(Rows.Count, 1).End(xlUp).Row
    Dim rwCnt_nav As Long: rwCnt_nav = nav.Cells(Rows.Count, 1).End(xlUp).Row
    Dim x As Long
    Dim y As Long
    Dim Deceased() As Variant
    Deceased = Array(123456789, 234567890, 345678901, 456789012, 567890123, 678901234, _
        789012345, 890123456, 901234567)
    Dim visa_Inx As Long
    Dim nav_Inx As Long

    visa.Columns("P:P").NumberFormat = "@"  'Format the VISA SSN column as text

    For x = rwCnt_visa To 4 Step -1
        For visa_Inx = LBound(Deceased) To UBound(Deceased)
            If visa.Cells(x, 16).Value2 = Deceased(visa_Inx) Then
                Debug.Print "Row: " & x
                'visa.Rows(x).Delete shift:=xlShiftUp
            End If
        Next visa_Inx
    Next x

    For y = rwCnt_nav To 2 Step -1
        For nav_Inx = LBound(Deceased) To UBound(Deceased)
            If nav.Cells(y, 5).Value2 = Deceased(nav_Inx) Then
                nav.Rows(y).Delete shift:=xlShiftUp
            End If
        Next nav_Inx
    Next y

End Sub

标签: arraysexcelvbaif-statement

解决方案


似乎是基础数据的问题,也许尝试CLng在单元格值上使用。

 If CLng(visa.Cells(x, 16).Value2) = Deceased(visa_Inx) Then

推荐阅读