我创建了一个宏来为名为"data"的工作表中存在的数据创建数据透视。对于“记录”表中存在的数据也是如此。这些表格的内容每次都具有可比性。如果这两张表中有任何不匹配,我想编写一个宏来更改单元格的颜色。
比如说在数据表中有 50 行,在记录表中有 52 行。然后我想写一个宏,记录表中两个不匹配的行应该是红色,其余 50 应该是绿色。
任何帮助,将不胜感激。
我的代码:创建支点是
Dim bReport As Workbook, Report As Worksheet, pivotSheet As Worksheet
Set bReport = Excel.ActiveWorkbook
Set Report = bReport.Worksheets("data")
Set pivotSheet = bReport.Worksheets.Add
Dim pivotSource As Range
Set pivotSource = Report.UsedRange 'selecting entire data in the sheet
Dim tableName As String
tableName = "Pivot_Data"
bReport.PivotCaches.Add(SourceType:=xlDatabase, SourceData:=pivotSource).CreatePivotTable _
TableDestination:=pivotSheet.Cells(1, 1), tableName:=tableName
Set pt = pivotSheet.PivotTables(tableName)
pivotSheet.PivotTableWizard TableDestination:=pivotSheet.Cells(1, 1)
Set pOne= pt.PivotFields("Number")
Set pTwo = pt.PivotFields("Premium")
Set pthree = pt.PivotFields("TransactoinID")
Set pFour = pt.PivotFields("money")
pOne.Orientation = xlRowField
pTwo.Orientation = xlRowField
pTwo.Subtotals(1) = False
pThree.Orientation = xlRowField
pThree.Subtotals(1) = False
pFour.Orientation = xlDataField
pFour.NumberFormat = "$#,##0.00"
我也为记录表编写了相同的代码。
我尝试使用此代码更改颜色,但在 If 条件下出现对象 438 错误。这是解决我的问题的错误方法还是可以发生任何改进?
Sub abc()
Dim rCell As Range
For Each rCell In Sheet1.Cells 'or Sheet1.Range("A1:D2").Cells
If rCell.Value2 <> Sheet2.Range(rCell.AddressLocal).Value2 Then
With rCell.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.Color = 65535 'YELLOW, make this the color of your choice
.TintAndShade = 0
.PatternTintAndShade = 0
End With
With Sheet2.Range(rCell.AddressLocal).Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.Color = 65500 'YELLOW, make this the color of your choice
.TintAndShade = 0
.PatternTintAndShade = 0
End With
End If
Next rCell
End Sub