[英]Hiding Range of Columns in Excel using VBA based on cell Values
I need to hide a range of cells using a macro in excel. 我需要使用Excel中的宏隐藏一定范围的单元格。 C11 contains the column index from where I need to start hiding the columns.
C11包含我需要开始隐藏列的列索引。
Sub test()
Dim i As Integer
Dim j As Integer
Dim rocket As Range
i = Range("c11").Value
j = 12
rocket = Range(Cells(5, i), Cells(5, j))
Range("Rocket").Select
Selection.EntireColumn.Hidden = True
End Sub
The code is giving some unexpected error and as I am a novice, so have no clue what needs to be done.. 该代码给出了一些意外的错误,并且由于我是新手,所以不知道需要做什么。
Tree steps to make your code working: 树状步骤使您的代码正常工作:
1st. 1号 Add
Set
key word in appropriate line which is necessary: 在适当的行中添加
Set
关键字,这是必要的:
Set rocket = Range(Cells(5, i), Cells(5, j))
2nd. 2号
Rocket variable
represents range, you will NOT need to call it in this way: Rocket variable
代表范围,您无需以这种方式调用它:
Range("Rocket")....
but 但
rocket....
3rd. 第三名 Avoid
Select method
and Selection object
always when possible. 尽可能避免使用
Select method
和Selection object
。 Therefore the last two lines replace with this single one (which implements 2nd step, too): 因此,最后两行替换为这一行(也实现了第二步):
rocket.EntireColumn.Hidden = true
That last answer was awesome! 最后一个答案真棒! Just for someone else's FYI, here is what worked in Excel 2007. The first line is always 3, but the ending line needed to be a variable.
仅对于其他人的FYI,这就是在Excel 2007中起作用的方法。第一行始终为3,但最后一行必须为变量。 That's where I had the problem.
那就是我的问题所在。 THIS FIXED IT!
此问题已解决! The last 4 lines before the "End If" do the work.
“ End If”之前的最后4行完成工作。 Hope this helps!
希望这可以帮助!
Dim RowsToHide As Range
Dim RowHideNum As Integer
' Set Correct Start Dates for Billing in New File
Workbooks("----- Combined_New_Students_Updated.xlsx").Activate
Sheets("2015").Activate
StartDateLine1 = Format(START_DATE_1, "ww") - 1 ' Convert Start Date to Week Number
StartDateLine1 = (StartDateLine1 * 6) - 2 ' Convert Start Date to Line Number
If StartDateLine1 >= "10" Then
Cells(4, "q").Value = ""
Cells(StartDateLine1, "q").Value = STATUS_1
Cells(StartDateLine1, "z").Value = "START DATE " + START_DATE_1
RowHideNum = StartDateLine1 - 2
Set RowsToHide = Range(Cells(3, "a"), Cells(RowHideNum, "ab"))
RowsToHide.Select
RowsToHide.EntireRow.Hidden = True
End If
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.