Ravindra Bagale · Excelसर्व coursesया course चे lessonsशोधाEnglish

Excel · मराठी आवृत्ती

Excel

12.11 Last row आणि last column

या page मध्ये

रोज data वाढत असेल तर शेवटची row 11 किंवा 5000 hard-code करू नका. ws आधी target worksheetला Set करा.

Dim lastRow As Long, lastCol As Long
lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row          ' last filled row in column A
lastCol = ws.Cells(1, ws.Columns.Count).End(xlToLeft).Column   ' last filled column in row 1

Column Aच्या तळापासून वर गेल्यावर शेवटची populated cell मिळते. Column A प्रत्येक recordमध्ये भरलेला असावा. LastCol हे row 1मधले शेवटचे header शोधतं; dataमधले दूरचे stray values नाही.

Edge cases

  • Column पूर्ण रिकामा असेल तरी End(xlUp).Row 1 देऊ शकतो. If lastRow < 2 Then Exit Subने no-data case हाताळा.
  • Formula दिसायला blank असला तरी cell populated असू शकतो.
  • CurrentRegion blank row/columnवर थांबतो. त्याचा Rows.Count हा count आहे; starting row वेगळी असेल तर तो absolute last-row number नाही.
  • Tableसाठी ws.ListObjects("tblOrders").ListRows.Count; empty Tableमध्ये DataBodyRange Nothing असू शकतो.

4,800 records आणि header row1 असेल तर lastRow4801 आणि ws.Range("A2:A" & lastRow) = A2:A4801.

Practice

Last row, last column आणि ws.Range(ws.Cells(1,1),ws.Cells(lastRow,lastCol)).Address MsgBoxमध्ये दाखवा. Header-only sheet आणि मध्ये blank key असलेली copyही तपासा.