Results 1 to 14 of 14

Thread: Get the last row in the field in my ExcelSheet?

  1. #1
    Pirre001
    Guest

    Question Get the last row in the field in my ExcelSheet?

    How do I do to get the value in the last row in the field, in my excelsheet, into a string. The problem is that the last row is a SUM function. "=SUM(A1:A40)".
    Can anyone help me with that?

  2. #2
    Addicted Member
    Join Date
    Aug 2001
    Location
    Minneapolis, MN
    Posts
    189
    I'm assuming that you already know how to access data in Excel...
    So, just do this:
    Code:
    SomeVariable = objExcel.Cells(Row, Column)
    I've also assumed that you know what cell you want to read. If you don't, I can post some code to find the last used cell, let me know.

  3. #3
    Fanatic Member SeaHag's Avatar
    Join Date
    Jul 2001
    Location
    Lake Huron
    Posts
    901
    For dynamic
    lists

    VB Code:
    1. Private Sub CommandButton1_Click() ' put a bunch of stuff in col a
    2.  
    3. Dim myRange As Range
    4.  
    5. Range("a1").Select
    6. Set myRange = ActiveCell.CurrentRegion
    7. MsgBox myRange.Count
    8.  
    9. Set myRange = Nothing
    10. End Sub



    Seahag

  4. #4
    Fanatic Member SeaHag's Avatar
    Join Date
    Jul 2001
    Location
    Lake Huron
    Posts
    901

    oops should read the thresd

    VB Code:
    1. MsgBox myRange(myRange.Count)


    That is all

  5. #5
    Pirre001
    Guest
    Thanks again, SeaHag! And thanks Kraig K!

  6. #6
    Pirre001
    Guest
    Help...

  7. #7
    Fanatic Member SeaHag's Avatar
    Join Date
    Jul 2001
    Location
    Lake Huron
    Posts
    901
    I dont know what u want..
    What values where.
    It looks like your on the right track though.




    (this forum is sloooooooowwwww)

  8. #8
    Pirre001
    Guest
    In field "C" in worksheet "Total" in excelfile "C:\test.xls" is a numeric field with many rows, i dont know how many it is, it various from time to time. How can I Sum this filed an put the sumvalue into a string or in VB? ...

  9. #9
    Addicted Member
    Join Date
    Aug 2001
    Location
    Minneapolis, MN
    Posts
    189
    Yep, I gotcha. I was working on getting some code for you but I had to reboot. Anywho, this is what I used to count the number of cells and enter a formula.

    Code:
    objExcel.Sheets("Sheet1").Select
       Rows = objExcel.ActiveSheet.UsedRange.Rows.Count
    Print Rows
    
    objExcel.ActiveCell.Formula = "=SUM(C1:C10)"
    his just gives you something to start with, I haven't tested it. Give me a few minutes and I can post some working code...

  10. #10
    Pirre001
    Guest
    Ok. thanks...

  11. #11
    Addicted Member
    Join Date
    Aug 2001
    Location
    Minneapolis, MN
    Posts
    189

    Thumbs up

    Here you go:

    Code:
    Option Explicit
    Dim objExcel As New Excel.Application
    Dim objBook As New Excel.Workbook
    Dim Rows As Integer
    Dim Formula As Variant
    
    Private Sub Form_Load()
    
       On Error GoTo ErrHandler
       
       Set objBook = objExcel.Workbooks.Open("c:\temp\test.xls")
       With objExcel
       .Sheets("Sheet1").Select
       Rows = .ActiveSheet.UsedRange.Rows.Count
       .Range("A" & (Rows + 1)).Activate
       Formula = "=SUM(A1" & ":A" & Rows & ")"
       .Cells(Rows + 1, 1) = Formula
       objBook.Save
       .Workbooks.Close
       End With
       Set objExcel = Nothing
       Print "Done"
       
    ErrHandler:
    Print Err.Description
       objExcel.Workbooks.Close
       Set objExcel = Nothing
       Exit Sub
    End Sub
    I know this line looks like a mess:

    Formula = "=SUM(A1" & ":A" & Rows & ")"

    but it's the only way I've found to get variables in an Excel formula. If you put one in a formula, it enters it as text. I just figured out how to do this right now, so thanks for the lesson!

  12. #12
    Pirre001
    Guest
    Thanks again Kraig K,

    But there is a little problem. The value I get with:
    Formula = "=SUM(A1" & ":A" & Rows & ")"
    I wanna put it into a textbox in my form? How do I do that?

  13. #13
    Addicted Member
    Join Date
    Aug 2001
    Location
    Minneapolis, MN
    Posts
    189
    NP:

    Code:
    Text1.Text = objExcel.Cells(Rows +1, 1)

  14. #14
    Pirre001
    Guest
    Kraig K, u are an angel, you have fix all my problems now!

    Thanks!

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