How can I find last row that contains data in a specific column?

后端 未结 13 1584
南笙
南笙 2020-11-22 03:44

How can I find the last row that contains data in a specific column and on a specific sheet?

13条回答
  •  逝去的感伤
    2020-11-22 04:19

    function LastRowIndex(byval w as worksheet, byval col as variant) as long
      dim r as range
    
      set r = application.intersect(w.usedrange, w.columns(col))
      if not r is nothing then
        set r = r.cells(r.cells.count)
    
        if isempty(r.value) then
          LastRowIndex = r.end(xlup).row
        else
          LastRowIndex = r.row
        end if
      end if
    end function
    

    Usage:

    ? LastRowIndex(ActiveSheet, 5)
    ? LastRowIndex(ActiveSheet, "AI")
    

提交回复
热议问题