简体   繁体   中英

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM