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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 11:31:00  来源:igfitidea点击:

Excel VBA - How to select colums in a specific sheet?

excelvbaselect

提问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").Selectdoesn'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 Selectanything, so I put the two statements together without the Select.

不需要Select任何东西,所以我把这两个语句放在一起,没有Select.

I added ThisWorkbookto (more) fully qualify your wsdeclaration. Make sure the worksheet Mysheetis in ThisWorkbookotherwise change that to state which workbook the sheet resides in.

我添加ThisWorkbook到(更多)完全符合您的ws声明。确保工作表Mysheet在,ThisWorkbook否则更改以说明工作表所在的工作簿。