Macro not working 100%

phillipcook

Board Regular
Joined
Jun 25, 2015
Messages
87
Hi guys

I have the following macro:
Code:
Private Sub Worksheet_Change(ByVal Target As Range)


Dim rng As Range


If Target.Cells.CountLarge > 1 Then Exit Sub
Set rng = Intersect(Target, Union(Range("E4"), Range("A10:A19")))
If rng Is Nothing Then Exit Sub


Select Case rng.Address(0, 0)
    Case "E4"
        Columns("J").EntireColumn.Hidden = Not CBool(Target.Value = "BMA - Hay Point Coal Terminal")
    Case Else
        Columns("K").EntireColumn.Hidden = Not CBool(Target.Value = "Surface Water - Monthly")
        Columns("L").EntireColumn.Hidden = Not CBool(Target.Value = "Surface Water - Investigation")
        Columns("M").EntireColumn.Hidden = Not CBool(Target.Value = "Potable Water - Investigation")
        Columns("N").EntireColumn.Hidden = Not CBool(Target.Value = "Ground Water - Unscheduled No Purge")
        Columns("O").EntireColumn.Hidden = Not CBool(Target.Value = "Ground Water - Unscheduled Purge")
        Columns("P").EntireColumn.Hidden = Not CBool(Target.Value = "Discharge Water - Daily" Or Target.Value = "Discharge Water - Weekly" Or Target.Value = "Discharge Water - Investigation")
        Columns("Q").EntireColumn.Hidden = Not CBool(Target.Value = "Quarterly Contractor Meeting")
        Columns("R").EntireColumn.Hidden = Not CBool(Target.Value = "Ground Water - Weekly" Or Target.Value = "Ground Water - Quarterly" Or Target.Value = "Ground Water - 6 Monthly")
        Columns("S").EntireColumn.Hidden = Not CBool(Target.Value = "Ground Water - Weekly" Or Target.Value = "Ground Water - Quarterly" Or Target.Value = "Ground Water - 6 Monthly")
End Select


End Sub

The Macro is close to working. The cells are remaining hidden if the data does not match, however only one column at any time will un-hide (the un-hidden column relates to the last data entered)

on occasions, if the criteria is met, multiple columns should be displayed, but this isn't occurring.

Can anyone please help me figure out why this macro is only allowing one column to ever un-hide?

Cheers in advance.
 
Last edited by a moderator:

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Can you give an example of when multiple columns should unhide?
 
Upvote 0
As an example:
If the following was true
A10 has "Surface Water - Investigation"
A11 has "Ground Water - Weekly"
A12 has "Quarterly contractor Meeting"

This should mean the following columns should un-hide:
L
R and S (as the criteria is met for both)
Q

currently if I had this data entered (with A12 "Quarterly contractor Meeting" being the last information entered) the only cell that would be un-hidden would be Q
 
Upvote 0
Your code won't work then because each subsequent entry will hide the previously unhidden ones. I suspect what you actually need to do is either only unhide columns based on the target value, not hide them, or loop through a range of cells to determine all the columns that should be visible. Hard to say which is appropriate without knowing the workflow/circumstances.
 
Upvote 0
So the document that I am working on is a time record sheet.

On set tasks, we need to collect background information in relation to the number of samples taken.

the record sheet is as follows:

