简体   繁体   English

如何使用VBA在Excel中选择标题/第一行以外的整个列

[英]How To Select Entire Column Except Header/First Row In Excel using VBA

I am trying to make a selection of an entire column except the first row . 我正在尝试选择第一行以外的整个列。 I used the following code which i got from https://www.extendoffice.com/documents/excel/1628-excel-select-column-except-header-first-row.html 我使用了从https://www.extendoffice.com/documents/excel/1628-excel-select-column-except-header-first-row.html获得的以下代码

Sub SelectColumn()
Dim xColIndex As Integer
Dim xRowIndex As Integer
xColIndex = Application.ActiveCell.Column
xRowIndex = Application.ActiveSheet.Cells(Rows.Count, xColIndex).End(xlUp).Row
Range(Cells(2, xColIndex), Cells(xRowIndex, xColIndex)).Select
End Sub

but the RowIndex variable reads to the value 1. So the section happens from Row2 to Row1 instead of Row2 to the Last row. 但是RowIndex变量读取的值为1。因此,该部分从Row2到Row1,而不是从Row2到最后一行。 How can i get this corrected 我该如何纠正

If you really need to select the whole column (why?) then this. 如果您确实需要选择整个列(为什么?),则此操作。 In your example you can't have any data in the relevant column. 在您的示例中,相关列中没有任何数据。

Range(Cells(2, xColIndex), Cells(rows.count, xColIndex)).Select

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

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