Unable to figure out why multiple columns are being hidden by my VBA code instead of one

TheJay

Active Member
Joined
Nov 12, 2014
Messages
364
Office Version
  1. 2019
Platform
  1. Windows
Hello there,

I have a dropdown box with three options and if I choose the first option, columns B:H are hidden when the code specifies column F only. Can someone please tell me where I have gone wrong and how to fix it?
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Address = ("$C$10") Then
        If Target.Value = "No Payments" Then
            Application.Columns("F").Select
            Application.selection.EntireColumn.Hidden = True
            Range("C10").Select
        ElseIf Target.Value = "Credit on Account" Then
            Application.Columns("F").Select
            Application.selection.EntireColumn.Hidden = False
            Range("C10").Select
        ElseIf Target.Value = "Debit on Account" Then
            Application.Columns("F").Select
            Application.selection.EntireColumn.Hidden = False
            Range("C10").Select
        End If
    End If
End Sub
Thank you.
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
Merged cells spread across those columns?

edit:- I had it in mind that merged cells would cause the joined columns to be hidden, but it appears that it is not the case. Doing a quick edit to your code but I suspect that there is something else causing the issue.
 
Last edited:
Upvote 0
Hello Jason, yes! That's the problem. Is there another way of having titles span several columns and be centred without this happening?
 
Upvote 0
Strange, I tried hiding a column manually with merged cells and it didn't cause any problems. See if this method works, although I would recommend not merging cells.
You can create the appearance of merged cells (horizontally) by formatting as 'Centre across selection'.

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Application.EnableEvents = False
    If Target.Address = ("$C$10") Then
        If Target.Value = "No Payments" Then
            Range("F:F").EntireColumn.Hidden = True
        ElseIf Target.Value = "Credit on Account" Then
            Range("F:F").EntireColumn.Hidden = False
        ElseIf Target.Value = "Debit on Account" Then
            Range("F:F").EntireColumn.Hidden = False
        End If
    End If
    Application.EnableEvents = True
End Sub
 
Upvote 0
Upvote 0
You are welcome.
Glad we were able to help.
 
Upvote 0
You already did. Just reply back saying that it is solved.
Nothing more needed to do.
 
Upvote 0

Forum statistics

Threads
1,223,904
Messages
6,175,295
Members
452,631
Latest member
a_potato

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