Hide Rows Automatically via Cell Result Change
up vote
1
down vote
favorite
Sub hide()
Dim wb As Workbook
Dim ws As Worksheet
Dim c As Range
Dim targetRange As Range
Set wb = ThisWorkbook
Set ws = wb.Sheets("Ballast Quote")
Set targetRange = ws.Range("A20:A30")
targetRange.EntireRow.Hidden = False
For Each c In targetRange.Rows
If (WorksheetFunction.CountIf(c, "<>0") - WorksheetFunction.CountIf(c, "") = 0) And (WorksheetFunction.CountA(c) - WorksheetFunction.Count(c) = 0) Then
c.EntireRow.Hidden = True
End If
Next c
End Sub
This macro works perfectly but i would rather not have to go in and hit "F5" every time a range of cell changes. My issue is that the cells that trigger the macro are not changed manually but are a result of another sheet, example cell A20 shows "=Inventory!$L$41". I have not found any answers on this on options to have a button or if cell is changed manually. Any takers???
excel vba excel-vba
New contributor
add a comment |
up vote
1
down vote
favorite
Sub hide()
Dim wb As Workbook
Dim ws As Worksheet
Dim c As Range
Dim targetRange As Range
Set wb = ThisWorkbook
Set ws = wb.Sheets("Ballast Quote")
Set targetRange = ws.Range("A20:A30")
targetRange.EntireRow.Hidden = False
For Each c In targetRange.Rows
If (WorksheetFunction.CountIf(c, "<>0") - WorksheetFunction.CountIf(c, "") = 0) And (WorksheetFunction.CountA(c) - WorksheetFunction.Count(c) = 0) Then
c.EntireRow.Hidden = True
End If
Next c
End Sub
This macro works perfectly but i would rather not have to go in and hit "F5" every time a range of cell changes. My issue is that the cells that trigger the macro are not changed manually but are a result of another sheet, example cell A20 shows "=Inventory!$L$41". I have not found any answers on this on options to have a button or if cell is changed manually. Any takers???
excel vba excel-vba
New contributor
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
Sub hide()
Dim wb As Workbook
Dim ws As Worksheet
Dim c As Range
Dim targetRange As Range
Set wb = ThisWorkbook
Set ws = wb.Sheets("Ballast Quote")
Set targetRange = ws.Range("A20:A30")
targetRange.EntireRow.Hidden = False
For Each c In targetRange.Rows
If (WorksheetFunction.CountIf(c, "<>0") - WorksheetFunction.CountIf(c, "") = 0) And (WorksheetFunction.CountA(c) - WorksheetFunction.Count(c) = 0) Then
c.EntireRow.Hidden = True
End If
Next c
End Sub
This macro works perfectly but i would rather not have to go in and hit "F5" every time a range of cell changes. My issue is that the cells that trigger the macro are not changed manually but are a result of another sheet, example cell A20 shows "=Inventory!$L$41". I have not found any answers on this on options to have a button or if cell is changed manually. Any takers???
excel vba excel-vba
New contributor
Sub hide()
Dim wb As Workbook
Dim ws As Worksheet
Dim c As Range
Dim targetRange As Range
Set wb = ThisWorkbook
Set ws = wb.Sheets("Ballast Quote")
Set targetRange = ws.Range("A20:A30")
targetRange.EntireRow.Hidden = False
For Each c In targetRange.Rows
If (WorksheetFunction.CountIf(c, "<>0") - WorksheetFunction.CountIf(c, "") = 0) And (WorksheetFunction.CountA(c) - WorksheetFunction.Count(c) = 0) Then
c.EntireRow.Hidden = True
End If
Next c
End Sub
This macro works perfectly but i would rather not have to go in and hit "F5" every time a range of cell changes. My issue is that the cells that trigger the macro are not changed manually but are a result of another sheet, example cell A20 shows "=Inventory!$L$41". I have not found any answers on this on options to have a button or if cell is changed manually. Any takers???
excel vba excel-vba
excel vba excel-vba
New contributor
New contributor
edited Nov 19 at 14:24
Pᴇʜ
19.3k42650
19.3k42650
New contributor
asked Nov 19 at 14:18
Jake Lenis
82
82
New contributor
New contributor
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
up vote
0
down vote
accepted
Use the Worksheet.Change Event on your inventory
worksheet to test if the original data changed:
'this must be in Inventory worksheet!
Private Sub Worksheet_Change(ByVal Target as Range)
If Not Intersect(Target, Me.Range("L41")) Is Nothing Then
hide 'run your procedure to hide
End If
End Sub
This tests if Inventory!$L$41
changed and if so it runs your hide procedure.
I have just inserted code for Inventory worksheet! and same issue occurs. Cell value changes but rows remain unchanged??? This has been my dilemma.
– Jake Lenis
Nov 19 at 14:38
either it is spelled wrong, or in the wrong module, workbook or worksheet file
– DaanV
Nov 19 at 14:40
This should definitely work if you change the value in L41 of the inventory sheet. Put a breakpoint in your code to see if it gets triggered. Then use F8 to go through it step by step.
– Pᴇʜ
Nov 19 at 14:42
Is it possible it does not work because L41 is also a formulated cell in the other sheet? cell shows as "=SUMIF(B7:B39,K11,C7:C39)"
– Jake Lenis
Nov 19 at 14:51
of course this cannot work with formulas. You must trigger it on the original value (not formula) change. So changeRange("L41")
toRange("B7:B39,K11,C7:C39")
in the code I provided.
– Pᴇʜ
Nov 19 at 14:57
|
show 4 more comments
up vote
0
down vote
I suggest looking into Events, these are usually Sub or function called whenever something happens to a sheet/workbook
The particular event you want is: Worksheet.Change (https://docs.microsoft.com/en-us/dotnet/api/microsoft.office.tools.excel.worksheet.change?view=vsto-2017)
Or Worksheet.Calculate (https://docs.microsoft.com/en-us/dotnet/api/microsoft.office.tools.excel.worksheet.calculate?view=vsto-2017)
You can hook that sub to activate yours or enter your code in there. The documentation is in C# but uses the same Events, properties and methods
This does not work on formulas (OP has formulas) only if the value of the cell changes by user edit or VBA edit.
– Pᴇʜ
Nov 19 at 14:27
well you can also do the event of the data sheet and activate the hiding or showing on the result sheet. or you can use Worksheet.Calculate
– DaanV
Nov 19 at 14:28
I have tried some a couple "worksheet.Change" and calculation scenarios but nothing has worked. I am pretty new to the VBA and have learnt a ton the last week or so. Unfortunately i cannot call the macro above my own. If you have an example i can paste in to "view code" that i can try that would be a good starting point. Thanks in advance.
– Jake Lenis
Nov 19 at 14:31
@JakeLenis if you put in a break does it then trigger?
– DaanV
Nov 19 at 14:34
i have inserted a break with no luck
– Jake Lenis
Nov 19 at 14:57
|
show 1 more comment
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
Use the Worksheet.Change Event on your inventory
worksheet to test if the original data changed:
'this must be in Inventory worksheet!
Private Sub Worksheet_Change(ByVal Target as Range)
If Not Intersect(Target, Me.Range("L41")) Is Nothing Then
hide 'run your procedure to hide
End If
End Sub
This tests if Inventory!$L$41
changed and if so it runs your hide procedure.
I have just inserted code for Inventory worksheet! and same issue occurs. Cell value changes but rows remain unchanged??? This has been my dilemma.
– Jake Lenis
Nov 19 at 14:38
either it is spelled wrong, or in the wrong module, workbook or worksheet file
– DaanV
Nov 19 at 14:40
This should definitely work if you change the value in L41 of the inventory sheet. Put a breakpoint in your code to see if it gets triggered. Then use F8 to go through it step by step.
– Pᴇʜ
Nov 19 at 14:42
Is it possible it does not work because L41 is also a formulated cell in the other sheet? cell shows as "=SUMIF(B7:B39,K11,C7:C39)"
– Jake Lenis
Nov 19 at 14:51
of course this cannot work with formulas. You must trigger it on the original value (not formula) change. So changeRange("L41")
toRange("B7:B39,K11,C7:C39")
in the code I provided.
– Pᴇʜ
Nov 19 at 14:57
|
show 4 more comments
up vote
0
down vote
accepted
Use the Worksheet.Change Event on your inventory
worksheet to test if the original data changed:
'this must be in Inventory worksheet!
Private Sub Worksheet_Change(ByVal Target as Range)
If Not Intersect(Target, Me.Range("L41")) Is Nothing Then
hide 'run your procedure to hide
End If
End Sub
This tests if Inventory!$L$41
changed and if so it runs your hide procedure.
I have just inserted code for Inventory worksheet! and same issue occurs. Cell value changes but rows remain unchanged??? This has been my dilemma.
– Jake Lenis
Nov 19 at 14:38
either it is spelled wrong, or in the wrong module, workbook or worksheet file
– DaanV
Nov 19 at 14:40
This should definitely work if you change the value in L41 of the inventory sheet. Put a breakpoint in your code to see if it gets triggered. Then use F8 to go through it step by step.
– Pᴇʜ
Nov 19 at 14:42
Is it possible it does not work because L41 is also a formulated cell in the other sheet? cell shows as "=SUMIF(B7:B39,K11,C7:C39)"
– Jake Lenis
Nov 19 at 14:51
of course this cannot work with formulas. You must trigger it on the original value (not formula) change. So changeRange("L41")
toRange("B7:B39,K11,C7:C39")
in the code I provided.
– Pᴇʜ
Nov 19 at 14:57
|
show 4 more comments
up vote
0
down vote
accepted
up vote
0
down vote
accepted
Use the Worksheet.Change Event on your inventory
worksheet to test if the original data changed:
'this must be in Inventory worksheet!
Private Sub Worksheet_Change(ByVal Target as Range)
If Not Intersect(Target, Me.Range("L41")) Is Nothing Then
hide 'run your procedure to hide
End If
End Sub
This tests if Inventory!$L$41
changed and if so it runs your hide procedure.
Use the Worksheet.Change Event on your inventory
worksheet to test if the original data changed:
'this must be in Inventory worksheet!
Private Sub Worksheet_Change(ByVal Target as Range)
If Not Intersect(Target, Me.Range("L41")) Is Nothing Then
hide 'run your procedure to hide
End If
End Sub
This tests if Inventory!$L$41
changed and if so it runs your hide procedure.
answered Nov 19 at 14:32
Pᴇʜ
19.3k42650
19.3k42650
I have just inserted code for Inventory worksheet! and same issue occurs. Cell value changes but rows remain unchanged??? This has been my dilemma.
– Jake Lenis
Nov 19 at 14:38
either it is spelled wrong, or in the wrong module, workbook or worksheet file
– DaanV
Nov 19 at 14:40
This should definitely work if you change the value in L41 of the inventory sheet. Put a breakpoint in your code to see if it gets triggered. Then use F8 to go through it step by step.
– Pᴇʜ
Nov 19 at 14:42
Is it possible it does not work because L41 is also a formulated cell in the other sheet? cell shows as "=SUMIF(B7:B39,K11,C7:C39)"
– Jake Lenis
Nov 19 at 14:51
of course this cannot work with formulas. You must trigger it on the original value (not formula) change. So changeRange("L41")
toRange("B7:B39,K11,C7:C39")
in the code I provided.
– Pᴇʜ
Nov 19 at 14:57
|
show 4 more comments
I have just inserted code for Inventory worksheet! and same issue occurs. Cell value changes but rows remain unchanged??? This has been my dilemma.
– Jake Lenis
Nov 19 at 14:38
either it is spelled wrong, or in the wrong module, workbook or worksheet file
– DaanV
Nov 19 at 14:40
This should definitely work if you change the value in L41 of the inventory sheet. Put a breakpoint in your code to see if it gets triggered. Then use F8 to go through it step by step.
– Pᴇʜ
Nov 19 at 14:42
Is it possible it does not work because L41 is also a formulated cell in the other sheet? cell shows as "=SUMIF(B7:B39,K11,C7:C39)"
– Jake Lenis
Nov 19 at 14:51
of course this cannot work with formulas. You must trigger it on the original value (not formula) change. So changeRange("L41")
toRange("B7:B39,K11,C7:C39")
in the code I provided.
– Pᴇʜ
Nov 19 at 14:57
I have just inserted code for Inventory worksheet! and same issue occurs. Cell value changes but rows remain unchanged??? This has been my dilemma.
– Jake Lenis
Nov 19 at 14:38
I have just inserted code for Inventory worksheet! and same issue occurs. Cell value changes but rows remain unchanged??? This has been my dilemma.
– Jake Lenis
Nov 19 at 14:38
either it is spelled wrong, or in the wrong module, workbook or worksheet file
– DaanV
Nov 19 at 14:40
either it is spelled wrong, or in the wrong module, workbook or worksheet file
– DaanV
Nov 19 at 14:40
This should definitely work if you change the value in L41 of the inventory sheet. Put a breakpoint in your code to see if it gets triggered. Then use F8 to go through it step by step.
– Pᴇʜ
Nov 19 at 14:42
This should definitely work if you change the value in L41 of the inventory sheet. Put a breakpoint in your code to see if it gets triggered. Then use F8 to go through it step by step.
– Pᴇʜ
Nov 19 at 14:42
Is it possible it does not work because L41 is also a formulated cell in the other sheet? cell shows as "=SUMIF(B7:B39,K11,C7:C39)"
– Jake Lenis
Nov 19 at 14:51
Is it possible it does not work because L41 is also a formulated cell in the other sheet? cell shows as "=SUMIF(B7:B39,K11,C7:C39)"
– Jake Lenis
Nov 19 at 14:51
of course this cannot work with formulas. You must trigger it on the original value (not formula) change. So change
Range("L41")
to Range("B7:B39,K11,C7:C39")
in the code I provided.– Pᴇʜ
Nov 19 at 14:57
of course this cannot work with formulas. You must trigger it on the original value (not formula) change. So change
Range("L41")
to Range("B7:B39,K11,C7:C39")
in the code I provided.– Pᴇʜ
Nov 19 at 14:57
|
show 4 more comments
up vote
0
down vote
I suggest looking into Events, these are usually Sub or function called whenever something happens to a sheet/workbook
The particular event you want is: Worksheet.Change (https://docs.microsoft.com/en-us/dotnet/api/microsoft.office.tools.excel.worksheet.change?view=vsto-2017)
Or Worksheet.Calculate (https://docs.microsoft.com/en-us/dotnet/api/microsoft.office.tools.excel.worksheet.calculate?view=vsto-2017)
You can hook that sub to activate yours or enter your code in there. The documentation is in C# but uses the same Events, properties and methods
This does not work on formulas (OP has formulas) only if the value of the cell changes by user edit or VBA edit.
– Pᴇʜ
Nov 19 at 14:27
well you can also do the event of the data sheet and activate the hiding or showing on the result sheet. or you can use Worksheet.Calculate
– DaanV
Nov 19 at 14:28
I have tried some a couple "worksheet.Change" and calculation scenarios but nothing has worked. I am pretty new to the VBA and have learnt a ton the last week or so. Unfortunately i cannot call the macro above my own. If you have an example i can paste in to "view code" that i can try that would be a good starting point. Thanks in advance.
– Jake Lenis
Nov 19 at 14:31
@JakeLenis if you put in a break does it then trigger?
– DaanV
Nov 19 at 14:34
i have inserted a break with no luck
– Jake Lenis
Nov 19 at 14:57
|
show 1 more comment
up vote
0
down vote
I suggest looking into Events, these are usually Sub or function called whenever something happens to a sheet/workbook
The particular event you want is: Worksheet.Change (https://docs.microsoft.com/en-us/dotnet/api/microsoft.office.tools.excel.worksheet.change?view=vsto-2017)
Or Worksheet.Calculate (https://docs.microsoft.com/en-us/dotnet/api/microsoft.office.tools.excel.worksheet.calculate?view=vsto-2017)
You can hook that sub to activate yours or enter your code in there. The documentation is in C# but uses the same Events, properties and methods
This does not work on formulas (OP has formulas) only if the value of the cell changes by user edit or VBA edit.
– Pᴇʜ
Nov 19 at 14:27
well you can also do the event of the data sheet and activate the hiding or showing on the result sheet. or you can use Worksheet.Calculate
– DaanV
Nov 19 at 14:28
I have tried some a couple "worksheet.Change" and calculation scenarios but nothing has worked. I am pretty new to the VBA and have learnt a ton the last week or so. Unfortunately i cannot call the macro above my own. If you have an example i can paste in to "view code" that i can try that would be a good starting point. Thanks in advance.
– Jake Lenis
Nov 19 at 14:31
@JakeLenis if you put in a break does it then trigger?
– DaanV
Nov 19 at 14:34
i have inserted a break with no luck
– Jake Lenis
Nov 19 at 14:57
|
show 1 more comment
up vote
0
down vote
up vote
0
down vote
I suggest looking into Events, these are usually Sub or function called whenever something happens to a sheet/workbook
The particular event you want is: Worksheet.Change (https://docs.microsoft.com/en-us/dotnet/api/microsoft.office.tools.excel.worksheet.change?view=vsto-2017)
Or Worksheet.Calculate (https://docs.microsoft.com/en-us/dotnet/api/microsoft.office.tools.excel.worksheet.calculate?view=vsto-2017)
You can hook that sub to activate yours or enter your code in there. The documentation is in C# but uses the same Events, properties and methods
I suggest looking into Events, these are usually Sub or function called whenever something happens to a sheet/workbook
The particular event you want is: Worksheet.Change (https://docs.microsoft.com/en-us/dotnet/api/microsoft.office.tools.excel.worksheet.change?view=vsto-2017)
Or Worksheet.Calculate (https://docs.microsoft.com/en-us/dotnet/api/microsoft.office.tools.excel.worksheet.calculate?view=vsto-2017)
You can hook that sub to activate yours or enter your code in there. The documentation is in C# but uses the same Events, properties and methods
edited Nov 19 at 14:27
answered Nov 19 at 14:26
DaanV
3476
3476
This does not work on formulas (OP has formulas) only if the value of the cell changes by user edit or VBA edit.
– Pᴇʜ
Nov 19 at 14:27
well you can also do the event of the data sheet and activate the hiding or showing on the result sheet. or you can use Worksheet.Calculate
– DaanV
Nov 19 at 14:28
I have tried some a couple "worksheet.Change" and calculation scenarios but nothing has worked. I am pretty new to the VBA and have learnt a ton the last week or so. Unfortunately i cannot call the macro above my own. If you have an example i can paste in to "view code" that i can try that would be a good starting point. Thanks in advance.
– Jake Lenis
Nov 19 at 14:31
@JakeLenis if you put in a break does it then trigger?
– DaanV
Nov 19 at 14:34
i have inserted a break with no luck
– Jake Lenis
Nov 19 at 14:57
|
show 1 more comment
This does not work on formulas (OP has formulas) only if the value of the cell changes by user edit or VBA edit.
– Pᴇʜ
Nov 19 at 14:27
well you can also do the event of the data sheet and activate the hiding or showing on the result sheet. or you can use Worksheet.Calculate
– DaanV
Nov 19 at 14:28
I have tried some a couple "worksheet.Change" and calculation scenarios but nothing has worked. I am pretty new to the VBA and have learnt a ton the last week or so. Unfortunately i cannot call the macro above my own. If you have an example i can paste in to "view code" that i can try that would be a good starting point. Thanks in advance.
– Jake Lenis
Nov 19 at 14:31
@JakeLenis if you put in a break does it then trigger?
– DaanV
Nov 19 at 14:34
i have inserted a break with no luck
– Jake Lenis
Nov 19 at 14:57
This does not work on formulas (OP has formulas) only if the value of the cell changes by user edit or VBA edit.
– Pᴇʜ
Nov 19 at 14:27
This does not work on formulas (OP has formulas) only if the value of the cell changes by user edit or VBA edit.
– Pᴇʜ
Nov 19 at 14:27
well you can also do the event of the data sheet and activate the hiding or showing on the result sheet. or you can use Worksheet.Calculate
– DaanV
Nov 19 at 14:28
well you can also do the event of the data sheet and activate the hiding or showing on the result sheet. or you can use Worksheet.Calculate
– DaanV
Nov 19 at 14:28
I have tried some a couple "worksheet.Change" and calculation scenarios but nothing has worked. I am pretty new to the VBA and have learnt a ton the last week or so. Unfortunately i cannot call the macro above my own. If you have an example i can paste in to "view code" that i can try that would be a good starting point. Thanks in advance.
– Jake Lenis
Nov 19 at 14:31
I have tried some a couple "worksheet.Change" and calculation scenarios but nothing has worked. I am pretty new to the VBA and have learnt a ton the last week or so. Unfortunately i cannot call the macro above my own. If you have an example i can paste in to "view code" that i can try that would be a good starting point. Thanks in advance.
– Jake Lenis
Nov 19 at 14:31
@JakeLenis if you put in a break does it then trigger?
– DaanV
Nov 19 at 14:34
@JakeLenis if you put in a break does it then trigger?
– DaanV
Nov 19 at 14:34
i have inserted a break with no luck
– Jake Lenis
Nov 19 at 14:57
i have inserted a break with no luck
– Jake Lenis
Nov 19 at 14:57
|
show 1 more comment
Jake Lenis is a new contributor. Be nice, and check out our Code of Conduct.
Jake Lenis is a new contributor. Be nice, and check out our Code of Conduct.
Jake Lenis is a new contributor. Be nice, and check out our Code of Conduct.
Jake Lenis 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%2f53376577%2fhide-rows-automatically-via-cell-result-change%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