简体   繁体   English

如何引用工作簿中的工作表?

[英]how to refer to a worksheet in a workbook?

manage to get to here but cannot delete the columns in the worksheet, can you please help?设法到达这里但无法删除工作表中的列,您能帮忙吗? Sorry, big mess even to post the questions对不起,即使发布问题也很混乱

Private Sub CommandButton2_Click() ' DELETE ROWS AND COLUMNS (TRIM FILE)

Sheets("RAW DATA FILE").Range("1:7").EntireRow.Delete

Sheets("RAW DATA FILE").Columns("v" & "x" & "z" & "ab" & "ad" & "af" & "ag" & "ah" & "ai" & "aj" & "ak" & "al").EntireColumn.Delete

End Sub

Can someone help please?有人可以帮忙吗?

There are a few ways you can refer to another worksheet in a workbook, and even refer to another workbook.有几种方法可以引用工作簿中的另一个工作表,甚至引用另一个工作簿。

Refer to another sheet with Sheets( or Worksheets( . This can take two arguments, either a sheet number Sheets(1) , but I find this unreliable as it sometimes is hard to keep track of the proper sheet numbers when adding and removing sheets. The best way is to refer directly by the sheet name: Sheets("Sheetname") . Please note there is a slight difference in using Sheets or Worksheets .使用Sheets(Worksheets( ) 参考另一张表。这可能需要两个 arguments,或者是表号Sheets(1) ,但我发现这不可靠,因为有时在添加和删除表时很难跟踪正确的表号。最好的方法是直接通过工作表名称引用: Sheets("Sheetname") 。请注意,使用SheetsWorksheets会有细微的差别

As SJR pointed out there is a third way to refer to sheets with the sheet codename .正如 SJR 指出的,还有第三种方法可以使用工作表codename来引用工作表。 The codename is the name of the sheet displayed in your VBA window.代号是 VBA window 中显示的工作表的名称。 You can get the codename of your sheet with Debug.Print Sheet("name").Codename .您可以使用Debug.Print Sheet("name").Codename获取工作表的代号。 And you can refer directly to this sheet like so Sheet1.Range("A:A") etc.您可以直接参考此表,例如Sheet1.Range("A:A")等。

To make sure you refer to the right sheet within the workbook you have open, and prevent yourself from having your VBA run in another workbook you have open at the same time use either Activeworkbook.Sheets , which refers to the workbook which is currently active, or Thisworkbook.Sheets which refers to the specific workbook the VBA code is in. You can also use Workbook("Workbook name").sheets .为了确保您在已打开的工作簿中引用正确的工作表,并防止您的 VBA 在您同时打开的另一个工作簿中运行,请使用Activeworkbook.Sheets ,它指的是当前处于活动状态的工作簿,或Thisworkbook.Sheets指的是 VBA 代码所在的特定工作簿。您也可以使用Workbook("Workbook name").sheets

Some further reading for you. 一些进一步的阅读给你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM