Excel VBA Hide rows that don't contain value

%3CLINGO-SUB%20id%3D%22lingo-sub-1288749%22%20slang%3D%22en-US%22%3EExcel%20VBA%20Hide%20rows%20that%20don't%20contain%20value%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1288749%22%20slang%3D%22en-US%22%3E%3CP%3EHi%2C%3CBR%20%2F%3E%3CBR%20%2F%3EI%20have%20produced%20the%20below%20code%20that%20hides%20rows%20if%20they%20contain%20specific%20values%20which%20works%20fine%2C%20but%20now%20i%20want%20to%20do%20the%20opposite%20and%20hide%20all%20rows%20that%20DON'T%20contain%20the%20same%20values.%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-markup%22%3E%3CCODE%3ERows.EntireRow.Hidden%20%3D%20False%0A%0A%20%20%20%20BeginRow%20%3D%202%0A%20%20%20%20EndRow%20%3D%20Cells(Rows.Count%2C%20%22A%22).End(xlUp).Row%0A%20%20%20%20ChkCol%20%3D%201%0A%0A%20%20%20%20For%20RowCnt%20%3D%20BeginRow%20To%20EndRow%0A%20%20%20%20%20%20%20%20If%20Cells(RowCnt%2C%20ChkCol).Value%20Like%20%22ZZZZ*%22%20Or%20Cells(RowCnt%2C%20ChkCol).Value%20Like%20%22ENDO005%22%20Or%20Cells(RowCnt%2C%20ChkCol).Value%20Like%20%22ENDO007%22%20Or%20Cells(RowCnt%2C%20ChkCol).Value%20Like%20%22ENDO009%22%20Or%20Cells(RowCnt%2C%20ChkCol).Value%20Like%20%22ENDO011%22%20Or%20Cells(RowCnt%2C%20ChkCol).Value%20Like%20%22ORTO020%22%20Or%20Cells(RowCnt%2C%20ChkCol).Interior.ColorIndex%20%3D%206%20Then%0A%20%20%20%20%20%20%20%20%20%20%20%20Cells(RowCnt%2C%20ChkCol).EntireRow.Hidden%20%3D%20True%0A%20%20%20%20%20%20%20%20%20%20%20%20Else%0A%20%20%20%20%20%20%20%20%20%20%20%20Cells(RowCnt%2C%20ChkCol).EntireRow.Hidden%20%3D%20False%0A%20%20%20%20%20%20%20%20End%20If%0A%20%20%20%20Next%20RowCnt%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20was%20though%20it%20would%20be%20as%20simple%20as%20swapping%20the%20true%20and%20false%20over%2C%20but%20that%20doesn't%20seem%20to%20work%20and%20i'm%20not%20sure%20how%20to%20go%20about%20making%20it%20do%20what%20i%20want.%3CBR%20%2F%3E%3CBR%20%2F%3EAnyone%20able%20to%20help%3F%3CBR%20%2F%3E%3CBR%20%2F%3EThanks%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-1288749%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EExcel%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EMacros%20and%20VBA%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1288966%22%20slang%3D%22en-US%22%3ERe%3A%20Excel%20VBA%20Hide%20rows%20that%20don't%20contain%20value%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1288966%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F611343%22%20target%3D%22_blank%22%3E%40TattyJJ%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EMay%20I%20ask%20%3CU%3E%3CSTRONG%3Ewhy%3C%2FSTRONG%3E%3C%2FU%3E%20you%20are%20writing%20a%20VBA%20routine%20to%20do%20something%20that%20a%20simple%20Data....Filter...%20will%20accomplish%3F%20That%20you're%20writing%20a%20VBA%20routine%20at%20all%20tells%20me%20that%20you're%20knowledgeable%20about%20Excel%2C%20so%20I'm%20going%20to%20guess%20that%20there%20is%20a%20reason%3B%20I'm%20just%20curious%20what%20it%20is.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1289032%22%20slang%3D%22en-US%22%3ERe%3A%20Excel%20VBA%20Hide%20rows%20that%20don't%20contain%20value%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1289032%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F425987%22%20target%3D%22_blank%22%3E%40mathetes%3C%2FA%3E%26nbsp%3B%3CBR%20%2F%3E%3CBR%20%2F%3EI%20thought%20about%20this%2C%20but%20it%20won't%20really%20work.%3CBR%20%2F%3E%3CBR%20%2F%3EThis%20bit%20of%20code%20is%20part%20of%20a%20much%20larger%20macro%20that%20automates%20a%20whole%20load%20of%20repetitive%20tasks.%26nbsp%3B%3CBR%20%2F%3E%3CBR%20%2F%3EThanks%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1289327%22%20slang%3D%22en-US%22%3ERe%3A%20Excel%20VBA%20Hide%20rows%20that%20don't%20contain%20value%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1289327%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F611343%22%20target%3D%22_blank%22%3E%40TattyJJ%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EWell%2C%20let%20me%20ask%20my%20question%20in%20a%20different%20way%2C%20then.%20Perhaps%20first%20a%20bit%20of%20background%20as%20to%20why%20I'm%20asking.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%20class%3D%22lia-indent-padding-left-30px%22%3EI%20became%20a%20skeptic%20on%20the%20subject%20of%20macros%20and%20VBA%20routines%20a%20number%20of%20years%20ago.%20I%20was%20the%20director%20of%20the%20HR%2FPayroll%20database%20for%20a%20major%20corporation%2C%20and%20one%20important%20but%20fairly%20minor%20recurring%20responsibility%20we%20had%20was%20to%20prepare%20a%20monthly%20report%20to%20top%20management%20on%20headcounts%2C%20by%20division%2C%20by%20employment%20category%20(P%2FT%2C%20F%2FT%2C%20temp%2C%20etc)%2C%20by%20location.....%20%26nbsp%3B%20The%20IT%20people%20had%20written%20a%20very%20elaborate%20VBA%20routine%20that%20manipulated%20the%20existing%20spreadsheet%20data%20into%20the%20format%20required%20for%20the%20report.%20I%20don't%20recall%20exactly%20how%20long%20it%20took%20to%20run%2C%20but%20for%20sake%20of%20illustration%20let's%20say%20it%20was%2045%20minutes%20(it%20was%20in%20that%20order%20of%20magnitude).%20I%20created%20a%20process%20that%20simply%20used%20Excel's%20built%20in%20functions%20for%20the%20summarization%20of%20database%20tables...%20mine%20worked%20virtually%20instantaneously.%20Granted%2C%20you%20had%20to%20get%20the%20data%20first%2C%20so%20add%20a%20few%20minutes%2C%20but%20it%20worked%2C%20quickly%20and%20reliably.%20I%20came%20away%20from%20that%20experience%20knowing%20that%2C%20yes%2C%20there%20are%20times%20when%20a%20macro%20or%20VBA%20routine%20is%20needed%3B%20but%20also%20convinced%20that%20Excel%20has%20built%20in%20many%20functions%20that%20reliably%20and%20efficiently%20do%20most%20of%20what%20we%20want%20to%20do%20in%20transforming%20raw%20data%20into%20a%20meaningful%20report.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ESo%20if%20you'll%20humor%20me--I%20recognize%20there%20may%20still%20be%20a%20fully%20valid%20reason%20for%20the%20use%20of%20VBA%3B%20I%20don't%20categorically%20reject%20it%3B%20I%20just%20question%20it--what%20is%20it%20that%20you're%20trying%20to%20accomplish%20in%20going%20from%20whatever%20raw%20data%20you%20have%20to%20whatever%20finished%20output%3F%20And%20are%20you%20sure%20that%20there's%20no%20capability%20already%20built-in%20that%20could%20do%20it%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EAnd%20please%2C%20feel%20free%20to%20ignore%20the%20question%20if%20you%20so%20desire.%20My%20intent%20certainly%20is%20not%20to%20irritate%20you%3B%20you're%20clearly%20very%20competent%20and%20may%20have%20completely%20valid%20reasons%2C%20whereas%20my%20experience%20may%20have%20unreasonably%20prejudiced%20me%20against%20VBA....so%20you%20can%20help%20me%20broaden%20my%20perspective.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1289961%22%20slang%3D%22en-US%22%3ERe%3A%20Excel%20VBA%20Hide%20rows%20that%20don't%20contain%20value%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1289961%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F425987%22%20target%3D%22_blank%22%3E%40mathetes%3C%2FA%3E%26nbsp%3B%3CBR%20%2F%3E%3CBR%20%2F%3EGranted%20the%20macro%20i%20have%20created%20(managed%20to%20solve%20the%20issue%20in%20the%20end)%20is%20a%20bit%20slow%2C%20but%20i%20believe%20this%20is%20a%20small%20price%20to%20pay%20for%20automating%20was%20was%20previously%20a%20very%20tedious%20and%20complex%20set%20of%20tasks.%3CBR%20%2F%3EIt%20is%20also%20worth%20noting%20this%20isn't%20for%20me%20to%20use%2C%20it%20is%20to%20allow%20people%20who%20have%20a%20very%20limited%20knowledge%20of%20excel%20to%20complete%20what%20would%20otherwise%20be%20a%20difficult%20set%20of%20tasks.%3CBR%20%2F%3E%3CBR%20%2F%3EEssentially%20we%20have%20a%20piece%20of%20software%20that%20outputs%20an%20excel%20file%20in%20order%20to%20produce%20a%20series%20of%20reports%20from.%3CBR%20%2F%3EWhat%20comes%20out%20is%20quite%20a%20mess%2C%20formatting%20all%20over%20the%20place%2C%20columns%20hidden%20that%20need%20to%20be%20visible%2C%20columns%20and%20rows%20that%20need%20to%20be%20deleted%20etc%20etc.%3CBR%20%2F%3EFirst%20job%20of%20the%20macro%20is%20to%20tidy%20that%20all%20up%20so%20it%20looks%20nice.%3CBR%20%2F%3E%3CBR%20%2F%3ESecondly%2C%20and%20this%20is%20where%20the%20code%20i%20was%20asking%20about%20comes%20in%2C%20it%20prompts%20for%20information%20in%20order%20to%20show%20the%20specific%20data%20that%20is%20needed%20for%20each%20report.%3CBR%20%2F%3E%3CBR%20%2F%3EWhile%20this%20could%20be%20done%20by%20formatting%20it%20as%20a%20table%20and%20filtering%2C%20there%20are%20literally%20thousands%20of%20them%2C%20to%20go%20through%20and%20pick%20out%20just%20the%20handful%20that%20are%20needed%2C%20then%20repeat%20a%20dozen%20times%20for%20the%20different%20sets%20of%20data%2C%20that%20is%20a%20real%20hassle.%3CBR%20%2F%3E%3CBR%20%2F%3EI%20hope%20this%20explains%20my%20reasoning%20for%20doing%20things%20the%20way%20i%20am.%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E
Highlighted
Occasional Contributor

