Excel代码,即使有空单元格也可以选择列

时间:2017-08-06 04:14:09

标签: excel vba excel-vba select

您好我正在使用此代码,但它只选择列,直到找到一个空单元格,我想要的是从H3单元格中选择列,直到该列中的最后一个值,即使有空行

Range("H3").Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlDown)).Select

enter image description here

2 个答案:

答案 0 :(得分:2)

with activeworkbook.sheets("sheet1")    ' you did not mention sheet name
    range(.range("h3") , .Cells(.Rows.Count, "h").End(xlUp)).select
end with
那说的话。尽量不要使用Select。你为什么选择范围?

答案 1 :(得分:1)

简单。首先,声明一个变量,它可以保存您感兴趣的列中使用的最后一行,并声明一个变量来保存范围并进行设置。从长远来看,它会对你有所帮助。

e.g。看下面的代码......

Sub Test()
Dim LastRow As Long
Dim Rng As Range

'This will find the last row used in column H
LastRow = Cells(Rows.Count, "H").End(xlUp).Row

'Set the Rng variable
Set Rng = Range("H3:H" & LastRow)

'Now do whatever you like to do with this range, like
Rng.Select
MsgBox Rng.Address
Rng.Interior.Color = vbYellow
'etc

'If you want to perform multiple actions on the same range, you can also use With and End With block like below

With Rng
    .Value = "Test"
    .Font.Size = 14
    .Font.Bold = True
    .HorizontalAlignment = xlCenter
    .RowHeight = 25
    'etc
End With
End Sub