Page 1 of 2 12 LastLast
Results 1 to 15 of 21
  1. #1
    DMT Dave is offline VIP
    Windows 10 Access 2016
    Join Date
    May 2018
    Posts
    1,329

    Retrieve data from last row of specific Columns

    Hi Guy's, I am fairly close with this one and require you great guy's to correct me



    I am trying open an excel file on sheet 3 and retrieve data from the last row on Columns A, B and F

    all looks ok to me apart from:

    I can't seem to open the file on sheet 3

    therefore strings strColA, strColB and strColF are not returning the correct data

    Have i got this correct apart from not opening on Sheet 3 ?

    Code:
        Dim xlApp As Object    Dim xlWbk As Object
        Dim xlWsh As Object
        Dim strPath As String, strFile As String, strColA As String, strColB As String, strColF As String
        Dim iSheet As Integer, i As Integer, intLR As Integer, iRow As Integer
        Dim varResult As Variant
        
        iSheet = "3"
           
        strPath = "T:\DMT Ltd\Fuel Price Sheets\"
        strFile = "Fuel Prices.xlsx"
    
    
        Set xlApp = CreateObject("Excel.Application")
        Set xlWbk = xlApp.Workbooks.Open(strPath & strFile)
        Set xlWsh = xlWbk.Worksheets(iSheet)
        xlApp.Visible = True
        
        With xlWsh
        
        intLR = .Cells(.Rows.Count, 1).End(xlUp).Row
            For i = 1 To intLR
                
                strColF = xlWsh.Range("F" & i).Value
                strColA = xlWsh.Range("A" & i).Value
                strColB = xlWsh.Range("B" & i).Value
            
            Exit For
            
            Next i
            
        End With
        
        Debug.Print strColA
        Debug.Print strColB
        Debug.Print strColF
        
            MsgBox ("the data on last rows of Column A, B and F is:" & vbCrLf & vbCrLf & _
                strColA & vbCrLf & _
                strColB & vbCrLf & _
                strColF)
        
        'Application.FollowHyperlink strPath & strFile
        
        Set xlWsh = Nothing
        Set xlWbk = Nothing
        Set xlApp = Nothing

  2. #2
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,644
    It is not necessary to set focus to specific sheet in order to pull its data. I tested your code and it works. However, it is not pulling data from last row, it is first row. Why do you have Exit For in the loop? That causes exit in the first iteration. If you want only the last row data, there is no need to loop.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    DMT Dave is offline VIP
    Windows 10 Access 2016
    Join Date
    May 2018
    Posts
    1,329
    Ahh June7, of course, i have just read the MsgBox and it is returning data from Row A

    So do i need to remove any for or looping statements ?

  4. #4
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,644
    You can remove the Exit For line. Again, if you only want last row, don't need loop - remove For and Next lines. Just reference the last row with intLR instead of i variable.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  5. #5
    DMT Dave is offline VIP
    Windows 10 Access 2016
    Join Date
    May 2018
    Posts
    1,329
    June7, perfect thank you so much working correctly

    Code:
        Dim xlApp As Object    Dim xlWbk As Object
        Dim xlWsh As Object
        Dim strPath As String, strFile As String, strColA As String, strColB As String, strColF As String
        Dim iSheet As Integer, i As Integer, intLR As Integer, iRow As Integer
        Dim varResult As Variant
        
        iSheet = "3"
           
        strPath = "T:\DMT Ltd\Fuel Price Sheets\"
        strFile = "Fuel Prices.xlsx"
    
    
        Set xlApp = CreateObject("Excel.Application")
        Set xlWbk = xlApp.Workbooks.Open(strPath & strFile)
        Set xlWsh = xlWbk.Worksheets(iSheet)
        xlApp.Visible = False
        
        With xlWsh
        
        intLR = .Cells(.Rows.Count, 1).End(xlUp).Row
            
            strColF = xlWsh.Range("F" & intLR).Value
            strColA = xlWsh.Range("A" & intLR).Value
            strColB = xlWsh.Range("B" & intLR).Value
            
        End With
        
        Debug.Print strColA
        Debug.Print strColB
        Debug.Print strColF
        
            MsgBox ("the data on last rows of Column A, B and F is:" & vbCrLf & vbCrLf & _
                strColA & vbCrLf & _
                strColB & vbCrLf & _
                strColF)
        
        Set xlWsh = Nothing
        Set xlWbk = Nothing
        Set xlApp = Nothing

  6. #6
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,644
    Since you are using With xlWsh, there is no need to repeat xlWsh within that code block.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  7. #7
    DMT Dave is offline VIP
    Windows 10 Access 2016
    Join Date
    May 2018
    Posts
    1,329
    I am now trying to workout how i can add to this to retrieve the last 12 rows used, is that possible ?

  8. #8
    DMT Dave is offline VIP
    Windows 10 Access 2016
    Join Date
    May 2018
    Posts
    1,329
    would i use something like:

    Code:
            For i = -12 To intLR        strColF = xlWsh.Range("F" & i).Value
            strColA = xlWsh.Range("A" & i).Value
            strColB = xlWsh.Range("B" & i).Value
            
            Next i
    Or use to step - 12 ?

  9. #9
    DMT Dave is offline VIP
    Windows 10 Access 2016
    Join Date
    May 2018
    Posts
    1,329
    Hi June7, now cleared xlWsh from within code block

    I i wanted to get data for last 12 rows, i have tried adding a loop back in but not returning data

    Code:
        With xlWsh    
        intLR = .Cells(.Rows.Count, 1).End(xlUp).Row
        
        For intLR = 1 To -12
            
            strColF = .Range("F" & intLR).Value
            strColA = .Range("A" & intLR).Value
            strColB = .Range("B" & intLR).Value
        
        Next intLR
        
        End With

  10. #10
    Minty is offline VIP
    Windows 10 Office 365
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,144
    A number of things, although Excel probably doesn't care
    Sheet(x)
    would expect x to be a number not a string.

    Your last bit of code is constantly rewriting the variables with the new values, so
    strColF (and the others) will, in very quick succession, have the value of the last cell, the one above it, the one above that etc. etc.

    You're not doing anything with the value each time other than overwriting it.
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

  11. #11
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,644
    What do you want do with retrieved data? As Minty noted, code only outputs the last data read within the loop. If you want to do something (Debug.Print, save to somewhere else, etc) with each row of data, that needs to be within the loop.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  12. #12
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,235
    Dave,
    your loop should be something like this:
    Code:
     With xlWsh    
        intLR = .Cells(.Rows.Count, 1).End(xlUp).Row
        Dim i as integer
        For i = 0 to 11
            
            strColF = .Range("F" & intLR-i).Value
            strColA = .Range("A" & intLR-i).Value
            strColB = .Range("B" & intLR-i).Value
        
        Next i    
    End With
    As Minty said, you are not doing anything with the strCol variables so you might want to declare them as string arrays and load them in the For loop:
    Code:
    Dim strColF() as string
    Dim strColA() as string
    Dim strColB() as string
    
    
     With xlWsh    
        intLR = .Cells(.Rows.Count, 1).End(xlUp).Row
        Dim i as integer
        For i = 0 to 11
            
            strColF(i) = .Range("F" & intLR-i).Value
            strColA(i) = .Range("A" & intLR-i).Value
            strColB(i) = .Range("B" & intLR-i).Value
        
        Next i    
    End With
    Cheers,
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  13. #13
    DMT Dave is offline VIP
    Windows 10 Access 2016
    Join Date
    May 2018
    Posts
    1,329
    Thanks Vlad, OK so declare the arrays and add the For within the arrays for each strCOL

    I can see what you have done

    thank you

  14. #14
    DMT Dave is offline VIP
    Windows 10 Access 2016
    Join Date
    May 2018
    Posts
    1,329
    Hi Guys, im back to this and trying for life of me to work out how to add differences between records and edit the set and add to Difference field

    I know im doing the wrong thing by using DAvg as all field will populate with an average in all records

    How do I add something in that will add difference between each record ?

    This should be something i can do easily but tried a few different things without success!!!

    This is on my "Add Calculations Button"

    Code:
    Dim rs As dao.Recordset    Dim curRate As Currency, curDiff As Currency, curFuelPrice As Currency, curAvg As Currency, curMax As Currency
        Dim i As Integer
        
        
            Set rs = CurrentDb.OpenRecordset("Select * From tblFuelPrices")
                
                With rs
                    
                Do While Not rs.EOF
                
                    curAvg = DAvg("FuelPrice", "tblFuelPrices")
                        
                    rs.Edit
                    !Difference = curAvg
                    .Update
                    rs.MoveNext
                
                Loop
                
                End With
    
    
    Set rs = Nothing
    Click image for larger version. 
    
    Name:	Capture.JPG 
    Views:	12 
    Size:	110.8 KB 
    ID:	49323

  15. #15
    Minty is offline VIP
    Windows 10 Office 365
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,144
    Step through the process, or create the calculation in Excel one at a time.
    Then apply the same logic to your access calculations.

    Curr average is fixed so move it out out the loop.

    The difference is going to be rs.FuelPrice - CurrAvg
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 1
    Last Post: 10-12-2020, 04:19 AM
  2. Replies: 2
    Last Post: 02-13-2017, 02:40 PM
  3. Replies: 3
    Last Post: 08-18-2012, 03:25 AM
  4. Replies: 1
    Last Post: 06-15-2012, 05:51 PM
  5. Replies: 2
    Last Post: 05-17-2012, 03:52 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums