[英]VBA hide unhide rows based on user number entry
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... 如果用户在定义的字段中输入“ 3”,则应取消隐藏,例如3列,依此类推...
Here is the code that works fine if I do it for Rows. 如果我对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. 如果输入3,这将隐藏A:C。
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! 谢谢SJR,谢谢Dorian,两个代码都非常有效! However because columns to hide/unhide are years (2018, 2019, 2020 and so on) I want to unhide starting from the first year. 但是,由于要隐藏/取消隐藏的列是年份(2018、2019、2020等),我想从第一年开始取消隐藏。 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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.