Excel/vba

Advertisement


Question
QUESTION: Hi Tom

i went ahead and sent you just the worksheet and comments of what we wanted to do, by vba or formula....

Tony

ANSWER: Anthony,

In the code module of the worksheet where you want this behavior, use this code:


Private Sub Worksheet_Change(ByVal Target As Range)
Dim sName As String, sCounty As String, cntName As Long, cntCounty As Long
Dim cnt As Long, r As Range, bHide As Boolean
If Target.Address = "$E$2" Or Target.Address = "$C$2" Then
 sName = Me.Range("C2").Value
 sCounty = Me.Range("E2").Value
 If Len(Trim(sName)) > 0 Then cntName = 1 Else cntName = 0
 If Len(Trim(sCounty)) > 0 Then cntCounty = 2 Else cntCounty = 0
 cnt = cntName + cntCounty
 If cnt > 0 Then
   Me.UsedRange.EntireRow.Hidden = False
   lastrow = Me.Cells(Rows.Count, "C").End(xlUp).Row
   Set r = Range("C5:C" & lastrow)

   For Each cell In r
   bHide = False
   Select Case cnt
     Case 1
       If InStr(1, cell.Value, sName, vbTextCompare) = 0 Then bHide = True

     Case 2
       If InStr(1, cell.Offset(0, 6).Value, sCounty, vbTextCompare) = 0 Then bHide = True

     Case 3
       If InStr(1, cell.Value, sName, vbTextCompare) = 0 Or _
         InStr(1, cell.Offset(0, 6).Value, sCounty, vbTextCompare) = 0 Then bHide = True
   End Select
   If bHide Then cell.EntireRow.Hidden = True
   Next
 Else
   Me.UsedRange.EntireRow.Hidden = False
 End If
End If
End Sub


--
Regards,
Tom Ogilvy


---------- FOLLOW-UP ----------

QUESTION: GOT IT ....WORKS GREAT, BUT WHEN I DO THE SELECTION THE NUMBER IN A COLUMN SHOWS THERE RESPECTIVE NUMBER THEY CHANGE TOO - THERE A WAY TO NOT HAVE COLUMN A EFFECTED BY THIS? SO THAT IF ONLY TWO CLIENTS SHOW UP THEN IT WILL SAY 1 AND 2?

TONY

Answer
Tony,

You can use a formula like found in column A of the workbook to produce the count.

A5:  =if(len(trim(C5))>0,Subtotal(103,$C$5:$C5),"")

then drag fill down column A for as far as you will ever have data.


--

Regards,

Tom Ogilvy

About Excel
This topic answers questions related to Microsoft Excel spreadsheet (or workbook) stand-alone or Mircrosoft Office Excel including Excel 2003, Excel 2007, Office 2000, and Office XP. You can get Excel help on Excel formulas(or functions), Excell macros, charting in Excel, advanced features, and the general use of Excel. This does not provide a general Excel tutorial nor the basics of using a spreadsheet. It provides specific answers to using Microsoft Excel only. If you do not see your Excel question answered in this area then please ask an Excel question here

Excel

All Answers


Answers by Expert:


Ask Experts

Volunteer


Tom Ogilvy

Expertise

Selected as an Excel MVP by Microsoft since 1999. Answering Excel questions in Allexperts since its inception in 2001. Able to answer questions on almost all aspects of Excel's internal capabilities. If seeking a VBA solution, please specify that in your question itself so I give you the answer you want. [Excel has weak protection - if you are distributing an application, I don't answer questions on how to protect your project from your users.]

Experience

Extensive experience.

Education/Credentials
Master of Science (MS) degree Operations Research (ORSA)

Awards and Honors
Microsoft MVP in Excel.

©2016 About.com. All rights reserved.