Results 1 to 7 of 7

Thread: going thru excel grid

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2002
    Posts
    14

    going thru excel grid

    Im not sure if i am on the right track at all. What i want to do is loop thru an excel spreadsheet a import the values from each cell. I have searched thru the forum and havent found anything that really helps me, most of it actually really confuses me. So any advice would be much appreciated. Thank

    Code:
    Private Sub cmdUpdate_Click()
    Dim lRow As Long, lCol As Long, dValue As Double, dYear As Integer, dID As String
    Dim obExcelApp As Excel.Application
    Dim obWorkBook As Excel.Workbook
    Set obExcelApp = CreateObject("Excel.Application")
    Set obWorkBook = obExcelApp.Workbooks.Open("C:\windows\desktop\dfo project\temp.xls")
    
    For lRow = 1 To 'not sure what to put here i know its the last row of the excel sheet
        For lCol = 1 'not sure what to put here
            dValue = Cells(lRow, lCol).Value
            MsgBox dValue ' will do something here later once i get the data in
        Next lCol
    Next lRow
    
    Set obWorkBook = Nothing
    Set obExcelApp = Nothing
    End Sub

  2. #2
    Fanatic Member
    Join Date
    Sep 2000
    Location
    Over There
    Posts
    522
    is the format of this excel file going to be the same everytime??
    as in consistent columns and data positions??

  3. #3
    Fanatic Member Armbruster's Avatar
    Join Date
    Sep 2002
    Location
    Maryland Heights, MO
    Posts
    857

    Re: going thru excel grid

    Originally posted by Brandon141
    Im not sure if i am on the right track at all. What i want to do is loop thru an excel spreadsheet a import the values from each cell. I have searched thru the forum and havent found anything that really helps me, most of it actually really confuses me. So any advice would be much appreciated. Thank

    Code:
    Private Sub cmdUpdate_Click()
    Dim lRow As Long, lCol As Long, dValue As Double, dYear As Integer, dID As String
    Dim obExcelApp As Excel.Application
    Dim obWorkBook As Excel.Workbook
    Set obExcelApp = CreateObject("Excel.Application")
    Set obWorkBook = obExcelApp.Workbooks.Open("C:\windows\desktop\dfo project\temp.xls")
    
    For lRow = 1 To 'not sure what to put here i know its the last row of the excel sheet
        For lCol = 1 'not sure what to put here
            dValue = Cells(lRow, lCol).Value
            MsgBox dValue ' will do something here later once i get the data in
        Next lCol
    Next lRow
    
    Set obWorkBook = Nothing
    Set obExcelApp = Nothing
    End Sub
    Do you really want all 63,000 rows?

    VB Code:
    1. Private Sub cmdUpdate_Click()
    2.  
    3. Dim lRow As Long, lCol As Long, dValue As Double, dYear As Integer, dID As String
    4. Dim obExcelApp As Excel.Application
    5. Dim obWorkBook As Excel.Workbook
    6. Dim c As Excel.Range
    7.  
    8.     Set obExcelApp = CreateObject("Excel.Application")
    9.     Set obWorkBook = obExcelApp.Workbooks.Open("C:\windows\desktop\dfo project\temp.xls")
    10.    
    11.     For Each c In Excel.ActiveSheet.UsedRange
    12.         dValue = c.Value
    13.         MsgBox dValue ' will do something here later once i get the data in
    14.     Next c
    15.    
    16.     Set obWorkBook = Nothing
    17.     Set obExcelApp = Nothing
    18.  
    19. End Sub
    "Look! Up in the sky! It's a bird! It's a plane! It's Diaper-Head Boy! (there by my name!) Yes, Diaper-Head Boy, who disguised as my son, Seth, fights a never-ending battle for truth, justice and terrorizing my house!

    Resistance is futile, you will be compiled . . . Please!

  4. #4
    Fanatic Member
    Join Date
    Sep 2000
    Location
    Over There
    Posts
    522
    if the format of the file is going to be the same, I would setup an import specification in access (if you have that program) and use vb to activate the macro.

  5. #5

    Thread Starter
    New Member
    Join Date
    Sep 2002
    Posts
    14

    Do you really want all 63,000 rows???

    no, i wanna get from column 2 to 13 and from row 3 to 33. Sorry, i should have been more specific. Thanks

  6. #6
    Fanatic Member Armbruster's Avatar
    Join Date
    Sep 2002
    Location
    Maryland Heights, MO
    Posts
    857
    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub cmdUpdate_Click()
    4. Dim lRow As Long
    5. Dim lCol As Long
    6. Dim dValue As Double
    7. Dim dYear As Integer
    8. Dim dID As String
    9. Dim obExcelApp As Excel.Application
    10. Dim obWorkBook As Excel.Workbook
    11. Set obExcelApp = CreateObject("Excel.Application")
    12. Set obWorkBook = obExcelApp.Workbooks.Open("C:\windows\desktop\dfo project\temp.xls")
    13.  
    14. For lRow = 3 To 33 ' get rows 3 to 33
    15.     For lCol = 2 To 13 ' get columns 2 to 13
    16.         'I used val and text property to avoid type mismatch
    17.         'if user enters text in a cell
    18.         dValue = Val(ActiveSheet.Cells(lRow, lCol).Text)
    19.         Debug.Pring dValue; ' will do something here later once i get the data in
    20.     Next lCol
    21. Next lRow
    22.  
    23. Set obWorkBook = Nothing
    24. Set obExcelApp = Nothing
    25. End Sub

    Is that what you mean?
    "Look! Up in the sky! It's a bird! It's a plane! It's Diaper-Head Boy! (there by my name!) Yes, Diaper-Head Boy, who disguised as my son, Seth, fights a never-ending battle for truth, justice and terrorizing my house!

    Resistance is futile, you will be compiled . . . Please!

  7. #7
    Hyperactive Member
    Join Date
    Oct 2000
    Location
    Sydney Australia
    Posts
    476
    Brandon141

    In my opinion, you are considerably better to use ADO rather than OLE to connect to Excel. This bypasses a lot of issues you have with OLE such as macros !@#$$ up and causing it to to need saving.

    It gives you an ADO recordset that that you can uses as like any other recordset.

    See the attachment for an example.

    FW
    Attached Files Attached Files

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