Excel VBA - 如何在特定工作表中选择列?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40547931/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Excel VBA - How to select colums in a specific sheet?
提问by Totallama
I need to select columns on a specific sheet. Somehow this is not working:
我需要选择特定工作表上的列。不知何故,这不起作用:
Dim ws As Worksheet
Set ws = Worksheets("Mysheet")
ws.Columns("A:S").Select
Selection.EntireColumn.AutoFit
And simple Columns("A:S").Select
doesn't activate the sheet I need
而且简单Columns("A:S").Select
不会激活我需要的工作表
回答by CallumDA
I tested your code and it works fine as follows.
我测试了你的代码,它工作正常,如下所示。
Sub test()
Dim ws As Worksheet
Set ws = ThisWorkbook.Worksheets("Mysheet")
ws.Columns("A:S").EntireColumn.AutoFit
End Sub
No need to Select
anything, so I put the two statements together without the Select
.
不需要Select
任何东西,所以我把这两个语句放在一起,没有Select
.
I added ThisWorkbook
to (more) fully qualify your ws
declaration. Make sure the worksheet Mysheet
is in ThisWorkbook
otherwise change that to state which workbook the sheet resides in.
我添加ThisWorkbook
到(更多)完全符合您的ws
声明。确保工作表Mysheet
在,ThisWorkbook
否则更改以说明工作表所在的工作簿。