Vba to conditionally hide/unhide columns

CROY1985

Well-known Member
Joined
Sep 21, 2009
Messages
501
Office Version
  1. 365
Platform
  1. Windows
I want to automatically hide columns K:Q when the condition of cell C3 is "No Report". C3 is a validation list box with the following options; Weekly, Monthly, Year To Date, Contract To Date, No Report.

There are also date range inputs for the report, so i have a macro which refreshes the whole workbook once these other inputs have been selected.

The code i have is:

Code:
Sub Update()
'
' Update Macro
    ActiveWorkbook.RefreshAll
End Sub

What would i need to add to this to have it hide/unhide columns k:q given the condition of C3? (Hide if it is "No Report", Show if it is not "No Report")

Thanks
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
try this code

Code:
If Range("C3") = "no report" Then
Columns("K:Q").Hidden = True
Else
Columns("K:q").Hidden = False
End If

"no report"-take care of spelling and case both in sheet (C3) and in macro.
 
Upvote 0
Will give that a try tommorow. Thank you, it look as though it will work a treat. I know what you mean about the spelling issue, so im using data validation boxes for any user inputs and Hopefully, through thorough testing, can be assured of correct formula and vba.

Thanks again.
 
Upvote 0
Just need some further assistance with the above code. If i want to run it on a specific sheet what do i add to the code, is it just

Code:
sheetname.select

after:

Code:
If Range("C3") = "no report" Then

I havent got access to excel at the moment so i cant just go in and try it.

Thanks
 
Upvote 0
worksheets(sheet name within double quotes).activate

another way of not selecting sheet

with worksheets(sheetname within double quotes)
i

If .Range("C3") = "no report" Then .Columns("K:Q").Hidden = True Else .Columns("K:q").Hidden = False End If
end with


'note the dots before range and columns.
</pre>
 
Upvote 0

Forum statistics

Threads
1,224,599
Messages
6,179,831
Members
452,947
Latest member
Gerry_F

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