Share via

Defining a range of cells in a VBA hide/unhide column function

Anonymous
2011-01-22T17:05:09+00:00

Hi,

I am a beginner with VBA, so if I am asking the obvious, I apologize.

This is a script I adapted and wrote. It a value of "--" appears in a given cell of row 4, it will hide a column. If something else is inputted (from a dropdown list) a column will appear dynamically and show the result of a calculation. For example, in column e, row 4, I have a value of 100$. If the drop down list default is "--" in column f, column h will not appear. However if I choose Mint from column f, it will do a calculation using a formula in column g and show the result in column H. Column G always remains hidden.

The code is

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

    If Range("F4").Value = "--" Then

        Columns("H").EntireColumn.Hidden = True

        Else

            Columns("H").EntireColumn.Hidden = False

    End If

    If Range("i4").Value = "--" Then

        Columns("k").EntireColumn.Hidden = True

        Else

            Columns("k").EntireColumn.Hidden = False

    End If

    If Range("L4").Value = "--" Then

        Columns("N").EntireColumn.Hidden = True

        Else

            Columns("N").EntireColumn.Hidden = False

    End If

    If Range("O4").Value = "--" Then

        Columns("Q").EntireColumn.Hidden = True

        Else

            Columns("Q").EntireColumn.Hidden = False

    End If

    If Range("R4").Value = "--" Then

        Columns("T").EntireColumn.Hidden = True

        Else

            Columns("T").EntireColumn.Hidden = False

    End If

End Sub

What I am trying to do now is to make this function work on all cells in any given column. It works now on Col E row 4, but not 5. I know it is a range issue, but everything I have been trying results in an error. Thanks for you patience.

Microsoft 365 and Office | Excel | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

  1. Anonymous
    2011-01-24T21:30:00+00:00

    This include the previous suggestions:

    Private Sub Worksheet_Change(ByVal Target As Range)

    If Target.Count > 1 Then Exit Sub

    Set isect = Intersect(Target, Range("F4:F100,I4:I100,L4:L100,Q4:Q100,R4:R100"))

    If Not isect Is Nothing Then

        If Target.Value = "--" Then

            Target.Offset(0, 2).EntireColumn.Hidden = True

        Else

            Target.Offset(0, 2).EntireColumn.Hidden = False

        End If

    End If

    End Sub

    Regarding the formula, I would use something like:

    =If(F4="",Estimated Price, Average formula)

    Was this answer helpful?

    0 comments No comments

4 additional answers

Sort by: Most helpful
  1. Anonymous
    2011-01-25T00:23:59+00:00

    Per,

    Thank you, works like a charm. Thanks again also to Don and JL, you guys are a credit to this online community.

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2011-01-24T18:45:55+00:00

    JL and Don,

    Mucho thanks for your suggestions.....I'll clarify what I am looking for, because I can't the solutions you suggest work.....I am getting compile or Run 13 errors. Here goes

    I have 1 column "E", starting at row 4, where a market value is entered ie. 155$. Now, in column "F" I have a drop down list where the default shown value is "--". See first image.  

    If the user selects one of the other values, say  "Mint", it will do a VLOOKUP on hidden column "G" and in column "H" will post the price of "E" multiplied by the % as determined in "G".....(155*10%)+ 155. The items priced in column E have various conditions in which they can be found, and the IF statement was way too complex to write which is why I chose this approach.

    But the script only works for the original row shown. If the user inputs a value in the next row and selects a Manual CDN other than "--", no column appears, neither are calculations done.

    One last question if I may. I am using the AVERAGE function in the Averaged price and I have been trying to use an IF condition basically stating that IF Media CDN = "Missing", then avg price = Estimated value of Media. ELSE average all the values.

    Approx. Market Value Media CDN Manual CDN Casing CDN Slip/Cover CDN Box CDN Averaged Price
    $155.00 -- -- -- -- -- $155.00
    Approx. Market Value Media CDN Estimated Value Manual CDN Casing CDN Slip/Cover CDN Box CDN Averaged Price
    $155.00 Mint $170.50 -- -- -- -- $162.75

    Thank you very much for considering my question which may seem simple to you but rocket science to me :)

    I appreciate the efforts, being a former MVP myself in another expertise.

    Roch

    Was this answer helpful?

    0 comments No comments
  3. Anonymous
    2011-01-23T13:40:48+00:00

    In addition to what Don Guillett provided, I think what you're looking for is to make the row number in your Range() statements variable.  You can do that with a construct like this:

    If Range("F" & Target.Row)="--" Then

    You don't need the .Value in this case because .Value is the default property, and believe it or not, when you want a default property to be used, it's actually faster than specifying that same property.  We're only talking microseconds, but they can add up.  But if you're worried about clarity of understanding for the code, then using the .Value makes it pretty darned clear what you're examining.

    Also, I'm not sure from your question but what you may be wanting to limit your code to run only when you select/change a cell in one specific column, E.  If so, you can add this to the test at the beginning of the code (incorporates Don's suggestion also):

    If Target.Count > 1 Or Target.Column <> 5 Then

      Exit Sub

    End If

    That says that if more than one cell is selected OR if the selected cell is not in column E, then exit the sub without further action.

    Finally, it may be that you need to make your tests after the user makes a data entry or choice from a list, and in that case you may want to use the Worksheet_Change() instead of Worksheet_SelectionChange() event to get the job done.


    I am free because I know that I alone am morally responsible for everything I do. R.A. Heinlein

    Was this answer helpful?

    0 comments No comments
  4. Anonymous
    2011-01-22T17:48:32+00:00

    something like this

    if target.count>0 then exit sub

    if not intersect(range("a2:a22"),target))is nothing then

    do your thing

    end if


    Don Guillett MVP Excel SalesAid Software *** Email address is removed for privacy ***

    Was this answer helpful?

    0 comments No comments