Macro to unhide rows if the values in a range are numeric and hide rows that are non numeric from the first appearance of a non numeric result

chappy

New Member
Joined
Jul 18, 2006
Messages
42
Office Version
  1. 365
Platform
  1. Windows
I would like to create a macro to Loop through a range from cell AH4 to cell AH10000 on Sheet7 in a workbook (sheet name "CALCULATIONS"). I want to ensure that for all cells in the range AH4 to AH10000 that include numeric values, the row for that cell is unhidden. In addition I want to ensure that for all cells in the range that are non numeric, the row for that cell is hidden. The data is sequential in that after the first non numeric result occurs, all the following cells to the bottom of the range will also contain a non numeric result (either text "No data" or an error value).

I have tested a lot of different code to try and resolve this but haven't managed to make anything that works well. My initial thought was to use Do Until in relation to the first appearance of a non numeric result, from that point it should be possible to select all rows from the current row to row 10000 and hide them in a single action. That seems an efficient way to approach this if it stops the loop through every remaining row in the range. However, I am stuck. If anyone can assist I would be very grateful
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
Code:
Sub t()
With ActiveSheet
    .Rows.Hidden = False
    .Range("AH4:AH1000").SpecialCells(xlCellTypeConstants, xlTextValues).EntireRow.Hidden = True
End With
End Sub
 
Upvote 0
Why loop? Assuming header in AH3
VBA Code:
ActiveSheet.Range("AH3:AH1000").AutoFilter Field:=1, Criteria1:="<>?*", Operator:=xlAnd, Criteria2:="<>"
 
Upvote 0
Looks like both suggestions may have omitted ..
all the following cells to the bottom of the range will also contain a non numeric result (either text "No data" or an error value).

If column AH are constant values, try
VBA Code:
Range("AH4:AH10000").SpecialCells(xlCellTypeConstants, 18).EntireRow.Hidden = True

If formulas ..
VBA Code:
Range("AH4:AH10000").SpecialCells(xlCellTypeFormulas, 18).EntireRow.Hidden = True
 
Upvote 0
JLGWhiz, jasonb75, Peter_SSs

Thank you all for your responses - All three of you have helped a lot. From your answers I realise that I probably left out some key information. The column I am referring to includes a number based on formula. Cell AH4 will always contain 1 and the numbers thereafter are sequential. As a result (if fully populated) the cells would be as follows AH4 = 1, AH5 = 2, down to AH10000 = 9996. Therefore I want to hide any rows that do not have a value between 1 and 9996.

I have adjusted based on your responses and come up with the following which has worked through initial tests at least. Many thanks again for your help and fast replies!

VBA Code:
Sub HideCalculationRows() 'Hide rows on "CALCULATIONS" sheet'

'Set Calculations sheet in a variable'
Set calculationsSheet = ActiveWorkbook.Sheets("CALCULATIONS")

calculationsSheet.Activate

With ActiveSheet
    .Rows.Hidden = False
    .AutoFilterMode = False
    .Range("AH3:AH10000").AutoFilter
    .Range("AH3:AH1000").AutoFilter Field:=1, Criteria1:=">=0", Operator:=xlAnd, Criteria2:="<=9996"

End With

End Sub
 
Upvote 0
Looks like both suggestions may have omitted ..


If column AH are constant values, try
VBA Code:
Range("AH4:AH10000").SpecialCells(xlCellTypeConstants, 18).EntireRow.Hidden = True

If formulas ..
VBA Code:
Range("AH4:AH10000").SpecialCells(xlCellTypeFormulas, 18).EntireRow.Hidden = True
I had tried that, but two things did not fall into place. One was that if formulas were used to create the mumerical sequence, then all the rows would be hidden and the other thing was that VBA did not seem to recognize the Excel error codes in cells with formulas, which seemed odd.
 
Upvote 0
One was that if formulas were used to create the mumerical sequence, then all the rows would be hidden and the other thing was that VBA did not seem to recognize the Excel error codes in cells with formulas, which seemed odd.
I did not have either experience. Argument of 18 in SpecialCells is Text Values + Error values. Numerical values returned by formula remained visible while text and error formula results were hidden.
 
Upvote 0
I did not have either experience. Argument of 18 in SpecialCells is Text Values + Error values. Numerical values returned by formula remained visible while text and error formula results were hidden.
Yes, using 18 it hid the text rows OK, but left the error values. I will have to reconstruct and see where I might have fouled up. cul.
 
Upvote 0
I will have to reconstruct and see where I might have fouled up.
My test setup was like this & I tested on column AH for formulas and AG for constants.

20 03 01.xlsm
AGAH
3Hdr AGHdr AH
433
555
666
799
8xx
9ee
10ww
11#N/A#N/A
12#N/A#N/A
13#N/A#N/A
14#N/A#N/A
Hide rows
Cell Formulas
RangeFormula
AH4:AH14AH4=AG4
 
Upvote 0
This is the set up I have.
TestBase.xlsm
AFAGAH
11
22
33
44
55
66
77
88
9a
10b
11c
12d
13e
14#VALUE!
15#VALUE!
16#VALUE!
170
Sheet1
Cell Formulas
RangeFormula
AH14:AH17AH14=Y2+Z2
and this is the code
Code:
Sub t()
Range("AH2", Cells(Rows.Count, "AH").End(xlUp)).SpecialCells(xlCellTypeConstants, 18).EntireRow.Hidden = True
End Sub
This is the result;
TestBase.xlsm
AFAGAHAI
11
22
33
44
55
66
77
88
14#VALUE!
15#VALUE!
16#VALUE!
170
Sheet1
Cell Formulas
RangeFormula
AH14:AH17AH14=Y2+Z2


So I seem to have a systemic problem somewhere.
 
Upvote 0

Forum statistics

Threads
1,215,032
Messages
6,122,770
Members
449,095
Latest member
m_smith_solihull

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