Consulting

Results 1 to 9 of 9

Thread: Solved: counting columns

  1. #1

    Solved: counting columns

    it's been a while since i've touched excel vba and i forgot how to do this:
    i have a spreadsheet that is based on an exported report and i am trying to format it. however, the fields exported are a differnet number each time, but always in the same format.
    so it's always like this:


    col A - row headings | ColB and C - plan 1 |
    and each additional plan takes another 2 columns.
    so i need to figure out how many plans there are.
    so i go down to the row that is always filled in in every cell and i did selection.end.
    so now here's my question:

    say i end on col E, i need to translate that into 5 so that i can subract 1 (for the col a headers) and divide by 2 to figure out that there are 2 plans.
    so how do i do this?

    thanks in advance,
    tkaplan

  2. #2
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    First select the range then run

    [VBA]
    Sub CountPlans()
    MsgBox (Application.WorksheetFunction.CountA(Selection) - Selection.Rows.Count)
    End Sub

    [/VBA]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  3. #3
    thank you.

  4. #4
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    or, without selecting
    [vba]
    Sub CountPlans2()
    MsgBox Application.WorksheetFunction.CountA(Rows("1:" & [A1].End(xlDown).Row)) _
    - [A1].End(xlDown).Row
    End Sub

    [/vba]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  5. #5
    these count the rows though. i need to count the columns.
    from a to e is 5 columns, from a to c is 3, etc.

  6. #6
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    CountA will count all non-blank cells in the selected rows, deduct the number of rows to discount the headings.
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  7. #7
    so if i select A1 through E1 and they all have data it will return 5?

  8. #8
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Here's an example. The formulae on the sheet are just for information.
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  9. #9
    got it.
    thank you.

Posting Permissions

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