AlliancePugs
New Member
- Joined
- Mar 24, 2025
- Messages
- 1
- Office Version
- 365
- Platform
- Windows
I am using this VBA code to automatically put an "X" in your detailed data sheet ("RawData") for each row that matches a highlighted pivot table cell. Specifically, when I highlight pivot cells green, the macro looks for rows in the RawData then marks them as selected. But I am currently facing issues that not all records are not being marked. Need help with this.
Sub MarkRawDataForGreenCells()
Dim wsPivot As Worksheet, wsData As Worksheet
Dim pt As PivotTable
Dim cell As Range, dataCell As Range
Dim dataRng As Range
Dim lastRow As Long
Dim pivotCode As Variant, pivotSource As Variant, pivotAmount As Double
Dim matched As Boolean
' Set worksheets
Set wsPivot = ThisWorkbook.Sheets("Pivot") ' Pivot Table sheet
Set wsData = ThisWorkbook.Sheets("RawData") ' Raw Data sheet
' Identify last row in RawData
lastRow = wsData.Cells(wsData.Rows.Count, "A").End(xlUp).Row
Set dataRng = wsData.Range("A1:Z" & lastRow) ' Adjust range for raw data
' Clear previous selections in "Selected" column (Column Z)
wsData.Range("M2:M" & lastRow).ClearContents ' Adjust column if needed
' Loop through Pivot Table cells
For Each cell In wsPivot.UsedRange
' Check if the cell is highlighted green
If cell.Interior.Color = RGB(0, 255, 0) Then
' Get Pivot Table object
On Error Resume Next
Set pt = cell.PivotTable
On Error GoTo 0
' If the cell is part of a Pivot Table
If Not pt Is Nothing Then
pivotAmount = cell.Value ' Get the highlighted value (Amount)
' Find related fields dynamically
pivotCode = cell.EntireRow.Cells(1, 1).Value ' "Code" is always in the first column
pivotSource = cell.EntireRow.Cells(1, 2).Value ' "Source" is the second column
' Check if Source is one of the required values
If pivotSource = "IVET" Or pivotSource = "BANK" Or pivotSource = "ACCRUAL" Or pivotSource = "Gen" Then
matched = False
' Loop through RawData to find matching entries
For Each dataCell In dataRng.Columns(1).Cells ' Column A = Code
If dataCell.Value = pivotCode And dataCell.Offset(0, 5).Value = pivotSource Then ' Column F = Source
If dataCell.Offset(0, 6).Value = pivotAmount Then ' Column G = Amount
dataCell.Offset(0, 12).Value = "X" ' Mark "Selected" column (Column Z)
matched = True
End If
End If
Next dataCell
' Debugging: Log unmatched values
If Not matched Then
Debug.Print "No match found for Code: " & pivotCode & ", Source: " & pivotSource & ", Amount: " & pivotAmount
End If
End If
End If
End If
Next cell
MsgBox "Raw data updated based on green-highlighted Pivot Table cells.", vbInformation
End Sub
Sub MarkRawDataForGreenCells()
Dim wsPivot As Worksheet, wsData As Worksheet
Dim pt As PivotTable
Dim cell As Range, dataCell As Range
Dim dataRng As Range
Dim lastRow As Long
Dim pivotCode As Variant, pivotSource As Variant, pivotAmount As Double
Dim matched As Boolean
' Set worksheets
Set wsPivot = ThisWorkbook.Sheets("Pivot") ' Pivot Table sheet
Set wsData = ThisWorkbook.Sheets("RawData") ' Raw Data sheet
' Identify last row in RawData
lastRow = wsData.Cells(wsData.Rows.Count, "A").End(xlUp).Row
Set dataRng = wsData.Range("A1:Z" & lastRow) ' Adjust range for raw data
' Clear previous selections in "Selected" column (Column Z)
wsData.Range("M2:M" & lastRow).ClearContents ' Adjust column if needed
' Loop through Pivot Table cells
For Each cell In wsPivot.UsedRange
' Check if the cell is highlighted green
If cell.Interior.Color = RGB(0, 255, 0) Then
' Get Pivot Table object
On Error Resume Next
Set pt = cell.PivotTable
On Error GoTo 0
' If the cell is part of a Pivot Table
If Not pt Is Nothing Then
pivotAmount = cell.Value ' Get the highlighted value (Amount)
' Find related fields dynamically
pivotCode = cell.EntireRow.Cells(1, 1).Value ' "Code" is always in the first column
pivotSource = cell.EntireRow.Cells(1, 2).Value ' "Source" is the second column
' Check if Source is one of the required values
If pivotSource = "IVET" Or pivotSource = "BANK" Or pivotSource = "ACCRUAL" Or pivotSource = "Gen" Then
matched = False
' Loop through RawData to find matching entries
For Each dataCell In dataRng.Columns(1).Cells ' Column A = Code
If dataCell.Value = pivotCode And dataCell.Offset(0, 5).Value = pivotSource Then ' Column F = Source
If dataCell.Offset(0, 6).Value = pivotAmount Then ' Column G = Amount
dataCell.Offset(0, 12).Value = "X" ' Mark "Selected" column (Column Z)
matched = True
End If
End If
Next dataCell
' Debugging: Log unmatched values
If Not matched Then
Debug.Print "No match found for Code: " & pivotCode & ", Source: " & pivotSource & ", Amount: " & pivotAmount
End If
End If
End If
End If
Next cell
MsgBox "Raw data updated based on green-highlighted Pivot Table cells.", vbInformation
End Sub