Excel代码即使有空单元格也可以select列
您好我目前正在使用此代码,但它只select列,直到它find一个空单元格,我想要的是从H3单元格中select列,直到该列中的最后一个值,即使有空行
Range("H3").Select Range(Selection, Selection.End(xlDown)).Select Range(Selection, Selection.End(xlDown)).Select
简单一点。 首先,声明一个variables,它可以保存你感兴趣的列中使用的最后一行,并声明一个variables来保存范围并设置它。 从长远来看它会帮助你。
看看下面的代码…
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
with activeworkbook.sheets("sheet1") ' you did not mention sheet name range(.range("h3") , .Cells(.Rows.Count, "h").End(xlUp)).select end with
那说。 尽量不要使用Select
。 你为什么select范围?