Preeti
Preeti

Reputation: 1386

How to count no of rows in selected excel file using VB6

I am using below code to read excel file: I want to print only first row data, for that I require to find no of rows in sheet (non blank) .

Set xlBook = GetObject(FilePath)
xlBook.Application.Visible = True
xlBook.Windows(1).Visible = True
xlBook.Application.WindowState = xlMinimized

Dim irow As Integer

For irow = 2 To 101
    MsgBox xlBook.Worksheets(1).Cells(irow, 1).Value
Next

Upvotes: 1

Views: 2708

Answers (1)

Siddharth Rout
Siddharth Rout

Reputation: 149295

Unfortunately I will not recommend UsedRange.

Two reasons

  1. UsedRange will not give you no of rows in sheet (non blank) as you want it.
  2. UsedRange is highly unreliable. If you want to find the last row then see this link . Please note that this will still not give you number of NON Blank Rows.

To get the number of Non Blank Rows, you will have to use AutoFilter. Use <>"" as the autofilter criteria and then use the visible cells rows count to get the non blank rows.

Edit:

See this link

This post deletes all columns which are empty apart from a specific header. I am sure you can modify it to suit your needs.

Upvotes: 1

Related Questions