Consulting

Results 1 to 10 of 10

Thread: Count of cells using VBA

  1. #1

    Count of cells using VBA

    Dear friends,

    I did a search on this topic within the forum but was not able to find anything that addressed my specific query.

    Background</SPAN> - I have a spreadsheet with 60 tabs, multiple cells with formulas, multiple cells that act as inputs (values, text).

    Question</SPAN> - I need to figure the following for the entire workbook:
    1. Count of cells being used within the entire workbook
    2. Count of cells that have formulas in them
    3. Count of Cells that act as inputs (values/text) and have formulas dependent on them
    4. Count of Cells that act as inputs (values/text) and but have NO formulas dependent on them

    Can someone suggest some kind of a macro to address this.

    Any help is much appreciated.

    Cheers, gk</SPAN>

  2. #2
    VBAX Contributor GarysStudent's Avatar
    Joined
    Aug 2012
    Location
    Lakehurst, NJ, USA
    Posts
    127
    Location
    Here are the first two parts:
    For USED:
    [VBA]Sub UseCounter()
    Dim s As Worksheet, r As Range
    Dim wf As WorksheetFunction
    Set wf = Application.WorksheetFunction
    Dim Kount As Long
    Kount = 0
    For Each s In Sheets
    s.Select
    Kount = Kount + wf.CountA(ActiveSheet.UsedRange)
    Next s
    MsgBox Kount
    End Sub
    [/VBA]
    For FORMULAS:
    [VBA]Sub FunctionCounter()
    Dim s As Worksheet, r As Range
    Dim wf As WorksheetFunction
    Set wf = Application.WorksheetFunction
    Dim Kount As Long
    Kount = 0
    For Each s In Sheets
    s.Select
    For Each r In ActiveSheet.UsedRange
    If r.HasFormula Then
    Kount = Kount + 1
    End If
    Next r
    Next s
    MsgBox Kount
    End Sub
    [/VBA]
    Have a Great Day!

  3. #3
    Hi GarysStudent, thank you so much for taking the time out to look at my request. I will just test this out and post the results.

    Any luck with bullet # 3 and 4?

    Best, gk

  4. #4
    VBAX Contributor GarysStudent's Avatar
    Joined
    Aug 2012
    Location
    Lakehurst, NJ, USA
    Posts
    127
    Location
    Hi gk:

    I don't know how to approach items 3 & 4
    Have a Great Day!

  5. #5
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    i dont know either.

    but post 18 here may give you an idea:
    http://www.xtremevbtalk.com/showthread.php?p=574302


    ps: membership is required to download attachments.
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  6. #6
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,876
    Quote Originally Posted by GarysStudent
    I don't know how to approach items 3 & 4
    GarysStudent, It's past my bedtime now but you may be able to dream up something along the lines of your HasFormula solution using range.dependents which returns a range and if that range is nothing then that cell has no dependents.
    There are also DirectPrecedents, DirectDependents, Dependents and Precedents properties to check out.
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  7. #7
    VBAX Contributor GarysStudent's Avatar
    Joined
    Aug 2012
    Location
    Lakehurst, NJ, USA
    Posts
    127
    Location
    Thanks!
    Have a Great Day!

  8. #8
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,646
    [vba]
    Sub M_snb()
    for each sh in sheets
    c00=c00 +sh.usedrange.cells.count
    c01=c01+sh.usedrange.specialcells(-4123)

    for each it in sh.usedrange.specialcells(-4123)
    for each it2 in it.precedents
    if instr(c03 & "|","|" & it2.address & "|")=0 then c03= c03 & "|" & it2.address
    next
    next
    next
    msgbox "number of usedcells " & c00 &vblf & "number of formulacells " & c01 & vblf & "number of prcedents " & ubound(split(c03,"|"))

    End Sub
    [/vba]

  9. #9
    Hi snb,

    Thanks for your response. The code does work but not all files. On some files I get error with the following statement
    c01=c01+sh.usedrange.specialcells(-4123)

    Based on my reserach, it seems like usedrange can lead to some errors at times.

    Any help will be greatly appreciated.

  10. #10
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,646
    No it has nothing to do with usedrange. Dive into the VBEditor's helpfiles; lemma specialcells.

Posting Permissions

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