简体   繁体   English

选择具有空白行的范围单元格

[英]Select range cells with blank rows

I've been having difficulties with figuring out how to code this select range macro to include blank rows. 我一直很难弄清楚如何编码此选择范围宏以包括空白行。 The worksheet is a chart with variable number of columns and rows. 工作表是具有可变的列和行数的图表。 This is what I have so far: 这是我到目前为止的内容:

Sub Macro1()
    Range("A5").Select
    Range(Selection, Selection.End(xlToRight)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.Copy
End Sub

The selection step in the macro will only go so far as that blank row and won't go any further (obviously because it's blank hehe). 宏中的选择步骤只会执行到该空白行,并且不会再执行任何其他操作(显然是因为它是空白的)。 For that reason, I tried adapting this discontiguous type of code but with no luck: 因此,我尝试改编这种不连续的代码,但是没有运气:

Sub SelectRangeDown_Discontiguous()
    Range("A5", Range("A65536").End(xlUp)).Select
End Sub

I was hoping someone could help me figure out the best way of writing this code? 我希望有人能帮助我找出编写此代码的最佳方法? Am I on the right path? 我在正确的道路上吗?

If you are using .End(xlToRight) or .End(xlDown) you are going to stop at blank cells. 如果您使用的是.End(xlToRight).End(xlDown).End(xlDown)在空白单元格处停止。 If you are using Range("A65536").End(xlUp) then you are only selecting a single column but you are getting everything from A5 down to the last populated cell and bypassing interim blank cells. 如果使用的是Range("A65536").End(xlUp)则仅选择一列,但要获取从A5到最后一个填充的单元格的所有内容,并绕过临时空白单元格。 Extend this latter method laterally. 横向扩展后一种方法。

Sub Macro1()
    with Range("A5")
        .resize(cells(rows.count, "A").end(xlup).row - (.row - 1), _
                cells(5, columns.count).end(xltoleft).column - (.column - 1)).Copy
    end with
End Sub

This would be better with a .Parent Worksheet Object . 使用.Parent Worksheet Object会更好。

You do not need to .Select method something in order to .Copy it¹. 您不需要.Select方法来复制 .¹。


¹ See How to avoid using Select in Excel VBA macros for more methods on getting away from relying on select and activate to accomplish your goals. ¹ 有关如何摆脱依赖选择和激活来实现目标的更多方法,请参见如何避免在Excel VBA宏中使用“选择”

Consider: 考虑:

Sub whatever()
    Dim r1 As Range, r2 As Range

    ActiveSheet.UsedRange
    Set r1 = Range(Cells(5, 1), Cells(Rows.Count, Columns.Count))
    Set r2 = Intersect(r1, ActiveSheet.UsedRange)

End Sub

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

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