Consulting

Results 1 to 7 of 7

Thread: Hide Columns based on a value in a range

  1. #1
    VBAX Regular
    Joined
    Mar 2012
    Posts
    34
    Location

    Unhappy Hide Columns based on a value in a range

    Hi There,

    I have a macro that copies certain cells to another sheet based on any cell in a specific range matching a value of 3.

    What I would like to do is expand that macro or call a seperate macro at the end of my code so that when the data is copied across into a sheet called "Test County", if any column D:H in the range D4:H200 does not equal the value of 3, then hide that column.

    I have searched and searched and seen many similar(ish) examples but cannot find anything that I can modify to fit my requirements.

    In Anticipation

    Craigos

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [vba]

    With Worksheets("Test County")

    For Each col In .Range("D4:H200").Columns

    col.EntireColumn.Hidden = Application.CountIf(col, "<>3") > 0
    Next col
    End With
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    VBAX Regular
    Joined
    Mar 2012
    Posts
    34
    Location
    Hi,

    Thanks for the reply, but all it is doing is hiding ALL columns D:H even when a 3 is showing in the cells within the range D4:H200.

    For example Col D has a 3 in range....Col E has a 3....Cols E & F have no 3 in the range...Col H has a 3....execute Macro and all cols Hide not what it should be doing as in Hiding E & F and showing D, E and H.

    Any ideas greatly welcomed

    Craigos

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    My testing didn't so maybe it would be best to post your workbook.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  5. #5
    VBAX Regular
    Joined
    Mar 2012
    Posts
    34
    Location
    Hi xld,

    Thanks for your patience:

    Workbook attached....

    DEPT Mapping Test - Attachment.xls

    Thank You

    Craigos

  6. #6
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    Hi There,

    I opened the wb. The range D4:H200 has no values at all. After unhiding the gridlines, I filled D4:H200 with all 3's. I didn't test your userform, but simply Bob's code, which of course ran thru the columns w/o hiding any. If I changed a cell to another value, then that column would be hidden when the colde is run.

    Are you sure you want all 3's in a column, from top to bottom?

    Mark

  7. #7
    VBAX Regular
    Joined
    Mar 2012
    Posts
    34
    Location
    Hi GTO,

    I dont want all 3's in a column as such as it will depend upon what is copied from the 'All Lists' sheet at columns T:X.

    On 'All Sheets' in Col B - Local Authority - the selection is 'Branch' and there are 4 Wards, looking at Cols T:X on that sheet, the macro in the userform copies any data that has a 3 in the Cells T:X.

    In the case of 'Branch' it copies 3 Wards as:

    Ward1 (Copy) has a 3 in cell T4.
    Ward2 has a 3 in the cell U5
    Ward4 has a 3 in cell X7

    The macro copies the Row across, so that when the data is presented on the 'Test County' sheet in Rows 4:7........ at D4:H7 (for this example) the result and my expectation:

    D3 has a 3 in it, keep Col showing.
    E5 has 3 in it, keep Col showing.
    H3 has a 3 in it, keep Col showing.

    As there are no 3's at all in Cols F and G , Hide those Cols.

    The show / hide will be any combination of D:H depending if a 3 is in the Col range - the range changes dependant upon how many Wards are in a Local Authority that have a Priority clasification of 3 on 'All Lists' Cols T:X.

    I hope that clarifies and makes sense

    Thanks

    Craig

Posting Permissions

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