Hi,

I have produced the below code that hides rows if they contain specific values which works fine, but now i want to do the opposite and hide all rows that DON'T contain the same values.

 

Rows.EntireRow.Hidden = False

    BeginRow = 2
    EndRow = Cells(Rows.Count, "A").End(xlUp).Row
    ChkCol = 1

    For RowCnt = BeginRow To EndRow
        If Cells(RowCnt, ChkCol).Value Like "ZZZZ*" Or Cells(RowCnt, ChkCol).Value Like "ENDO005" Or Cells(RowCnt, ChkCol).Value Like "ENDO007" Or Cells(RowCnt, ChkCol).Value Like "ENDO009" Or Cells(RowCnt, ChkCol).Value Like "ENDO011" Or Cells(RowCnt, ChkCol).Value Like "ORTO020" Or Cells(RowCnt, ChkCol).Interior.ColorIndex = 6 Then
            Cells(RowCnt, ChkCol).EntireRow.Hidden = True
            Else
            Cells(RowCnt, ChkCol).EntireRow.Hidden = False
        End If
    Next RowCnt

 

 

I was though it would be as simple as swapping the true and false over, but that doesn't seem to work and i'm not sure how to go about making it do what i want.

Anyone able to help?

Thanks

4 Replies
Highlighted

@TattyJJ 

 

