|
-
Feb 16th, 2006, 07:40 AM
#1
Thread Starter
Member
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
-
Feb 16th, 2006, 07:52 AM
#2
Thread Starter
Member
Re: Counting until end of excel sheet
Guys,
I have this code but is there anything more efficient....
VB Code:
Sub exportMargins()
Dim acbsDB As Database, marginsRS As Recordset, r As Long, RecordNumber As Long
Dim queryNo As Double
Set acbsDB = OpenDatabase("S:\Hockney Folder Structure\CUSTOMER SERVICE\Michael\ACBS\ACBS.mdb")
' open the database
Set marginsRS = acbsDB.OpenRecordset("Margins")
' get all records in a table
r = 2 ' the start row in the worksheet
Do While Sheet1.Range("A" & r).Value <> ""
With marginsRS
.AddNew ' create a new record
' add values to each field in the record
RecordNumber = .Fields(0)
.Fields("Portfolio") = Sheet1.Range("A" & r).Value
.Fields("Cost_Centre_Number") = Sheet1.Range("B" & r).Value
.Fields("ACBS_Customer_Name") = Sheet1.Range("C" & r).Value
.Fields("Credit_Arrangement_Number") = Sheet1.Range("D" & r).Value
.Fields("Loan_Number") = Sheet1.Range("E" & r).Value
.Fields("Product_Group_Description") = Sheet1.Range("F" & r).Value
.Fields("Currency") = Sheet1.Range("G" & r).Value
.Fields("Interest_Income") = Sheet1.Range("H" & r).Value
.Fields("Base_Currency_Equiv_Interest_Income") = Sheet1.Range("I" & r).Value
.Fields("Interest_Expense") = Sheet1.Range("J" & r).Value
.Fields("Base_Currency_Interest_Expense") = Sheet1.Range("K" & r).Value
.Fields("Cummulative_Base_Currency_Margin") = Sheet1.Range("L" & r).Value
.Fields("Top_Hierarchical_Group") = Sheet1.Range("M" & r).Value
.Fields("Secondary_Hierarchical_Group") = Sheet1.Range("N" & r).Value
.Fields("Notes") = Sheet1.Range("O" & r).Value
' add more fields if necessary...
.Update ' stores the new record
End With
r = r + 1 ' count the rows to determine if data exists (exit loop)
Loop
marginsRS.Close
Set marginsRS = Nothing
acbsDB.Close
Set acbsDB = Nothing
End Sub
-
Feb 16th, 2006, 08:08 AM
#3
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.
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...
-
Feb 16th, 2006, 08:18 AM
#4
Thread Starter
Member
Re: Counting until end of excel sheet
Hi,
Thanks - looks a lot cleaner.
Cheers
C19
-
Feb 23rd, 2006, 12:16 PM
#5
Frenzied Member
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
-
Feb 23rd, 2006, 01:53 PM
#6
Re: Counting until end of excel sheet
I would personally use the UsedRange property of the sheet:
VB Code:
Private Sub LoopUntilEnd()
Dim oRange As Range, lRow As Long
Set oRange = Application.ActiveSheet.UsedRange
'Loop through all rows
For lRow = 1 To oRange.Rows.Count
'Do whatever
Debug.Print lRow
Next lRow
Set oRange = Nothing
End Sub
-
Feb 23rd, 2006, 02:16 PM
#7
Frenzied Member
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
-
Feb 28th, 2006, 07:31 AM
#8
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.
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...
-
Feb 28th, 2006, 08:13 AM
#9
Frenzied Member
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
-
Feb 28th, 2006, 10:09 AM
#10
Re: Counting until end of excel sheet
Also, to determine the last used row...
VB Code:
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|