Results 1 to 4 of 4

Thread: How to determine the number of Rows/Columns in Excel File [VB6]

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2003
    Posts
    308

    Question 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,

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    This should help you.
    VBA code...

    VB Code:
    1. Private Sub Workbook_Open()
    2.     MaxRowNumber = ActiveSheet.Cells.SpecialCells(xlCellTypeLastCell).Row
    3.     MaxColNumber = ActiveSheet.Cells.SpecialCells(xlCellTypeLastCell).Column
    4.     MsgBox "Last Row: " & MaxRowNumber & ", Col: " & MaxColNumber, vbOKOnly + vbInformation
    5. 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 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

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2003
    Posts
    308
    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?

  4. #4
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    It could be that I am using Excel 2003.
    The definition is as follows from the Object Browser.

    VB Code:
    1. 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 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