ActiveSheet.UsedRange.Columns.Count - 8 what does it mean?

后端 未结 5 2071
孤独总比滥情好
孤独总比滥情好 2020-12-28 09:14

what does ActiveSheet.UsedRange.Columns.Count - 8 mean in vba?

How does vba know the usedRange?

5条回答
  •  失恋的感觉
    2020-12-28 09:52

    Here's the exact definition of UsedRange (MSDN reference) :

    Every Worksheet object has a UsedRange property that returns a Range object representing the area of a worksheet that is being used. The UsedRange property represents the area described by the farthest upper-left and farthest lower-right nonempty cells in a worksheet and includes all cells in between.

    So basically, what that line does is :

    1. .UsedRange -> "Draws" a box around the outer-most cells with content inside.
    2. .Columns -> Selects the entire columns of those cells
    3. .Count -> Returns an integer corresponding to how many columns there are (in this selection)
    4. - 8 -> Subtracts 8 from the previous integer.

    I assume VBA calculates the UsedRange by finding the non-empty cells with lowest and highest index values.

    Most likely, you're getting an error because the number of lines in your range is smaller than 3, and therefore the number returned is negative.

提交回复
热议问题