我开发了一个宏来隐藏当前活动工作簿中所有工作表上的所有列(如果它们可见且不受保护)。隐藏基于运行宏时从输入框中选择的单元格值。 相关代码如下:
Sub HideColumnsSheets()
Dim ws As Worksheet
Dim InputRng As Range
Dim c As Range
Dim xTitleId As String
xTitleId = "Choose the cell you want to hide columns in all visible unprotected sheets in this workbook, based on the value of that cell."
Set InputRng = Application.Selection
Set InputRng = Application.InputBox("Select one cell :", xTitleId, InputRng.Address, Type:=8)
If InputRng = "" Then
MsgBox ("Because the selected cell is empty, the macro was not executed.")
GoTo 900 'Stop the macro because cell is empty
End If
For Each ws In ActiveWorkbook.Worksheets
If ws.Visible = False Or ws.ProtectContents = True Then GoTo 100
'If the sheet is hidden or protected go to next sheet
For Each c In ws.UsedRange.Cells
If c.Value = InputRng Then
c.EntireColumn.Hidden = True
End If
Next c
100:
Next ws
900:
End Sub
运行宏时,出现错误消息:运行时错误‘13’:类型不匹配 该错误与上面代码中的以下行相关: 错误转移到下一行:
如果 c.Value = InputRng 那么
我想帮助我纠正错误并开发宏,使其正常工作。
InputRng
微软文档:
Option Explicit
Sub HideColumnsSheets()
Dim ws As Worksheet
Dim InputRng As Range
Dim c As Range, vMch
Dim xTitleId As String
xTitleId = "Choose the cell you want to hide columns in all visible unprotected sheets in this workbook, based on the value of that cell."
On Error Resume Next
Set InputRng = Application.InputBox("Select one cell :", xTitleId, Selection.Address, Type:=8)
On Error GoTo 0
If InputRng Is Nothing Then Exit Sub ' cancel InputBox
If InputRng.Cells.CountLarge > 1 Then ' select multiple cells
MsgBox "Please selecte ONE cell."
Exit Sub
End If
If Len(InputRng.Value) = 0 Then ' blank cell
MsgBox "Because the selected cell is empty, the macro was not executed."
Exit Sub
End If
For Each ws In ActiveWorkbook.Worksheets
'If the sheet is hidden or protected go to next sheet
If ws.Visible And (Not ws.ProtectContents) Then
For Each c In ws.UsedRange.Columns ' loop through Cols
vMch = Application.Match(InputRng.Value, c, 0) ' search target value
If Not IsError(vMch) Then ' found
c.EntireColumn.Hidden = True
End If
Next c
End If
Next ws
End Sub