Excel VBA -如何在特定的表中选择列?

时间:2022-07-20 04:28:41

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

和简单的列(“S”)。selectdon没有激活我需要的表格

1 个解决方案

#1


1  

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。

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.

我添加了这个工作簿以(更)充分地限定您的ws声明。确保工作表Mysheet在这个工作簿中,否则将其更改为该表所在的工作簿。

#1


1  

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。

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.

我添加了这个工作簿以(更)充分地限定您的ws声明。确保工作表Mysheet在这个工作簿中,否则将其更改为该表所在的工作簿。