如何实现对多个sheet的含有某些特定字段的所在列进行隐藏?
想要实现对多个sheet的含有某些特定字段的所在列进行隐藏,下边这段运行显示类型不匹配错误,请问各位大神是为什么?Sub 隐藏或删除包含特定字段的列 ()
Dim ws As Worksheet
Dim targetColumns() As String
Dim rng As Range
Dim i As Integer
' 设置需要隐藏或删除的字段名
Dim data As Variant
data = Array("字段 1 名", "字段 2 名", "字段 3 名")
targetColumns = data
' 遍历工作表
For Each ws In ThisWorkbook.Worksheets
' 设置需要隐藏列的 Range
Set rng = ws.Range(ws.Cells(1, 1), ws.Cells(ws.Rows.Count, 1).End(xlToLeft))
' 检查该列是否包含特定字段
Dim cellValue As Variant
For i = LBound(targetColumns) To UBound(targetColumns)
cellValue = rng.Cells(1, 1).Value
If cellValue = targetColumns(i) Then
' 隐藏该列
If i = LBound(targetColumns) Then
rng.Hide Shift:=xlToLeft
End If
End If
Next i
Next ws
End Sub