[TABLE="width: 1169"]
<colgroup><col><col><col><col><col span="4"></colgroup><tbody>[TR]
[TD]Scheduled Work[/TD]
[TD]Position[/TD]
[TD]Location[/TD]
[TD]Purchase Order[/TD]
[TD]Start
Time
[/TD]
[TD]Finish
Time
[/TD]
[TD]Vehicle Allowance[/TD]
[TD]Hours[/TD]
[/TR]
[TR]
[TD]Potable Water - Investigation[/TD]
[TD]Scientific Officer[/TD]
[TD]Potable Water Sampling[/TD]
[TD][/TD]
[TD]11:20[/TD]
[TD]13:00[/TD]
[TD] [/TD]
[TD]1.67[/TD]
[/TR]
[TR]
[TD]Surface Water - Investigation[/TD]
[TD]Scientific Officer[/TD]
[TD]Surface Water Sampling[/TD]
[TD] [/TD]
[TD]13:00[/TD]
[TD]14:00[/TD]
[TD] [/TD]
[TD]1.00[/TD]
[/TR]
[TR]
[TD]Ground Water - Unscheduled No Purge[/TD]
[TD]Scientific Officer[/TD]
[TD]Ground Water Sampling[/TD]
[TD] [/TD]
[TD]14:00[/TD]
[TD]15:30[/TD]
[TD] [/TD]
[TD]1.50[/TD]
[/TR]
[TR]
[TD]Quarterly Contractor Meeting[/TD]
[TD]Report Writer[/TD]
[TD]NATA Laboratory[/TD]
[TD] [/TD]
[TD]15:30[/TD]
[TD]18:00[/TD]
[TD]1.0[/TD]
[TD]2.50[/TD]
[/TR]
[TR]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[/TR]
[TR]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[/TR]
[TR]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[/TR]
[TR]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[/TR]
[TR]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[/TR]
[TR]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[/TR]
</tbody>[/TABLE]
The background information sheet looks like this:
[TABLE="width: 1593"]
<colgroup><col span="7"><col span="3"></colgroup><tbody>[TR]
[TD]HPCT Purchase Order Number[/TD]
[TD]DBCT Surface Water Sites <0.5m Below Spillway Sampled[/TD]
[TD]DBCT Unscheduled Surface Water Sites Sampled[/TD]
[TD]DBCT Unscheduled Potable Water Sites Sampled[/TD]
[TD]DBCT Unscheduled Unpurged Ground Water Sites Attended[/TD]
[TD]DBCT Unscheduled Purged Ground Water Sites Attended[/TD]
[TD]DBCT Discharge Water Sites Sampled[/TD]
[TD]DBCT Contractor meeting, Total Gauge Representatives[/TD]
[TD]DBCT Scheduled Unpurged Ground Water Sites Attended[/TD]
[TD]DBCT Scheduled Purged Ground Water Sites Attended[/TD]
[/TR]
[TR]
[/TR]
[TR]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [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[TD] [/TD]
[TD] [/TD]
[/TR]
</tbody>[/TABLE]
I have conditional formatting in all of the cells in the background table (box turns yellow when extra data needs to be provided.

As this takes up a lot of room, what I wanted to do was hide columns J:S unless the criteria outlined in the Macro above is entered in one of the cells between A10:A19
 
Upvote 0
Then I'd probably suggest hiding J:S to start with and have the code only unhide columns based on the values entered, not hide anything.
 
Upvote 0
Hi Rory

How would I write the code?

Below I have written a table of the defined names that can possibly appear anywhere between A10:A19 (Apart from the first one in the list "HPCT" which is located only in E4) and the columns that need to unhide based on the selection:

[TABLE="class: grid, width: 500"]
<tbody>[TR]
[TD="align: center"]Defined
Names
[/TD]
[TD="align: center"]Column
To
Unhide
[/TD]
[/TR]
[TR]
[TD]HPCT
Location: E4[/TD]
[TD="align: center"]J[/TD]
[/TR]
[TR]
[TD]Surface Water - Monthly
Surface Water - Bi-Monthly
Surface Water - Quarterly
Surface Water - Annual

Location: A10:A19[/TD]
[TD="align: center"]K[/TD]
[/TR]
[TR]
[TD]Surface Water - Investigation
Location: A10:A19[/TD]
[TD="align: center"]L[/TD]
[/TR]
[TR]
[TD]Potable Water - Investigation
Location: A10:A19[/TD]
[TD="align: center"]M[/TD]
[/TR]
[TR]
[TD]Ground Water - Unscheduled No Purge
Location: A10:A19[/TD]
[TD="align: center"]N[/TD]
[/TR]
[TR]
[TD]Ground Water - Unscheduled Purge
Location: A10:A19[/TD]
[TD="align: center"]O[/TD]
[/TR]
[TR]
[TD]Discharge Water - Daily
Discharge Water - Weekly
Discharge Water - Investigation
Location: A10:A19[/TD]
[TD="align: center"]P[/TD]
[/TR]
[TR]
[TD]Quarterly Contractor Meeting
Location: A10:A19[/TD]
[TD="align: center"]Q[/TD]
[/TR]
[TR]
[TD]Ground Water - Weekly
Ground Water - Quarterly
Ground Water - 6 Monthly
Location: A10:A19[/TD]
[TD="align: center"]R[/TD]
[/TR]
[TR]
[TD]Ground Water - Weekly
Ground Water - Quarterly
Ground Water - 6 Monthly
Location: A10:A19[/TD]
[TD="align: center"]S[/TD]
[/TR]
</tbody>[/TABLE]

Thank you heaps for your help as I'm only just starting to learn about macros
 
Upvote 0
What I mean is start with those columns hidden and then make your code unhide them as appropriate like this:

Code:
Select Case rng.Address(0, 0)
    Case "E4"
        If rng.Value = "BMA - Hay Point Coal Terminal" Then Columns("J").EntireColumn.Hidden = False
    Case Else
        If rng.Value = "Surface Water - Monthly" Then Columns("K").EntireColumn.Hidden = False
' etc
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,244
Members
452,622
Latest member
Laura_PinksBTHFT

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top