May I ask why you are writing a VBA routine to do something that a simple Data....Filter... will accomplish? That you're writing a VBA routine at all tells me that you're knowledgeable about Excel, so I'm going to guess that there is a reason; I'm just curious what it is.

Highlighted

@mathetes 

I thought about this, but it won't really work.

This bit of code is part of a much larger macro that automates a whole load of repetitive tasks. 

Thanks

Highlighted

@TattyJJ 

 

Well, let me ask my question in a different way, then. Perhaps first a bit of background as to why I'm asking.

 

I became a skeptic on the subject of macros and VBA routines a number of years ago. I was the director of the HR/Payroll database for a major corporation, and one important but fairly minor recurring responsibility we had was to prepare a monthly report to top management on headcounts, by division, by employment category (P/T, F/T, temp, etc), by location.....   The IT people had written a very elaborate VBA routine that manipulated the existing spreadsheet data into the format required for the report. I don't recall exactly how long it took to run, but for sake of illustration let's say it was 45 minutes (it was in that order of magnitude). I created a process that simply used Excel's built in functions for the summarization of database tables... mine worked virtually instantaneously. Granted, you had to get the data first, so add a few minutes, but it worked, quickly and reliably. I came away from that experience knowing that, yes, there are times when a macro or VBA routine is needed; but also convinced that Excel has built in many functions that reliably and efficiently do most of what we want to do in transforming raw data into a meaningful report.

 

So if you'll humor me--I recognize there may still be a fully valid reason for the use of VBA; I don't categorically reject it; I just question it--what is it that you're trying to accomplish in going from whatever raw data you have to whatever finished output? And are you sure that there's no capability already built-in that could do it?

 

And please, feel free to ignore the question if you so desire. My intent certainly is not to irritate you; you're clearly very competent and may have completely valid reasons, whereas my experience may have unreasonably prejudiced me against VBA....so you can help me broaden my perspective.

 

Highlighted

@mathetes 

Granted the macro i have created (managed to solve the issue in the end) is a bit slow, but i believe this is a small price to pay for automating was was previously a very tedious and complex set of tasks.
It is also worth noting this isn't for me to use, it is to allow people who have a very limited knowledge of excel to complete what would otherwise be a difficult set of tasks.

Essentially we have a piece of software that outputs an excel file in order to produce a series of reports from.
What comes out is quite a mess, formatting all over the place, columns hidden that need to be visible, columns and rows that need to be deleted etc etc.
First job of the macro is to tidy that all up so it looks nice.

Secondly, and this is where the code i was asking about comes in, it prompts for information in order to show the specific data that is needed for each report.

While this could be done by formatting it as a table and filtering, there are literally thousands of them, to go through and pick out just the handful that are needed, then repeat a dozen times for the different sets of data, that is a real hassle.

I hope this explains my reasoning for doing things the way i am.