简体   繁体   中英

Finding the last cell in an excel sheet?

I'm using vb6 and copying data to an excel sheet. What I need to do is append data to an existing sheet. I am at the point where I need to find the index number of the last cell in the sheet (there's only one sheet.) I've tried a couple things like oBook.range.cells.specialcells(xllastcell) but to no avail. What I have right now to open the worksheet I want is this

If Not Dir(strPath) = "" Then
Set oExcel = CreateObject("Excel.application")
Set oBook = oExcel.workbooks.open(strPath)

but I have no idea what to do from here to find the last cell in the workbook.

There are several ways to do this. My favorite way is to do:

count = Application.CountA(Range("A:A")) 'you must use whatever column will give an accurate count

"count" will then be the last row containing data.

Upon scouring the internet for the answer I found a line of code that worked. I had to change some of the values such as xlbyrows to "1" and xlprevious to "2" because vb6 dows not recognize the terms but can use the number constants.

this was the original line of code

LastRow = Cells.Find("*",SearchOrder:=xlByRows,SearchDirection:=xlPrevious).Row

this is what I changed it to that worked with my code

iCell = oExcel.cells.find("*", searchorder:=1, searchdirection:=2).row

Info from http://msdn.microsoft.com/en-us/library/aa139976%28v=office.10%29.aspx . It deals with VBA, but it may help you here. It is an old reference, but in this respect valid.

Look under Last Cell . In particular,

"The last cell is considered to be the intersection of the highest numbered row in the worksheet that contains information and the highest numbered column in the worksheet that contains information. Excel also includes cells that have contained information during the current session, even if you have deleted that information. The last cell is not reset until you save the worksheet .

Excel considers formatted cells and unlocked cells to contain information. As a result, you will often find the last cell well beyond the region containing data, especially if the workbook has been imported from another spreadsheet application, such as Lotus 1-2-3. If you want to consider only cells that contain data in the form of numbers, text, and formulas, you can use the following code:

Sub GetRealLastCell()
  Dim lRealLastRow As Long
  Dim lRealLastColumn As Long
  Range("A1").Select
  On Error Resume Next
  lRealLastRow = Cells.Find("*", Range("A1"), xlFormulas, , xlByRows, _
                                               xlPrevious).Row
  lRealLastColumn = Cells.Find ("*",Range("A1"), xlFormulas, , _
                                    xlByColumns, xlPrevious).Column
  Cells(lRealLastRow, lRealLastColumn).Select
End Sub

"

See also this .

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