Excel 增强在工作簿中的所有工作表上运行的代码

Excel 增强在工作簿中的所有工作表上运行的代码,excel,Excel,如何更改此代码,使其在工作簿的所有工作表上运行?它运行良好,只需在所有工作表上运行即可 选项显式 选项比较文本 亚HideColumns() 将ws设置为工作表:设置ws=ThisWorkbook.Sheets(“数据”) 暗淡的迈塞尔山脉 暗隐为射程 Application.ScreenUpdating=False 对于ws.范围内的每个迈塞尔(“A2:EA2”) 如果迈塞尔是“名字”,迈塞尔是“年龄”,迈塞尔是“性别”,那么 如果隐藏是什么,那么 设置HideMe=MyCell 其他的 Se

如何更改此代码,使其在工作簿的所有工作表上运行?它运行良好,只需在所有工作表上运行即可

选项显式
选项比较文本
亚HideColumns()
将ws设置为工作表:设置ws=ThisWorkbook.Sheets(“数据”)
暗淡的迈塞尔山脉
暗隐为射程
Application.ScreenUpdating=False
对于ws.范围内的每个迈塞尔(“A2:EA2”)
如果迈塞尔是“名字”,迈塞尔是“年龄”,迈塞尔是“性别”,那么
如果隐藏是什么,那么
设置HideMe=MyCell
其他的
Set HideMe=Union(HideMe,MyCell)
如果结束
如果结束
下一个迈塞尔
如果不是的话,那么隐藏就是什么
HideMe.EntireColumn.Hidden=True
如果结束
Application.ScreenUpdating=True
端接头

在移动到下一张工作表之前,对每张工作表使用
循环并重置
HideMe
Nothing

Option Explicit
Option Compare Text

Sub HideColumns()

Dim ws As Worksheet 'Change made here
Dim MyCell As Range
Dim HideMe As Range

Application.ScreenUpdating = False
For Each ws in Worksheets 'and here
    For Each MyCell In ws.Range("A2:EA2")
        If MyCell <> "First Name" And MyCell <> "Age" And MyCell <> "Gender" Then
            If HideMe Is Nothing Then
                Set HideMe = MyCell
            Else
                Set HideMe = Union(HideMe, MyCell)
            End If
        End If
    Next MyCell

    If Not HideMe Is Nothing Then
        HideMe.EntireColumn.Hidden = True
    End If

Set HideMe = Nothing 'and here
Next ws 'and here

Application.ScreenUpdating = True

End Sub
选项显式
选项比较文本
亚HideColumns()
将ws设置为此处所做的工作表更改
暗淡的迈塞尔山脉
暗隐为射程
Application.ScreenUpdating=False
对于工作表和此处的每个ws
对于ws.范围内的每个迈塞尔(“A2:EA2”)
如果迈塞尔是“名字”,迈塞尔是“年龄”,迈塞尔是“性别”,那么
如果隐藏是什么,那么
设置HideMe=MyCell
其他的
Set HideMe=Union(HideMe,MyCell)
如果结束
如果结束
下一个迈塞尔
如果不是的话,那么隐藏就是什么
HideMe.EntireColumn.Hidden=True
如果结束
设置HideMe=Nothing'和此处
下一个ws’和这里
Application.ScreenUpdating=True
端接头

我之前就这样试过了。我尝试运行这段代码,并更新了“运行时错误'1004':应用程序定义或对象定义错误”。需要将隐藏项重置为无。尝试更新代码plzAwesome,我会接受答案(stackoverflow说我需要等待5分钟)如果列A中的单元格为空,我可能会再发布一条关于删除/隐藏行的信息=)你帮了大忙,再来一条?=)
Option Explicit
Option Compare Text

Sub HideColumns()

Dim ws As Worksheet 'Change made here
Dim MyCell As Range
Dim HideMe As Range

Application.ScreenUpdating = False
For Each ws in Worksheets 'and here
    For Each MyCell In ws.Range("A2:EA2")
        If MyCell <> "First Name" And MyCell <> "Age" And MyCell <> "Gender" Then
            If HideMe Is Nothing Then
                Set HideMe = MyCell
            Else
                Set HideMe = Union(HideMe, MyCell)
            End If
        End If
    Next MyCell

    If Not HideMe Is Nothing Then
        HideMe.EntireColumn.Hidden = True
    End If

Set HideMe = Nothing 'and here
Next ws 'and here

Application.ScreenUpdating = True

End Sub