我在自动化企业系统中编写了以下脚本,该脚本用于隐藏excel文件中的某些单元格。但它显示
Activex错误索引
代码如下
Sub Main
Windows("InTransitReport - WELLFOAKL.xls").Activate
Application.ActiveWorkbook.Sheets("Shipments").Select
Range("Q5").Select
Selection.EntireColumn.Hidden = True
Range("R5").Select
Selection.EntireColumn.Hidden = True
Range("T5").Select
Selection.EntireColumn.Hidden = True
Range("U5").Select
Selection.EntireColumn.Hidden = True
Range("V5").Select
Selection.EntireColumn.Hidden = True
Range("W5").Select
Selection.EntireColumn.Hidden = True
Range("X5").Select
Selection.EntireColumn.Hidden = True
Range("Y5").Select
Selection.EntireColumn.Hidden = True
Range("AA5").Select
Selection.EntireColumn.Hidden = True
Range("AB5").Select
Selection.EntireColumn.Hidden = True`enter code here`
Range("AC5").Select
Selection.EntireColumn.Hidden = True
Range("A6").Select
Application.ActiveWorkbook.Sheets("POs").Select
Range("A6").Select
End Sub
答案 0 :(得分:3)
忽略“错误索引”错误,可以写为:
Workbooks("InTransitReport - WELLFOAKL.xls").Sheets("Shipments"). _
Range("Q1:R1,T1:Y1,AA1:AC1").EntireColumn.Hidden = True
如果您获得“索引超出范围”,则您的工作簿名称或工作表名称(或两者)可能会拼写错误。
答案 1 :(得分:1)
只需快速遍历这些列并将其隐藏即可。
Sub Main()
Dim wb As Workbook, ws As Worksheet
Set wb = Workbooks("InTransitReport - WELLFOAKL.xls")
Set ws = wb.Worksheets("Shipments")
Dim i As Long
For i = 17 To 29
Select Case i
Case 19, 26 'These weren't listed as a column to hide
Case Else
ws.Columns(i).Hidden = True
End Select
Next
wb.Worksheets("POs").columns(6).Hidden = True
End Sub