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

前端 未结 5 1469
一向
一向 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:26

    It's possible you forgot a sheet1 each time somewhere before the columns.count, or it will count the activesheet columns and not the sheet1's.

    Also, shouldn't it be xltoleft instead of xltoright? (Ok it is very late here, but I think I know my right from left) I checked it, you must write xltoleft.

    lastColumn = Sheet1.Cells(1, sheet1.Columns.Count).End(xlToleft).Column
    

提交回复
热议问题