Hiding Rows Based On Different Column Data Ranges2019 Community Moderator ElectionCopy a Row of Data based on a conditional 'Date' value from sheet2 to sheet1Copy certain excel columns based on ones criteriaHiding the column label “Values” in pivot table using VBAHide Selected Columns in Excel using (VBA) Click ButtonExcel VBA - Hide range of rows after variable sized pivot tableSumming up a dynamic range of multiple columns and rowshide column based on cell font color in a row vbaExcel VBA macro to send emails to unique users in rangeHide multiple rows based on multiple ranges cell value VBAHow to select a pivot table data range from another sheet
Has a sovereign Communist government ever run, and conceded loss, on a fair election?
Giving a talk in my old university, how prominently should I tell students my salary?
Problems with rounding giving too many digits
What's the best tool for cutting holes into duct work?
Is every open circuit a capacitor?
Is there a math expression equivalent to the conditional ternary operator?
Short story about an infectious indestructible metal bar?
When to use the term transposed instead of modulation?
Iron deposits mined from under the city
Why doesn't "adolescent" take any articles in "listen to adolescent agonising"?
Does the US political system, in principle, allow for a no-party system?
Under what conditions would I NOT add my Proficiency Bonus to a Spell Attack Roll (or Saving Throw DC)?
Why do we call complex numbers “numbers” but we don’t consider 2 vectors numbers?
Should I use HTTPS on a domain that will only be used for redirection?
Naming Characters after Friends/Family
How do you make a gun that shoots melee weapons and/or swords?
Too soon for a plot twist?
A bug in Excel? Conditional formatting for marking duplicates also highlights unique value
Can a Mimic (container form) actually hold loot?
Where do you go through passport control when transiting through another Schengen airport on your way out of the Schengen area?
PTiJ: How should animals pray?
Should we avoid writing fiction about historical events without extensive research?
In the world of The Matrix, what is "popping"?
What is "desert glass" and what does it do to the PCs?
Hiding Rows Based On Different Column Data Ranges
2019 Community Moderator ElectionCopy a Row of Data based on a conditional 'Date' value from sheet2 to sheet1Copy certain excel columns based on ones criteriaHiding the column label “Values” in pivot table using VBAHide Selected Columns in Excel using (VBA) Click ButtonExcel VBA - Hide range of rows after variable sized pivot tableSumming up a dynamic range of multiple columns and rowshide column based on cell font color in a row vbaExcel VBA macro to send emails to unique users in rangeHide multiple rows based on multiple ranges cell value VBAHow to select a pivot table data range from another sheet
I am fairly new to using VBA and am trying to create a code that will look at two different columns with varying data ranges and hide rows beyond the last data point (referencing both columns).
At the moment I have this;
Private Sub Worksheet_PivotTableUpdate(ByVal Target As PivotTable)
'Updateby Extendoffice 20160913
Dim xRg As Range
Application.ScreenUpdating = False
For Each xRg In Range("G24:G71, N24:N71")
If xRg.Value = "" Then
xRg.EntireRow.Hidden = True
Else
xRg.EntireRow.Hidden = False
End If
Next xRg
Application.ScreenUpdating = True
End Sub
Column G and Column N are peoples names in two separate pivot tables. So depending on the day the range of data in each of these columns can differ (the pivot table has different filters). For example today there could be 50 rows of data in Column G and 40 in Column N. In this case the above formula would work and hide rows 51 to 71 with no data in. However, if Column G has 40 rows of data and Column N has 50 rows then it would reference column G and hide rows 41 - 71, hiding unwanted data from column N.
Is there a way to get the code to look at Columns G & N, identify which has a larger data range and hide rows beyond that point.
Thanks in advance for any help.
excel vba
New contributor
add a comment |
I am fairly new to using VBA and am trying to create a code that will look at two different columns with varying data ranges and hide rows beyond the last data point (referencing both columns).
At the moment I have this;
Private Sub Worksheet_PivotTableUpdate(ByVal Target As PivotTable)
'Updateby Extendoffice 20160913
Dim xRg As Range
Application.ScreenUpdating = False
For Each xRg In Range("G24:G71, N24:N71")
If xRg.Value = "" Then
xRg.EntireRow.Hidden = True
Else
xRg.EntireRow.Hidden = False
End If
Next xRg
Application.ScreenUpdating = True
End Sub
Column G and Column N are peoples names in two separate pivot tables. So depending on the day the range of data in each of these columns can differ (the pivot table has different filters). For example today there could be 50 rows of data in Column G and 40 in Column N. In this case the above formula would work and hide rows 51 to 71 with no data in. However, if Column G has 40 rows of data and Column N has 50 rows then it would reference column G and hide rows 41 - 71, hiding unwanted data from column N.
Is there a way to get the code to look at Columns G & N, identify which has a larger data range and hide rows beyond that point.
Thanks in advance for any help.
excel vba
New contributor
1
Is it that you want to hide the extra Rows of data taking limit the column with less rows filled? So if Column G has 70 rows and Column N hast 75, hide from 71 to 75?
– Damian
yesterday
Hi Damian, to add a bit more context Column G and Column N are peoples names in two separate pivot tables. So depending on the day the range of data in each of these columns can differ (the pivot table has different filters). For example today there could be 50 rows of data in Column G and 40 in Column N. In this case the above formula would work and hide rows 51 to 71 with no data in. However, if Column G had 40 rows of data and Column N had 50 rows then it would still reference column G and hide rows 41 - 71, hiding data from column N.
– Qwerty
yesterday
add a comment |
I am fairly new to using VBA and am trying to create a code that will look at two different columns with varying data ranges and hide rows beyond the last data point (referencing both columns).
At the moment I have this;
Private Sub Worksheet_PivotTableUpdate(ByVal Target As PivotTable)
'Updateby Extendoffice 20160913
Dim xRg As Range
Application.ScreenUpdating = False
For Each xRg In Range("G24:G71, N24:N71")
If xRg.Value = "" Then
xRg.EntireRow.Hidden = True
Else
xRg.EntireRow.Hidden = False
End If
Next xRg
Application.ScreenUpdating = True
End Sub
Column G and Column N are peoples names in two separate pivot tables. So depending on the day the range of data in each of these columns can differ (the pivot table has different filters). For example today there could be 50 rows of data in Column G and 40 in Column N. In this case the above formula would work and hide rows 51 to 71 with no data in. However, if Column G has 40 rows of data and Column N has 50 rows then it would reference column G and hide rows 41 - 71, hiding unwanted data from column N.
Is there a way to get the code to look at Columns G & N, identify which has a larger data range and hide rows beyond that point.
Thanks in advance for any help.
excel vba
New contributor
I am fairly new to using VBA and am trying to create a code that will look at two different columns with varying data ranges and hide rows beyond the last data point (referencing both columns).
At the moment I have this;
Private Sub Worksheet_PivotTableUpdate(ByVal Target As PivotTable)
'Updateby Extendoffice 20160913
Dim xRg As Range
Application.ScreenUpdating = False
For Each xRg In Range("G24:G71, N24:N71")
If xRg.Value = "" Then
xRg.EntireRow.Hidden = True
Else
xRg.EntireRow.Hidden = False
End If
Next xRg
Application.ScreenUpdating = True
End Sub
Column G and Column N are peoples names in two separate pivot tables. So depending on the day the range of data in each of these columns can differ (the pivot table has different filters). For example today there could be 50 rows of data in Column G and 40 in Column N. In this case the above formula would work and hide rows 51 to 71 with no data in. However, if Column G has 40 rows of data and Column N has 50 rows then it would reference column G and hide rows 41 - 71, hiding unwanted data from column N.
Is there a way to get the code to look at Columns G & N, identify which has a larger data range and hide rows beyond that point.
Thanks in advance for any help.
excel vba
excel vba
New contributor
New contributor
edited 19 hours ago
Qwerty
New contributor
asked yesterday
QwertyQwerty
184
184
New contributor
New contributor
1
Is it that you want to hide the extra Rows of data taking limit the column with less rows filled? So if Column G has 70 rows and Column N hast 75, hide from 71 to 75?
– Damian
yesterday
Hi Damian, to add a bit more context Column G and Column N are peoples names in two separate pivot tables. So depending on the day the range of data in each of these columns can differ (the pivot table has different filters). For example today there could be 50 rows of data in Column G and 40 in Column N. In this case the above formula would work and hide rows 51 to 71 with no data in. However, if Column G had 40 rows of data and Column N had 50 rows then it would still reference column G and hide rows 41 - 71, hiding data from column N.
– Qwerty
yesterday
add a comment |
1
Is it that you want to hide the extra Rows of data taking limit the column with less rows filled? So if Column G has 70 rows and Column N hast 75, hide from 71 to 75?
– Damian
yesterday
Hi Damian, to add a bit more context Column G and Column N are peoples names in two separate pivot tables. So depending on the day the range of data in each of these columns can differ (the pivot table has different filters). For example today there could be 50 rows of data in Column G and 40 in Column N. In this case the above formula would work and hide rows 51 to 71 with no data in. However, if Column G had 40 rows of data and Column N had 50 rows then it would still reference column G and hide rows 41 - 71, hiding data from column N.
– Qwerty
yesterday
1
1
Is it that you want to hide the extra Rows of data taking limit the column with less rows filled? So if Column G has 70 rows and Column N hast 75, hide from 71 to 75?
– Damian
yesterday
Is it that you want to hide the extra Rows of data taking limit the column with less rows filled? So if Column G has 70 rows and Column N hast 75, hide from 71 to 75?
– Damian
yesterday
Hi Damian, to add a bit more context Column G and Column N are peoples names in two separate pivot tables. So depending on the day the range of data in each of these columns can differ (the pivot table has different filters). For example today there could be 50 rows of data in Column G and 40 in Column N. In this case the above formula would work and hide rows 51 to 71 with no data in. However, if Column G had 40 rows of data and Column N had 50 rows then it would still reference column G and hide rows 41 - 71, hiding data from column N.
– Qwerty
yesterday
Hi Damian, to add a bit more context Column G and Column N are peoples names in two separate pivot tables. So depending on the day the range of data in each of these columns can differ (the pivot table has different filters). For example today there could be 50 rows of data in Column G and 40 in Column N. In this case the above formula would work and hide rows 51 to 71 with no data in. However, if Column G had 40 rows of data and Column N had 50 rows then it would still reference column G and hide rows 41 - 71, hiding data from column N.
– Qwerty
yesterday
add a comment |
2 Answers
2
active
oldest
votes
Try this one:
Private Sub Worksheet_PivotTableUpdate(ByVal Target As PivotTable)
Dim xRg As Range, xCell As Range, bHide As Boolean
Application.ScreenUpdating = False
For Each xRg In Range("G24:G71, N24:N71").Rows
bHide = True
For Each xCell In xRg.Cells
If IsEmpty(xRg.Value) = False Then
bHide = False
End If
Next xCell
xRg.EntireRow.Hidden = bHide
Next xRg
Application.ScreenUpdating = True
End Sub
Explanation of the error: your code iterates through all cells, and makes a decision based on individual cells. That is incorrect because you want to know if both cells in a row are empty.
Solution: So you should iterate rows in an outer loop, and cells inside the given row in an inner loop. Take note if there is any cell that is non-empty, and make the decision on hiding the row based on this.
Update
Sorry, my code did not work because Range("G24:G71, N24:N71")
consists of 2 .Areas,
and although .Rows.Count
returns 48, For Each
enumerates 96 "rows", each consisting of 1 cell (48 rows for each area).
I modified the code to take into account Areas:
Private Sub Worksheet_PivotTableUpdate()
Application.ScreenUpdating = False
With Range("G24:G71,N24:N71")
Dim r As Long: For r = 1 To .Areas(1).Rows.Count
Dim bHide As Boolean: bHide = True
Dim xArea As Range: For Each xArea In .Areas
If IsEmpty(xArea.Cells(r, 1).Value) = False Then
bHide = False
End If
Next xArea
.Rows(r).EntireRow.Hidden = bHide
Next r
End With
Application.ScreenUpdating = True
End Sub
I have tried this and have found the following; G24:G35 have data in & N24:N30 have data in. With the above code it is hiding rows 31:71. The aim is for the code to look at rows G and rows N, identify where the greater range is and hide rows beyond that point up until row 71. So in this example identify the data range is larger in row G (G24:35) and then hide rows G36:71. Also for it to do the same in instances where the data range is greater in row N.
– Qwerty
yesterday
Thank you, Solved & Working!
– Qwerty
16 hours ago
add a comment |
This is another way to loop pivot table. The below code loop in two different columns (Items & Quantity) and hidden rows:
Option Explicit
Sub Hide()
Dim pvtTable As PivotTable
Dim pvtItem As PivotItem
Dim pvtRow As Long
'Set table with name
Set pvtTable = ThisWorkbook.Worksheets("Sheet1").PivotTables("PivotTable1")
'Loop Items in a specific field
For Each pvtItem In pvtTable.PivotFields("Item").PivotItems
'Check conditions
pvtRow = pvtRow + 1
If pvtItem.Value = "A" Then
pvtTable.DataBodyRange.Rows(pvtRow).EntireRow.Hidden = True
ElseIf pvtItem.Value <> "A" And pvtTable.DataBodyRange.Rows(pvtRow).EntireRow.Hidden = True Then
pvtTable.DataBodyRange.Rows(pvtRow).EntireRow.Hidden = False
End If
Next pvtItem
'Loop Items in a specific field
For Each pvtItem In pvtTable.PivotFields("Quantity").PivotItems
'Check conditions
pvtRow = pvtRow + 1
If pvtItem.Value = "A" Then
pvtTable.DataBodyRange.Rows(pvtRow).EntireRow.Hidden = True
ElseIf pvtItem.Value <> "A" And pvtTable.DataBodyRange.Rows(pvtRow).EntireRow.Hidden = True Then
pvtTable.DataBodyRange.Rows(pvtRow).EntireRow.Hidden = False
End If
Next pvtItem
End Sub
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Qwerty is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55023241%2fhiding-rows-based-on-different-column-data-ranges%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Try this one:
Private Sub Worksheet_PivotTableUpdate(ByVal Target As PivotTable)
Dim xRg As Range, xCell As Range, bHide As Boolean
Application.ScreenUpdating = False
For Each xRg In Range("G24:G71, N24:N71").Rows
bHide = True
For Each xCell In xRg.Cells
If IsEmpty(xRg.Value) = False Then
bHide = False
End If
Next xCell
xRg.EntireRow.Hidden = bHide
Next xRg
Application.ScreenUpdating = True
End Sub
Explanation of the error: your code iterates through all cells, and makes a decision based on individual cells. That is incorrect because you want to know if both cells in a row are empty.
Solution: So you should iterate rows in an outer loop, and cells inside the given row in an inner loop. Take note if there is any cell that is non-empty, and make the decision on hiding the row based on this.
Update
Sorry, my code did not work because Range("G24:G71, N24:N71")
consists of 2 .Areas,
and although .Rows.Count
returns 48, For Each
enumerates 96 "rows", each consisting of 1 cell (48 rows for each area).
I modified the code to take into account Areas:
Private Sub Worksheet_PivotTableUpdate()
Application.ScreenUpdating = False
With Range("G24:G71,N24:N71")
Dim r As Long: For r = 1 To .Areas(1).Rows.Count
Dim bHide As Boolean: bHide = True
Dim xArea As Range: For Each xArea In .Areas
If IsEmpty(xArea.Cells(r, 1).Value) = False Then
bHide = False
End If
Next xArea
.Rows(r).EntireRow.Hidden = bHide
Next r
End With
Application.ScreenUpdating = True
End Sub
I have tried this and have found the following; G24:G35 have data in & N24:N30 have data in. With the above code it is hiding rows 31:71. The aim is for the code to look at rows G and rows N, identify where the greater range is and hide rows beyond that point up until row 71. So in this example identify the data range is larger in row G (G24:35) and then hide rows G36:71. Also for it to do the same in instances where the data range is greater in row N.
– Qwerty
yesterday
Thank you, Solved & Working!
– Qwerty
16 hours ago
add a comment |
Try this one:
Private Sub Worksheet_PivotTableUpdate(ByVal Target As PivotTable)
Dim xRg As Range, xCell As Range, bHide As Boolean
Application.ScreenUpdating = False
For Each xRg In Range("G24:G71, N24:N71").Rows
bHide = True
For Each xCell In xRg.Cells
If IsEmpty(xRg.Value) = False Then
bHide = False
End If
Next xCell
xRg.EntireRow.Hidden = bHide
Next xRg
Application.ScreenUpdating = True
End Sub
Explanation of the error: your code iterates through all cells, and makes a decision based on individual cells. That is incorrect because you want to know if both cells in a row are empty.
Solution: So you should iterate rows in an outer loop, and cells inside the given row in an inner loop. Take note if there is any cell that is non-empty, and make the decision on hiding the row based on this.
Update
Sorry, my code did not work because Range("G24:G71, N24:N71")
consists of 2 .Areas,
and although .Rows.Count
returns 48, For Each
enumerates 96 "rows", each consisting of 1 cell (48 rows for each area).
I modified the code to take into account Areas:
Private Sub Worksheet_PivotTableUpdate()
Application.ScreenUpdating = False
With Range("G24:G71,N24:N71")
Dim r As Long: For r = 1 To .Areas(1).Rows.Count
Dim bHide As Boolean: bHide = True
Dim xArea As Range: For Each xArea In .Areas
If IsEmpty(xArea.Cells(r, 1).Value) = False Then
bHide = False
End If
Next xArea
.Rows(r).EntireRow.Hidden = bHide
Next r
End With
Application.ScreenUpdating = True
End Sub
I have tried this and have found the following; G24:G35 have data in & N24:N30 have data in. With the above code it is hiding rows 31:71. The aim is for the code to look at rows G and rows N, identify where the greater range is and hide rows beyond that point up until row 71. So in this example identify the data range is larger in row G (G24:35) and then hide rows G36:71. Also for it to do the same in instances where the data range is greater in row N.
– Qwerty
yesterday
Thank you, Solved & Working!
– Qwerty
16 hours ago
add a comment |
Try this one:
Private Sub Worksheet_PivotTableUpdate(ByVal Target As PivotTable)
Dim xRg As Range, xCell As Range, bHide As Boolean
Application.ScreenUpdating = False
For Each xRg In Range("G24:G71, N24:N71").Rows
bHide = True
For Each xCell In xRg.Cells
If IsEmpty(xRg.Value) = False Then
bHide = False
End If
Next xCell
xRg.EntireRow.Hidden = bHide
Next xRg
Application.ScreenUpdating = True
End Sub
Explanation of the error: your code iterates through all cells, and makes a decision based on individual cells. That is incorrect because you want to know if both cells in a row are empty.
Solution: So you should iterate rows in an outer loop, and cells inside the given row in an inner loop. Take note if there is any cell that is non-empty, and make the decision on hiding the row based on this.
Update
Sorry, my code did not work because Range("G24:G71, N24:N71")
consists of 2 .Areas,
and although .Rows.Count
returns 48, For Each
enumerates 96 "rows", each consisting of 1 cell (48 rows for each area).
I modified the code to take into account Areas:
Private Sub Worksheet_PivotTableUpdate()
Application.ScreenUpdating = False
With Range("G24:G71,N24:N71")
Dim r As Long: For r = 1 To .Areas(1).Rows.Count
Dim bHide As Boolean: bHide = True
Dim xArea As Range: For Each xArea In .Areas
If IsEmpty(xArea.Cells(r, 1).Value) = False Then
bHide = False
End If
Next xArea
.Rows(r).EntireRow.Hidden = bHide
Next r
End With
Application.ScreenUpdating = True
End Sub
Try this one:
Private Sub Worksheet_PivotTableUpdate(ByVal Target As PivotTable)
Dim xRg As Range, xCell As Range, bHide As Boolean
Application.ScreenUpdating = False
For Each xRg In Range("G24:G71, N24:N71").Rows
bHide = True
For Each xCell In xRg.Cells
If IsEmpty(xRg.Value) = False Then
bHide = False
End If
Next xCell
xRg.EntireRow.Hidden = bHide
Next xRg
Application.ScreenUpdating = True
End Sub
Explanation of the error: your code iterates through all cells, and makes a decision based on individual cells. That is incorrect because you want to know if both cells in a row are empty.
Solution: So you should iterate rows in an outer loop, and cells inside the given row in an inner loop. Take note if there is any cell that is non-empty, and make the decision on hiding the row based on this.
Update
Sorry, my code did not work because Range("G24:G71, N24:N71")
consists of 2 .Areas,
and although .Rows.Count
returns 48, For Each
enumerates 96 "rows", each consisting of 1 cell (48 rows for each area).
I modified the code to take into account Areas:
Private Sub Worksheet_PivotTableUpdate()
Application.ScreenUpdating = False
With Range("G24:G71,N24:N71")
Dim r As Long: For r = 1 To .Areas(1).Rows.Count
Dim bHide As Boolean: bHide = True
Dim xArea As Range: For Each xArea In .Areas
If IsEmpty(xArea.Cells(r, 1).Value) = False Then
bHide = False
End If
Next xArea
.Rows(r).EntireRow.Hidden = bHide
Next r
End With
Application.ScreenUpdating = True
End Sub
edited 19 hours ago
answered yesterday
z32a7ulz32a7ul
1,5861926
1,5861926
I have tried this and have found the following; G24:G35 have data in & N24:N30 have data in. With the above code it is hiding rows 31:71. The aim is for the code to look at rows G and rows N, identify where the greater range is and hide rows beyond that point up until row 71. So in this example identify the data range is larger in row G (G24:35) and then hide rows G36:71. Also for it to do the same in instances where the data range is greater in row N.
– Qwerty
yesterday
Thank you, Solved & Working!
– Qwerty
16 hours ago
add a comment |
I have tried this and have found the following; G24:G35 have data in & N24:N30 have data in. With the above code it is hiding rows 31:71. The aim is for the code to look at rows G and rows N, identify where the greater range is and hide rows beyond that point up until row 71. So in this example identify the data range is larger in row G (G24:35) and then hide rows G36:71. Also for it to do the same in instances where the data range is greater in row N.
– Qwerty
yesterday
Thank you, Solved & Working!
– Qwerty
16 hours ago
I have tried this and have found the following; G24:G35 have data in & N24:N30 have data in. With the above code it is hiding rows 31:71. The aim is for the code to look at rows G and rows N, identify where the greater range is and hide rows beyond that point up until row 71. So in this example identify the data range is larger in row G (G24:35) and then hide rows G36:71. Also for it to do the same in instances where the data range is greater in row N.
– Qwerty
yesterday
I have tried this and have found the following; G24:G35 have data in & N24:N30 have data in. With the above code it is hiding rows 31:71. The aim is for the code to look at rows G and rows N, identify where the greater range is and hide rows beyond that point up until row 71. So in this example identify the data range is larger in row G (G24:35) and then hide rows G36:71. Also for it to do the same in instances where the data range is greater in row N.
– Qwerty
yesterday
Thank you, Solved & Working!
– Qwerty
16 hours ago
Thank you, Solved & Working!
– Qwerty
16 hours ago
add a comment |
This is another way to loop pivot table. The below code loop in two different columns (Items & Quantity) and hidden rows:
Option Explicit
Sub Hide()
Dim pvtTable As PivotTable
Dim pvtItem As PivotItem
Dim pvtRow As Long
'Set table with name
Set pvtTable = ThisWorkbook.Worksheets("Sheet1").PivotTables("PivotTable1")
'Loop Items in a specific field
For Each pvtItem In pvtTable.PivotFields("Item").PivotItems
'Check conditions
pvtRow = pvtRow + 1
If pvtItem.Value = "A" Then
pvtTable.DataBodyRange.Rows(pvtRow).EntireRow.Hidden = True
ElseIf pvtItem.Value <> "A" And pvtTable.DataBodyRange.Rows(pvtRow).EntireRow.Hidden = True Then
pvtTable.DataBodyRange.Rows(pvtRow).EntireRow.Hidden = False
End If
Next pvtItem
'Loop Items in a specific field
For Each pvtItem In pvtTable.PivotFields("Quantity").PivotItems
'Check conditions
pvtRow = pvtRow + 1
If pvtItem.Value = "A" Then
pvtTable.DataBodyRange.Rows(pvtRow).EntireRow.Hidden = True
ElseIf pvtItem.Value <> "A" And pvtTable.DataBodyRange.Rows(pvtRow).EntireRow.Hidden = True Then
pvtTable.DataBodyRange.Rows(pvtRow).EntireRow.Hidden = False
End If
Next pvtItem
End Sub
add a comment |
This is another way to loop pivot table. The below code loop in two different columns (Items & Quantity) and hidden rows:
Option Explicit
Sub Hide()
Dim pvtTable As PivotTable
Dim pvtItem As PivotItem
Dim pvtRow As Long
'Set table with name
Set pvtTable = ThisWorkbook.Worksheets("Sheet1").PivotTables("PivotTable1")
'Loop Items in a specific field
For Each pvtItem In pvtTable.PivotFields("Item").PivotItems
'Check conditions
pvtRow = pvtRow + 1
If pvtItem.Value = "A" Then
pvtTable.DataBodyRange.Rows(pvtRow).EntireRow.Hidden = True
ElseIf pvtItem.Value <> "A" And pvtTable.DataBodyRange.Rows(pvtRow).EntireRow.Hidden = True Then
pvtTable.DataBodyRange.Rows(pvtRow).EntireRow.Hidden = False
End If
Next pvtItem
'Loop Items in a specific field
For Each pvtItem In pvtTable.PivotFields("Quantity").PivotItems
'Check conditions
pvtRow = pvtRow + 1
If pvtItem.Value = "A" Then
pvtTable.DataBodyRange.Rows(pvtRow).EntireRow.Hidden = True
ElseIf pvtItem.Value <> "A" And pvtTable.DataBodyRange.Rows(pvtRow).EntireRow.Hidden = True Then
pvtTable.DataBodyRange.Rows(pvtRow).EntireRow.Hidden = False
End If
Next pvtItem
End Sub
add a comment |
This is another way to loop pivot table. The below code loop in two different columns (Items & Quantity) and hidden rows:
Option Explicit
Sub Hide()
Dim pvtTable As PivotTable
Dim pvtItem As PivotItem
Dim pvtRow As Long
'Set table with name
Set pvtTable = ThisWorkbook.Worksheets("Sheet1").PivotTables("PivotTable1")
'Loop Items in a specific field
For Each pvtItem In pvtTable.PivotFields("Item").PivotItems
'Check conditions
pvtRow = pvtRow + 1
If pvtItem.Value = "A" Then
pvtTable.DataBodyRange.Rows(pvtRow).EntireRow.Hidden = True
ElseIf pvtItem.Value <> "A" And pvtTable.DataBodyRange.Rows(pvtRow).EntireRow.Hidden = True Then
pvtTable.DataBodyRange.Rows(pvtRow).EntireRow.Hidden = False
End If
Next pvtItem
'Loop Items in a specific field
For Each pvtItem In pvtTable.PivotFields("Quantity").PivotItems
'Check conditions
pvtRow = pvtRow + 1
If pvtItem.Value = "A" Then
pvtTable.DataBodyRange.Rows(pvtRow).EntireRow.Hidden = True
ElseIf pvtItem.Value <> "A" And pvtTable.DataBodyRange.Rows(pvtRow).EntireRow.Hidden = True Then
pvtTable.DataBodyRange.Rows(pvtRow).EntireRow.Hidden = False
End If
Next pvtItem
End Sub
This is another way to loop pivot table. The below code loop in two different columns (Items & Quantity) and hidden rows:
Option Explicit
Sub Hide()
Dim pvtTable As PivotTable
Dim pvtItem As PivotItem
Dim pvtRow As Long
'Set table with name
Set pvtTable = ThisWorkbook.Worksheets("Sheet1").PivotTables("PivotTable1")
'Loop Items in a specific field
For Each pvtItem In pvtTable.PivotFields("Item").PivotItems
'Check conditions
pvtRow = pvtRow + 1
If pvtItem.Value = "A" Then
pvtTable.DataBodyRange.Rows(pvtRow).EntireRow.Hidden = True
ElseIf pvtItem.Value <> "A" And pvtTable.DataBodyRange.Rows(pvtRow).EntireRow.Hidden = True Then
pvtTable.DataBodyRange.Rows(pvtRow).EntireRow.Hidden = False
End If
Next pvtItem
'Loop Items in a specific field
For Each pvtItem In pvtTable.PivotFields("Quantity").PivotItems
'Check conditions
pvtRow = pvtRow + 1
If pvtItem.Value = "A" Then
pvtTable.DataBodyRange.Rows(pvtRow).EntireRow.Hidden = True
ElseIf pvtItem.Value <> "A" And pvtTable.DataBodyRange.Rows(pvtRow).EntireRow.Hidden = True Then
pvtTable.DataBodyRange.Rows(pvtRow).EntireRow.Hidden = False
End If
Next pvtItem
End Sub
answered yesterday
Error 1004Error 1004
2,2831416
2,2831416
add a comment |
add a comment |
Qwerty is a new contributor. Be nice, and check out our Code of Conduct.
Qwerty is a new contributor. Be nice, and check out our Code of Conduct.
Qwerty is a new contributor. Be nice, and check out our Code of Conduct.
Qwerty is a new contributor. Be nice, and check out our Code of Conduct.
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55023241%2fhiding-rows-based-on-different-column-data-ranges%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Is it that you want to hide the extra Rows of data taking limit the column with less rows filled? So if Column G has 70 rows and Column N hast 75, hide from 71 to 75?
– Damian
yesterday
Hi Damian, to add a bit more context Column G and Column N are peoples names in two separate pivot tables. So depending on the day the range of data in each of these columns can differ (the pivot table has different filters). For example today there could be 50 rows of data in Column G and 40 in Column N. In this case the above formula would work and hide rows 51 to 71 with no data in. However, if Column G had 40 rows of data and Column N had 50 rows then it would still reference column G and hide rows 41 - 71, hiding data from column N.
– Qwerty
yesterday