使用If函数VBA隐藏和取消隐藏所有表中的列

我试图隐藏/取消隐藏名为VPL的一个表上的特定列,然后隐藏/取消隐藏工作簿中其余工作表上不同的一组特定列。

这里是我到目前为止的代码,但它只能在名为VPL的工作表上工作,并且在工作簿中的其余工作表循环时,不会隐藏其他工作表上的列。

Sub HideAndUnHideProduct2() 'Are you sure you want to run this macro, when you run this a box will popup and ask yes or no Dim varResponse As Variant varResponse = MsgBox("This will Hide/UnHide Product 2 on All Sheets, Do You Want To Continue", vbYesNo, "Selection") If varResponse <> vbYes Then Exit Sub Application.ScreenUpdating = False 'Hides/UnHides Product columns on all sheets If VPL.Columns("L:N").Hidden = True Then 'UnHides Specified columns On Specified Sheet VPL.Columns("L:N").EntireColumn.Hidden = False 'Unhides Selected Colunms 'UnHides columns On All Sheets Except The Ones Specified Below Dim wsU As Worksheet For Each wsU In Sheets If wsU.Name <> "VPL" Then '<Sheets To Be Skiped From Loop 'Code To Be Looped below Columns("L:M").EntireColumn.Hidden = False 'UnHides Selected Colunms 'End of Code To Be Looped End If Next wsU Else 'Hides Specified columns On Specified Sheet VPL.Columns("L:N").EntireColumn.Hidden = True 'Hides columns On All Sheets Except The Ones Specified Below Dim wsH As Worksheet For Each wsH In Sheets If wsH.Name <> "VPL" Then '<Sheets To Be Skiped From Loop 'Code To Be Looped below Columns("L:M").EntireColumn.Hidden = True 'Hides Selected columns 'End of Code To Be Looped End If Next wsH End If Application.ScreenUpdating = True End Sub 

任何帮助,非常感谢,因为我不是很好的VBA

没有图纸参考,您的代码

 Columns("L:M").EnterColumn.Hidden = False 

只会引用活动工作表。 既然你循环了多个工作表,你需要在你的for循环中引用sheetvariables,像这样

 wsU.Colunns("L:M").EnterColumn.Hidden = False 

同样的

 wsH.Columns("L:M").EntireColumn.Hidden = True