Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

Result except zero value

0

Sir, I have data like below in sheet 1 

work no.     Mouse.   Keyboard    bag              

2511.              5.               4.              0

2512.              3.               0.              1           

Now I need like below, row wise in sheet 2 excluding zero value (it should not show keyboard) of said above material when I will entry 2512 

Answer should be like below

Work no= 2512 (press enter)

1.      Mouse.     3

2.      Bag.           1

Thank u

Answer
Discuss

Answers

0
Selected Answer

Naga

Three things are needed to achieve what you want (from the file you posted elsewhere)...

In the Result sheet of the attached revised file (with search term in cell C1), the VLOOKUP formula to return the Keyboard value to cell  C4 is:

=VLOOKUP($C$1,Data!$B$5:$G$9,2,FALSE)
where the range column number 2 in bold needs to change to get the other results (in C5:C9) and the FALSE means an exact value will be returned (or an error if the value is not found).

That could return some rows with a zero entry (which can be filtered out) but you want a "1, 2, 3, 4" count of only the rows with a non-zero value. That can be done in conjunction with the filter. The formula in A4 is:

=COUNTIF($C$4:C4,">0")
and only the part in bold changes as that formula is copied to A5:A9. The count increases 1,2, 3... unless a row containing zero is encountered. 

The final bit is to apply a filter (on column C) to hide the non-zero rows. The only problem with that is that the filter does not auto-refresh when the value in C1 is changed. To get around that, there's a small event macro behind the Results sheet. The code is as follows (where the words in red are just comments to guide you, in case you're unfamiliar with VBA):

Private Sub Worksheet_Change(ByVal Target As Range)
'Do nothing if the change was not to the W.O. Number cell...
If Intersect(Target, Range("C1")) Is Nothing Then Exit Sub
'Otherwise refresh the Autofilter...
Range("A3:C8").AutoFilter Field:=3, Criteria1:=">0"
End Sub
When something is changed in that sheet, VBA sees it as a Workseet_Change event to cells(s) is identies as the Target (if you entered soemthing in C3, it would see C3 as Target). The line beginning If Intersect... sees if there's an "overlap" between C1 and the cell(s) entered- if not (the intersect is nothing) then nothing happens. Otherwise the line beginning Range("A3:C8")...  applied a filter to the 3rd column of A3:C8 to show only rows where that column is >0.

The nett result is that Results sheet will display only rows  with a number under Qty each time you enter something in C1. (The other rows are just hidden)

Hope this helps

Discuss

Discussion

Thanks sir, it will help me in other situation. But I need different result. I will ask question with attaching file in other question. Please send me the answer.
Naga (rep: 10) Aug 29, '21 at 11:28 am
Naga

Please see revised Answer (and, if it works for you, mark it as Selected for the guidance of others)
John_Ru (rep: 6142) Aug 30, '21 at 10:11 am
Thanks a lot john_Ru.

It works great.
Thank u very much.
Naga (rep: 10) Aug 30, '21 at 12:06 pm
Thanks for selecting my answer Naga.

I suggest you also consider changing your data range into a named table and altering the formulae/ macro to suit. That way as your data expands, you won't need to alter the formulae/macros again. Look in the Tutorials section of this site and look for Tables and Names. 
John_Ru (rep: 6142) Aug 30, '21 at 3:39 pm
Thanks for selecting my answer Naga. Don't forget to change the range in the VLOOKUP formulae as your data expands (or convert to named ranges/ tables- search the Tutorials section for those)
John_Ru (rep: 6142) Aug 30, '21 at 3:58 pm
Yes sir, Definately.
Thank u.
Naga (rep: 10) Sep 2, '21 at 10:49 am
Add to Discussion


Answer the Question

You must create an account to use the forum. Create an Account or Login