在单独的工作表中选择除标题之外的列的所有行

时间:2016-07-28 04:21:03

标签: excel excel-vba vbscript excel-2013 vba

我已经尝试了各种方法和答案来选择除了某个列的标题之外的所有行,但似乎都没有。

我尝试过使用(这里的列是15):

 Range(Cells(2, 15), Cells(.Cells(rows.Count, 15).End(xlUp).Row, 15)).Select

我设法在工作表上使用.Activate使用不同的语句来选择all,但这会更改工作表,您可以看到所有选中的行。这不是我需要的。用户不能在他们面前经常切换一堆纸张,这会带来糟糕的体验。

如何在不使用.Activate的情况下选择标题(第一)行之后的所有非空白列?

我需要获取这些值,将它们放在一个数组中,并检查当前单元格值是否在数组中。不要求这部分,但如果重要的话,将其作为背景提供。

2 个答案:

答案 0 :(得分:0)

以下代码从工作表中读取数据(不使用SelectActivate),并将其放入二维数组中。

Option Explicit

Sub Range_WO_Headers()

Dim Sht_Source              As Worksheet
Dim Rng                     As Range
Dim LastRow                 As Long
Dim LastCol                 As Long
Dim Rng_Array               As Variant

' modify Sheet1 according to your sheet name
Set Sht_Source = ActiveWorkbook.Worksheets("Sheet1")

' assuming the table's data starts from Cell A1
LastRow = Sht_Source.Cells(Sht_Source.Rows.Count, "A").End(xlUp).Row
LastCol = Sht_Source.Cells(1, Sht_Source.Columns.Count).End(xlToLeft).Column

' resize array according to number of columns and number of rows
ReDim Rng_Array(0 To LastRow, 0 To LastCol)

' set dynamic array from Cell A1 to last row and last column found (starting the second row)
Set Rng = Sht_Source.Range(Cells(2, 1), Cells(LastRow, LastCol))

Rng_Array = Application.Transpose(Rng)

End Sub

答案 1 :(得分:0)

您无法在非活动工作表上选择范围。

以下是如何设置对标题行以外的所有单元格的引用。

Dim TargetRange As Range

With Worksheets("Sheet1")

    Set TargetRange = .Range(.Cells(2, 15), .Cells(Rows.Count, 15).End(xlUp))

End With
相关问题