Finding the number of non-blank columns in an Excel sheet using VBA

前端 未结 5 1471
一向
一向 2020-12-05 00:24

How do I find the number of used columns in an Excel sheet using VBA?

Dim lastRow As Long
lastRow = Sheet1.Range(\"A\" & Rows.Count).End(xlUp).Row
MsgBox         


        
5条回答
  •  生来不讨喜
    2020-12-05 01:07

    Jean-François Corbett's answer is perfect. To be exhaustive I would just like to add that with some restrictons you could also use UsedRange.Columns.Count or UsedRange.Rows.Count.
    The problem is that UsedRange is not always updated when deleting rows/columns (at least until you reopen the workbook).

提交回复
热议问题