I would like to write a macro that unhides and hides columns based on user entry. It should unhide eg 3 columns if user enters "3" in a defined field and so on...
Here is the code that works fine if I do it for Rows. However for columns it does not work.
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Address = "$C$2" Then
Range("G:L").EntireColumns.Hidden = True
Dim i As Integer
Dim imax As Integer
Dim str1 As String
Dim str2 As String
imax = 5
If Range("C2").Value > 0 And Range("C2") <= imax Then
i = Range("C2").Value
str1 = "7:" & 7 + i
str2 = 7 + i & ":12"
Range(str1).EntireColumn.Hidden = True
Range(str2).EntireColumn.Hidden = True
End If
End If
End Sub
Maybe you can try by doing like that ( The last Code should be yours try it ) :
Sub tryme()
Dim MyRng As Range
Set MyRng = Columns("C:D")
MyRng.Hidden = True
End Sub
In your case if you want to Hide column with the number of the column and not the letter you should do stuff like this :
Sub Worksheet_SelectionChange()
Dim MyRng As Range
Dim ColumnsToHide1 As Integer
Dim ColumnsToHide2 As Integer
ColumnsToHide1 = 1
ColumnsToHide2 = 5
Set MyRng = Range(Columns(ColumnsToHide1), Columns(ColumnsToHide2))
MyRng.Hidden = True
End Sub
So maybe your code would be like this : give this a try :
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Address = "$C$2" Then
Range("G:L").EntireColumns.Hidden = True
Dim i As Integer
Dim imax As Integer
Dim str1 As Integer
Dim str11 As Integer
Dim str2 As Integer
Dim str22 As Integer
Dim MyRng1 As Range
Dim MyRng2 As Range
imax = 5
If Range("C2").Value > 0 And Range("C2") <= imax Then
i = Range("C2").Value
str1 = 7
str11 = 7 + i
str2 = 7 + i
str22 = 12
Range(Columns(str1), Columns(str11)).Hidden = True
Range(Columns(str2), Columns(str22)).Hidden = True
End If
End If
End Sub
This would hide A:C if you entered 3.
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Address = "$C$2" Then
If IsNumeric(Target.Value) Then
Range("A1").Resize(, Target.Value).EntireColumn.Hidden = True
End If
End If
End Sub
Thank you SJR, Thank you Dorian, both codes work really good! However because columns to hide/unhide are years (2018, 2019, 2020 and so on) I want to unhide starting from the first year. So I had to modify the code a little. Here is the result:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim MyRng As Range
Dim Hide1 As Integer
Dim Hide2 As Integer
If Target.Address = "$C$2" Then
Hide1 = 7
Hide2 = 14
Set MyRng = Range(Columns(Hide1), Columns(Hide2))
MyRng.Hidden = True
Dim i As Integer
Dim imax As Integer
Dim str1 As Integer
Dim str11 As Integer
Dim str2 As Integer
Dim str22 As Integer
imax = 9
If Range("C2").Value > 0 And Range("C2") <= imax Then
i = Range("C2").Value
str1 = 7
str11 = 7 + i
str2 = 7 + i
str22 = 15
Range(Columns(str1), Columns(str11)).Hidden = False
Range(Columns(str2), Columns(str22)).Hidden = True
End If
End If
End Sub
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.