|
-
Jul 29th, 2004, 03:04 PM
#1
Thread Starter
Hyperactive Member
How to determine the number of Rows/Columns in Excel File [VB6]
Given an excel file with X number of rows each having Y columns.
I need a mechanism that will allow me select each Cell.
For example:
For ( j = 0; j < x ; j++)
For (k = 0; k < y; k++)
‘Do whatever needs to be done.
This will then go over each row and each column in each row.
Given that I am importing data I do know beforehand how many columns (Y) should be there, I just want to ensure there is actually the right amount and therefore I need a way to determine (Y) from the file itself and compare it with the # I am expecting.
For the rows it is a little different, I need to go down and loop through all rows in the file, how do I tell it when to stop?
Is there a totally different/better way to do this?
Something built into the Excel object maybe? (Excel.endrow or something?)
Code so far:
Dim oExcel As Object
Dim oBook As Object
Dim oSheet As Object
Set oExcel = CreateObject("Excel.Application")
Set oBook = oExcel.Workbooks.Open(c:\test.xls, True)
Thanks,
-
Jul 29th, 2004, 06:06 PM
#2
This should help you.
VBA code...
VB Code:
Private Sub Workbook_Open()
MaxRowNumber = ActiveSheet.Cells.SpecialCells(xlCellTypeLastCell).Row
MaxColNumber = ActiveSheet.Cells.SpecialCells(xlCellTypeLastCell).Column
MsgBox "Last Row: " & MaxRowNumber & ", Col: " & MaxColNumber, vbOKOnly + vbInformation
End Sub
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 
-
Aug 2nd, 2004, 10:10 AM
#3
Thread Starter
Hyperactive Member
Looks like the best method I have seen so far.
One little question, how do I define the variable "xlCellTypeLastCell"?
I copy/pasted your VBA code below (to see it work) and keep getting the typical "Compile Error: Variable not defined" for xlCellTypeLastCell.
Is there a special way to define it?
As what?
-
Aug 2nd, 2004, 12:53 PM
#4
It could be that I am using Excel 2003.
The definition is as follows from the Object Browser.
VB Code:
Const xlCellTypeLastCell = 11
What is the version you are using?
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
|