DataRange off by 1 row

mbarillaro

New Member
Joined
Feb 12, 2020
Messages
8
Office Version
  1. 2019
Platform
  1. Windows
I have a pivot table w/ 1 column, no rows and multiple values. When there is not pivotField for my rows, the datarange as I loop through my pivotitems in my pivot table are off by 1 row. So, the datarange returned to me is down 1 row from where the datarange should start. If I set this to True for my pivotTable (DisplayFieldCaptions = True), the dataRange will show the correct dataRange. As anyone seen this issue? A sample of my code is listed below. The pivot table is structured like the following;

1 Column
values for the Rows
Values: price, cost, discounted_price

Dim PvtItem As PivotItem
Dim UnionRng As Range
Dim XRange As Range
Dim pvtTable As PivotTable
Dim pvtParentRowField As Variant
Dim RowFieldNm As Variant

'ActiveSheet Pivot Table
Set pvtTable = ActiveSheet.PivotTables(1)

'Work around to get correct DataRange when there are no row fields
pvtTable.DisplayFieldCaptions = True

'Pivot Tables First RowFields Name
Set pvtParentRowField = pvtTable.RowFields(1)

'Cast pvtParentRowField RowField to RowFieldNm Variant
RowFieldNm = pvtParentRowField

For Each PvtItem In ActiveSheet.PivotTables(1).PivotFields(RowFieldNm).PivotItems
On Error Resume Next

On Error GoTo 0
If Not XRange Is Nothing Then
If UnionRng Is Nothing Then
Set UnionRng = XRange

Else
Set UnionRng = Union(UnionRng, XRange)
End If
End If
Next PvtItem

If DisableLinks = False Then
'Change all pivotCells to look like hyperlinks
UnionRng.Font.Underline = xlUnderlineStyleSingle
UnionRng.Font.Color = RGB(5, 99, 193)
Application.EnableEvents = True
Else
'Reset all pivotCells to remove hyperlinks
UnionRng.Font.Underline = xlUnderlineStyleNone
UnionRng.Font.Color = RGB(0, 0, 0)
Application.EnableEvents = False
End If
End Sub
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"

Forum statistics

Threads
1,214,606
Messages
6,120,484
Members
448,967
Latest member
visheshkotha

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top