+ Reply to Thread
Results 1 to 13 of 13

Search a range in a sheet

Hybrid View

  1. #1
    Registered User
    Join Date
    01-18-2009
    Location
    UK
    MS-Off Ver
    Excel 2003
    Posts
    21

    Search a range in a sheet

    Hi Folks,
    My problem is simple. I have a monthly time sheet. The times are entered in cells C8:F68.
    When some one has a annual leave day or a bank holiday they enter A/L or B/H.

    I have a summary sheet and have placed a cmd button on it to hopefully search the sheets that are months or the year.
    I know how to search the sheets and increment through them and not the 'Summary' sheet.

    What I need to establish is how to place in my code the actual search criteria and also limit the range to C8:F68
    On top of this I need to total each occurance of A/L and B/H and display them in two cells.

    I have tried to do a search of the forum but with no luck.
    Like I say I have managed to understand how not to select the 'Summary' sheet when incrementing through the sheets but its just the search for the A/L and adding them up!

    Cheers for any help.

  2. #2
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Search a range in a sheet

    A/L Formula:
    =COUNTIF(C8:F68,"A/L")

    B/H Formula:
    =COUNTIF(C8:F68,"B/H")

    In VBA:
    Sub Macro()
    Dim AL As Long, BH As Long, Rng As Range
    Set Rng = Range("C8:F68")
    
        AL = WorksheetFunction.CountIf(Rng, "A/L")
        BH = WorksheetFunction.CountIf(Rng, "B/H")
    
    MsgBox "A/L = " & AL & " and B/H = " & BH
    
        Range("A1").Value = AL
        Range("A2").Value = BH
    
    Set Rng = Nothing
    End Sub
    Last edited by JBeaucaire; 05-12-2009 at 03:42 AM.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  3. #3
    Registered User
    Join Date
    01-18-2009
    Location
    UK
    MS-Off Ver
    Excel 2003
    Posts
    21

    Re: Search a range in a sheet

    Tip top !!!
    Fantastic, thank you.

    Do you know how I could check if a number exists in the same range rather than the text like A/L etc. What I want to do is always make sure that the cell background is white if there is a numeric value in it.

    You have guessed I use VB to colour the cell if there is a A/L B/H etc but this does not release if you over type the A/L with a number (time actually).

    Cheers again, it worked like a charm.

  4. #4
    Registered User
    Join Date
    01-18-2009
    Location
    UK
    MS-Off Ver
    Excel 2003
    Posts
    21

    Re: Search a range in a sheet

    My code so far is thus but I am having trouble getting the code to loop through each sheet but not the 'Summary' sheet.
    I thought I was on the right track but it would seem not.

    What I dont want to do is reset the Rng counter as it skips to the next sheet.

    Any help would be appreciated.

    Cheers

    Sub Search_NotClear()
    
    Dim AL As Long, CL As Long, Rng As Range
    Dim LT As Long, BH As Long, SL As Long
    Dim US As Long, ML As Long, UL As Long, a As Long
    Dim ws As Worksheet
    Set Rng = Range("C8:F68")
    
    For Each ws In Worksheets
            If ws.Index < Sheets("Summary").Index Then
    
        AL = WorksheetFunction.CountIf(Rng, "A/L")
        CL = WorksheetFunction.CountIf(Rng, "C/L")
        LT = WorksheetFunction.CountIf(Rng, "L/T")
        BH = WorksheetFunction.CountIf(Rng, "B/H")
        SL = WorksheetFunction.CountIf(Rng, "S/L")
        US = WorksheetFunction.CountIf(Rng, "U/S")
        ML = WorksheetFunction.CountIf(Rng, "M/L")
        UL = WorksheetFunction.CountIf(Rng, "U/L")
        a = WorksheetFunction.CountIf(Rng, "A")
    
    
    
    'MsgBox "A/L = " & AL & " and C/L = " & CL
    'MsgBox "B/H = " & BH
    
    Next ws
    
    
        Range("A1").Value = AL
        Range("A2").Value = CL
        Range("A3").Value = LT
        Range("A4").Value = BH
        Range("A5").Value = SL
        Range("A6").Value = US
        Range("A7").Value = ML
        Range("A8").Value = UL
        Range("A9").Value = a
    
    End If
    Set Rng = Nothing
    End Sub

  5. #5
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Search a range in a sheet

    Just to be clear, you want to search EVERY sheet except SUMMARY for those text values, then insert the TOTAL counts on the Summary in column A?

  6. #6
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Search a range in a sheet

    I think this will do what you're after. By verifying the Names of sheets instead of the position, it won't matter how you organize the workbook in the future.
    Option Explicit
    
    Sub Search_NotClear()
    
    Dim AL As Long, CL As Long, LT As Long, BH As Long, SL As Long
    Dim US As Long, ML As Long, UL As Long, a As Long
    Dim ws As Worksheet, Rng As Range
    Sheets("Summary").Activate
    Set Rng = Range("C8:F68")
    
    'Collect values
        For Each ws In Worksheets
            ws.Activate
            If ws.Name <> "Summary" Then
                AL = AL + WorksheetFunction.CountIf(Rng, "A/L")
                CL = CL + WorksheetFunction.CountIf(Rng, "C/L")
                LT = LT + WorksheetFunction.CountIf(Rng, "L/T")
                BH = BH + WorksheetFunction.CountIf(Rng, "B/H")
                SL = SL + WorksheetFunction.CountIf(Rng, "S/L")
                US = US + WorksheetFunction.CountIf(Rng, "U/S")
                ML = ML + WorksheetFunction.CountIf(Rng, "M/L")
                UL = UL + WorksheetFunction.CountIf(Rng, "U/L")
                a = a + WorksheetFunction.CountIf(Rng, "A")
            End If
        Next ws
    
    'Insert values into Summary
    Sheets("Summary").Activate
        Range("A1").Value = AL
        Range("A2").Value = CL
        Range("A3").Value = LT
        Range("A4").Value = BH
        Range("A5").Value = SL
        Range("A6").Value = US
        Range("A7").Value = ML
        Range("A8").Value = UL
        Range("A9").Value = a
    
    Set Rng = Nothing
    End Sub

  7. #7
    Registered User
    Join Date
    01-18-2009
    Location
    UK
    MS-Off Ver
    Excel 2003
    Posts
    21

    Re: Search a range in a sheet

    This code is not working correctly and I am stuck.
    I dont think this is a massive issue but my Cmd button is on the summary sheet and not the monthy sheets.

    It only seems to count data if it is in the cell range on the 'Sumary' sheet.
    Even then it is counting and adding double the amount ie 2 A/L days it will display as 4 and so on.

    Trying hard this end but getting stuck again!!

    Cheers again

  8. #8
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Search a range in a sheet

    Shouldn't be hard at all. The code I've given is ready to use, simply insert it into your command button. It doesn't matter where the button is, the code will go to the sheets and get the data needed.

    When inserting my macro into your command button, only insert the lines from
    Dim...
    .
    .
    .
    Set Rng = Nothing
    This can made really simple if you just click GO ADVANCED and use the paperclip icon to upload your sheet. Sometimes people forget to indicate something MAJOR about their sheet that inhibits simple macros like this one.

  9. #9
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Search a range in a sheet

    Try this:
    Option Explicit
    
    Sub Search_NotClear()
        Const sRng  As String = "C8:F68"
    
        Dim wks     As Worksheet
        Dim v       As Variant
        Dim i       As Long
        Dim ai(1 To 9) As Long
    
        'Collect values
        For Each wks In Worksheets
            If wks.Name <> "Summary" Then
                i = 0
                For Each v In Array("A/L", "C/L", "L/T", "B/H", "S/L", "U/S", "M/L", "U/L", "a")
                    i = i + 1
                    ai(i) = ai(i) + WorksheetFunction.CountIf(wks.Range(sRng), v)
                Next v
            End If
        Next wks
    
        Worksheets("Summary").Range("A1:A9").Value = WorksheetFunction.Transpose(ai)
    End Sub
    Last edited by shg; 05-12-2009 at 07:35 PM. Reason: fixed a bug :eek:
    Entia non sunt multiplicanda sine necessitate

  10. #10
    Registered User
    Join Date
    01-18-2009
    Location
    UK
    MS-Off Ver
    Excel 2003
    Posts
    21

    Re: Search a range in a sheet

    Have tried is exacty and still doing as I said above!

    But why is it counting values from the 'Summary' sheet. That really gets me as we have told it not to!! Most odd I have to say.

    JBeaucaire, thanks for your valuble help so far.
    Cheers ever so.

  11. #11
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Search a range in a sheet

    Still no sheet posted?

    Look at the macro in the section "Collect Values"...you see we I activate each sheet and do the COUNT on that sheet. Every sheet EXCEPT Summary. So, until you post your sheet, I can't guess why the basics aren't working for you. I'm sure it's something simple.

  12. #12
    Registered User
    Join Date
    01-18-2009
    Location
    UK
    MS-Off Ver
    Excel 2003
    Posts
    21

    Re: Search a range in a sheet

    Sheet attached.
    Interestingly I changed the code from:
    If ws.Name <> "Summary" Then
    To
    If ws.Name = "Try" Then
    and it was still counting the values on the 'Summary' sheet where my Cmd button is placed. BUT it was counting correctly rather than doubling up ??

    Cheers
    Attached Files Attached Files

  13. #13
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Search a range in a sheet

    Well, it DOES behave differently than it did on my test sheet. I imagine this is a good a reason as any to start with the worksheet itself, huh?

    Borrowing SHG's "string" approach, it behaves as expected.
    Sub Search_NotClear()
    Dim AL As Long, CL As Long, LT As Long, BH As Long, SL As Long
    Dim US As Long, ML As Long, UL As Long, a As Long
    Dim ws As Worksheet, Rng As String
    Rng = "C8:F68"
    
    'Collect values
        For Each ws In Worksheets
            ws.Activate
            If ws.Name <> "Summary" Then
                AL = AL + WorksheetFunction.CountIf(Range(Rng), "A/L")
                CL = CL + WorksheetFunction.CountIf(Range(Rng), "C/L")
                LT = LT + WorksheetFunction.CountIf(Range(Rng), "L/T")
                BH = BH + WorksheetFunction.CountIf(Range(Rng), "B/H")
                SL = SL + WorksheetFunction.CountIf(Range(Rng), "S/L")
                US = US + WorksheetFunction.CountIf(Range(Rng), "U/S")
                ML = ML + WorksheetFunction.CountIf(Range(Rng), "M/L")
                UL = UL + WorksheetFunction.CountIf(Range(Rng), "U/L")
                a = a + WorksheetFunction.CountIf(Range(Rng), "A")
            End If
        Next ws
    
    'Insert values into Summary
    Sheets("Summary").Activate
        Range("A1").Value = AL
        Range("A2").Value = CL
        Range("A3").Value = LT
        Range("A4").Value = BH
        Range("A5").Value = SL
        Range("A6").Value = US
        Range("A7").Value = ML
        Range("A8").Value = UL
        Range("A9").Value = a
    
    End Sub
    Attached Files Attached Files

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1