Results 1 to 10 of 10

Thread: Counting until end of excel sheet

  1. #1

    Thread Starter
    Member
    Join Date
    Feb 2006
    Posts
    48

    Counting until end of excel sheet

    Hi,

    I was windering if there is a way to stop a loop when it reaches the end of an excel sheets data rows i.e if there is 3000 rows the loop will only run 3000 times.

    Thanks

    C19

  2. #2

    Thread Starter
    Member
    Join Date
    Feb 2006
    Posts
    48

    Re: Counting until end of excel sheet

    Guys,

    I have this code but is there anything more efficient....

    VB Code:
    1. Sub exportMargins()
    2.  
    3.  Dim acbsDB As Database, marginsRS As Recordset, r As Long, RecordNumber As Long
    4.     Dim queryNo As Double
    5.     Set acbsDB = OpenDatabase("S:\Hockney Folder Structure\CUSTOMER SERVICE\Michael\ACBS\ACBS.mdb")
    6.      ' open the database
    7.     Set marginsRS = acbsDB.OpenRecordset("Margins")
    8.      
    9.      ' get all records in a table
    10.     r = 2 ' the start row in the worksheet
    11. Do While Sheet1.Range("A" & r).Value <> ""
    12.    
    13.    
    14.     With marginsRS
    15.         .AddNew ' create a new record
    16.          ' add values to each field in the record
    17.         RecordNumber = .Fields(0)
    18.         .Fields("Portfolio") = Sheet1.Range("A" & r).Value
    19.         .Fields("Cost_Centre_Number") = Sheet1.Range("B" & r).Value
    20.         .Fields("ACBS_Customer_Name") = Sheet1.Range("C" & r).Value
    21.         .Fields("Credit_Arrangement_Number") = Sheet1.Range("D" & r).Value
    22.         .Fields("Loan_Number") = Sheet1.Range("E" & r).Value
    23.         .Fields("Product_Group_Description") = Sheet1.Range("F" & r).Value
    24.         .Fields("Currency") = Sheet1.Range("G" & r).Value
    25.         .Fields("Interest_Income") = Sheet1.Range("H" & r).Value
    26.         .Fields("Base_Currency_Equiv_Interest_Income") = Sheet1.Range("I" & r).Value
    27.         .Fields("Interest_Expense") = Sheet1.Range("J" & r).Value
    28.         .Fields("Base_Currency_Interest_Expense") = Sheet1.Range("K" & r).Value
    29.         .Fields("Cummulative_Base_Currency_Margin") = Sheet1.Range("L" & r).Value
    30.         .Fields("Top_Hierarchical_Group") = Sheet1.Range("M" & r).Value
    31.         .Fields("Secondary_Hierarchical_Group") = Sheet1.Range("N" & r).Value
    32.         .Fields("Notes") = Sheet1.Range("O" & r).Value
    33.          ' add more fields if necessary...
    34.         .Update ' stores the new record
    35.         End With
    36.         r = r + 1 ' count the rows to determine if data exists (exit loop)
    37. Loop
    38.  
    39.     marginsRS.Close
    40.     Set marginsRS = Nothing
    41.     acbsDB.Close
    42.     Set acbsDB = Nothing
    43. End Sub

  3. #3
    Don't Panic! Ecniv's Avatar
    Join Date
    Nov 2000
    Location
    Amsterdam...
    Posts
    5,343

    Re: Counting until end of excel sheet

    you can use the equivalent of pressing end then up from cell A65500

    Some thing like
    Code:
    lngLastRow=sht.cells(65500,1).End(xlUp).Row
    This assumes no entries have been made beneath the last data cell used in column 1 (A).

    Or you could go from the top down, but the top down method may take you to the bottom of the sheet
    Code:
    lngLastRow=sht.cells(1,1).End(xlDown).Row
    This one assumes continuous data in column 1 (A) from the title/data line in row 1.


    hope that helps a bit.

    BOFH Now, BOFH Past, Information on duplicates

    Feeling like a fly on the inside of a closed window (Thunk!)
    If I post a lot, it is because I am bored at work! ;D Or stuck...
    * Anything I post can be only my opinion. Advice etc is up to you to persue...

  4. #4

    Thread Starter
    Member
    Join Date
    Feb 2006
    Posts
    48

    Re: Counting until end of excel sheet

    Hi,

    Thanks - looks a lot cleaner.

    Cheers

    C19

  5. #5
    Frenzied Member
    Join Date
    May 2004
    Location
    Carlisle, PA
    Posts
    1,045

    Re: Counting until end of excel sheet

    I guess you've figured out by now that there are 65,536 rows max in an Excell spreadsheet.

    I think that this function from Vince is what you were looking for if you want to universally calculate the number (but 65,536 has been the actual number for a long time now) ... with a Message Box to show the value of the function:
    Code:
    MsgBox ActiveWorkbook.ActiveSheet.Cells(1, 1).End(xlDown).Row
    Blessings in abundance,
    All the Best,
    & ENJOY!

    Art . . . . Carlisle, PA . . USA

  6. #6
    Fanatic Member Comintern's Avatar
    Join Date
    Nov 2004
    Location
    Lincoln, NE
    Posts
    826

    Re: Counting until end of excel sheet

    I would personally use the UsedRange property of the sheet:
    VB Code:
    1. Private Sub LoopUntilEnd()
    2.  
    3.     Dim oRange As Range, lRow As Long
    4.    
    5.     Set oRange = Application.ActiveSheet.UsedRange
    6.    
    7.     'Loop through all rows
    8.     For lRow = 1 To oRange.Rows.Count
    9.         'Do whatever
    10.         Debug.Print lRow
    11.     Next lRow
    12.  
    13.     Set oRange = Nothing
    14.  
    15. End Sub

  7. #7
    Frenzied Member
    Join Date
    May 2004
    Location
    Carlisle, PA
    Posts
    1,045

    Re: Counting until end of excel sheet

    I think the original poster wanted to be sure he didn't roll off the bottom of the Worksheet as he added rows. (???)
    Blessings in abundance,
    All the Best,
    & ENJOY!

    Art . . . . Carlisle, PA . . USA

  8. #8
    Don't Panic! Ecniv's Avatar
    Join Date
    Nov 2000
    Location
    Amsterdam...
    Posts
    5,343

    Re: Counting until end of excel sheet

    Comintern:
    Used range usually works ok.

    But if you add a couple of extra lines and delete the data out, the used range stays larger than the data is so may throw off the counter.

    Whichever way the original poster decides to do... hope it works. Also if the OP has the time, try all methods so you can compare.

    BOFH Now, BOFH Past, Information on duplicates

    Feeling like a fly on the inside of a closed window (Thunk!)
    If I post a lot, it is because I am bored at work! ;D Or stuck...
    * Anything I post can be only my opinion. Advice etc is up to you to persue...

  9. #9
    Frenzied Member
    Join Date
    May 2004
    Location
    Carlisle, PA
    Posts
    1,045

    Re: Counting until end of excel sheet

    Ummmmm ... Vince ... if your last line was added at the bottom of the sheet (row 65,536 or whatever the bottom row calculates out to be ... 65,536) ... it's time to stop adding lines to the sheet and maybe open a new sheet or switch to Access.

    Where is the Original Poster? He could put this all to rest.

    Sorry ... I am in outer space ... I concede that the OP was looking for the actual used range. Pardon my dumb flailing.
    Last edited by Webtest; Feb 28th, 2006 at 11:16 AM.
    Blessings in abundance,
    All the Best,
    & ENJOY!

    Art . . . . Carlisle, PA . . USA

  10. #10
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Counting until end of excel sheet

    Also, to determine the last used row...
    VB Code:
    1. MsgBox Workbooks(1).Sheets(1).Cells.SpecialCells(xlCellTypeLastCell).Row
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width