ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Discussion (Misc queries) (https://www.excelbanter.com/excel-discussion-misc-queries/)
-   -   Writing a macro to hide columns based on cell value (https://www.excelbanter.com/excel-discussion-misc-queries/227879-writing-macro-hide-columns-based-cell-value.html)

JAbels001

Writing a macro to hide columns based on cell value
 
I'm trying to write a macro that will automatically hide certain columns
based on a cell's value. Basically, if C3 = "Sales" I need columns R, S, and
T to be hiden; however if C3 does not equal "Sales" i need columns L, M and N
to be hiden, but R, S, and T need to come back into view and vice versa. Is
there a macro that can assist me? Please help. Thanks!

Luke M

Writing a macro to hide columns based on cell value
 
Right click on sheet tab, view code, and paste appropriate macro in.
If C3 is controlled by formula:

Private Sub Worksheet_Calculate()

If Range("c3").Value = "Sales" Then
Columns("R:T").EntireColumn.Hidden = True
Columns("L:N").EntireColumn.Hidden = False
Else
Columns("R:T").EntireColumn.Hidden = False
Columns("L:N").EntireColumn.Hidden = True
End If

End Sub


If inputted manually:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target = Range("C3") Then
If Range("c3").Value = "Sales" Then
Columns("R:T").EntireColumn.Hidden = True
Columns("L:N").EntireColumn.Hidden = False
Else
Columns("R:T").EntireColumn.Hidden = False
Columns("L:N").EntireColumn.Hidden = True
End If
End If

End Sub
--
Best Regards,

Luke M
*Remember to click "yes" if this post helped you!*


"JAbels001" wrote:

I'm trying to write a macro that will automatically hide certain columns
based on a cell's value. Basically, if C3 = "Sales" I need columns R, S, and
T to be hiden; however if C3 does not equal "Sales" i need columns L, M and N
to be hiden, but R, S, and T need to come back into view and vice versa. Is
there a macro that can assist me? Please help. Thanks!


JAbels001

Writing a macro to hide columns based on cell value
 
Worked like a charm! Thanks!

"Luke M" wrote:

Right click on sheet tab, view code, and paste appropriate macro in.
If C3 is controlled by formula:

Private Sub Worksheet_Calculate()

If Range("c3").Value = "Sales" Then
Columns("R:T").EntireColumn.Hidden = True
Columns("L:N").EntireColumn.Hidden = False
Else
Columns("R:T").EntireColumn.Hidden = False
Columns("L:N").EntireColumn.Hidden = True
End If

End Sub


If inputted manually:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target = Range("C3") Then
If Range("c3").Value = "Sales" Then
Columns("R:T").EntireColumn.Hidden = True
Columns("L:N").EntireColumn.Hidden = False
Else
Columns("R:T").EntireColumn.Hidden = False
Columns("L:N").EntireColumn.Hidden = True
End If
End If

End Sub
--
Best Regards,

Luke M
*Remember to click "yes" if this post helped you!*


"JAbels001" wrote:

I'm trying to write a macro that will automatically hide certain columns
based on a cell's value. Basically, if C3 = "Sales" I need columns R, S, and
T to be hiden; however if C3 does not equal "Sales" i need columns L, M and N
to be hiden, but R, S, and T need to come back into view and vice versa. Is
there a macro that can assist me? Please help. Thanks!



All times are GMT +1. The time now is 04:52 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com