比较宏

时间:2013-12-12 15:46:08

标签: excel excel-vba comparison vba

我有一个执行以下操作的宏:

SETUP:

  1. 比较“April Count”和“Prg-Srv Data”之间的ID#,并将共同的ID转换为绿色单元格背景。
  2. 过滤公共数据(具有绿色单元格背景的任何内容)并将其复制到新工作表“Medicaid Report”。然后清除AutoFilter并将工作表格式化为指定的样式。
  3. 过滤并删除包含“Duplicate”一词的所有行。
  4. 最后,它将April Count与Medicaid报告进行比较,看看是否有人错过了4月份的点数。
  5. 问题就是这样:

    当宏完成时,它仍然是“随机”标记4月份计数中的数据,这也是医疗补助报告中的数据,我不确定我做错了什么。

    此外,如果有一种更有效的方法可以让我知道,这个宏需要很长时间才能运行,我不确定它是否只是因为它必须要做5,000多条记录或者我编码效率低下。谢谢

    CODE:

    Sub ComparePrgSrv()
        'Get the last row
        Dim Report As Worksheet
        Dim Report2 As Worksheet
        Dim Report3 As Worksheet
        Dim i, j, k As Integer
        Dim LastRow, LastRow2, LastRow3 As Integer
        Dim UniqueVal As New Collection
        Dim Val As String
    
        Set Report = Excel.Worksheets("April Count")
        Set Report2 = Excel.Worksheets("Prg-Srv Data")
        Set Report3 = Excel.Worksheets("Medicaid Report")
    
        LastRow = Report.UsedRange.Rows.count
        LastRow2 = Report2.UsedRange.Rows.count
        LastRow3 = Report3.UsedRange.Rows.count
    
        Application.ScreenUpdating = False
    
        'April Count to Program Services comparison.
        For i = 2 To LastRow2
            For j = 2 To LastRow
                If Report2.Cells(i, 1).Value <> "" Then 'This will omit blank cells at the end (in the event that the column lengths are not equal.
                    If InStr(1, Report.Cells(j, 1).Value, Report2.Cells(i, 1).Value, vbTextCompare) > 0 Then
                        Report2.Cells(i, 1).Interior.Color = RGB(0, 102, 51) 'Dark green background
                        Report2.Cells(i, 1).Font.Color = RGB(0, 204, 102) 'Light green font color
                        Exit For
                    Else
                        Report2.Cells(i, 1).Interior.Color = xlNone 'Transparent background
                        Report2.Cells(i, 1).Font.Color = RGB(0, 0, 0) 'Black font color
                    End If
                End If
            Next j
        Next i
    
        'Filter Program Services to show correct data.
        Report2.Range("$A$1:$M$" & LastRow2).AutoFilter Field:=1, Criteria1:=RGB(0, 102, 51), Operator:=xlFilterCellColor
    
        'Copy filtered data to new worksheet.
        Report2.Range("$A$1:$M$" & LastRow2).SpecialCells(xlCellTypeVisible).Copy Destination:=Worksheets("Medicaid Report").Range("A1")
    
        'Clear filter selection on both sheets.
        Report.AutoFilterMode = False
        Report2.AutoFilterMode = False
    
        'Format cell colors on Medicaid sheet.
        Report3.UsedRange.Interior.Color = xlNone 'Transparent background
        Report3.UsedRange.Font.Color = RGB(0, 0, 0) 'Black font color
    
        Report3.Range("$A$1:$M$1").Interior.Color = RGB(31, 73, 125) 'Blue background
        Report3.Range("$A$1:$M$1").Font.Color = RGB(255, 255, 255) 'White font color
    
        'Filter and Delete Rows Containing "DUPLICATE"
        With ActiveSheet
        .AutoFilterMode = False
        With Range("B1", Range("B" & Rows.count).End(xlUp))
            .AutoFilter 1, "*DUPLICATE*"
            On Error Resume Next
            .Offset(1).SpecialCells(12).EntireRow.Delete
        End With
        .AutoFilterMode = False
        End With
    
        'April Count to Medicaid Report comparison.
        For i = 2 To LastRow
            For j = 2 To LastRow3
                If Report.Cells(i, 1).Value <> "" Then 'This will omit blank cells at the end (in the event that the column lengths are not equal.
                    If InStr(1, Report3.Cells(j, 1).Value, Report.Cells(i, 1).Value, vbTextCompare) > 0 Then
                        Report.Cells(i, 1).Interior.Color = xlNone 'Transparent background
                        Report.Cells(i, 1).Font.Color = RGB(0, 0, 0) 'Black font color
                        Exit For
                    Else
                        Report.Cells(i, 1).Interior.Color = RGB(156, 0, 6) 'Dark red background
                        Report.Cells(i, 1).Font.Color = RGB(255, 199, 206) 'Light red font color
                    End If
                End If
            Next j
        Next i
    End Sub
    

    工作簿设置: enter image description here

1 个答案:

答案 0 :(得分:0)

首先,你是什么意思

  

“当宏完成时”

有效性部分:
您应该删除If Report2.Cells(i, 1).Value <> "" Then,因为它已经考虑了InStr。如果单元格为空InStr将评估为0;这应该加快一点。

其次,你应该使用这个来获取最后一行数据:

LastRow = Report.Range("a" & Report.Rows.Count).End(xlUp).Row
LastRow2 = Report2.Range("a" & Report2.Rows.Count).End(xlUp).Row
LastRow3 = Report3.Range("a" & Report3.Rows.Count).End(xlUp).Row

“a”是包含要检查的数据的列。这将为您提供目标列的最后一个非空行,而不是整个工作表的总使用范围。

另外,在VBA中,当你在一行上声明变量时,这个:
Dim i, j, k As Integer
只会将“k”声明为Integer,但“i”和“j”将为Variant

你应该把它写成:
Dim i As Integer, j As Integer, k As IntegerDim LastRow, LastRow2, LastRow3 As Integer

的相同评论

请不要忘记在退出Application.ScreenUpdating之前启用Sub