Consulting

Results 1 to 3 of 3

Thread: How to find the Last Used Row on Each Sheet

  1. #1

    How to find the Last Used Row on Each Sheet

    In this loop, I need to change the focus from the ActiveSheet to the rest of the sheets in the workbook.
    I refer to the current sheet, the activesheet w/ the variable oWst.
    lLrws is the last row variable, LR(oWst, "A") is the function to find the last row.
    Can I still use the function, or do I need to use a variable instead?

    [VBA]
    Function LR(Ws As Worksheet, Col As Variant) As Long
    Application.Volatile
    If Not IsNumeric(Col) Then Col = Columns(Col).Column()
    LR = Ws.Cells(Rows.Count, Col).End(xlUp).Row
    End Function
    [/VBA]

    [VBA]
    Set oWst = ActiveSheet

    For Each Ws In Workbooks("TGSProductsAttrib.xls").Worksheets
    lLrws = LR(oWst, "A")
    For i = 2 To lLrws
    If Not IsEmpty(Cells(i, "D").Value) Then
    'If IsEmpty(Cells(i, "G").Value) And Not IsEmpty(Cells(i, "D").Value) Then 'Or Not IsEmpty(Cells(i, "E").Value) Or Not IsEmpty(Cells(i, "F").Value) Then
    Cells(i, "G").Value = Range("D1").Value & Cells(i, "D").Value & "; "
    End If

    If Not IsEmpty(Cells(i, "E").Value) Then
    Cells(i, "G").Value = Cells(i, "G").Value & Range("E1").Value & Cells(i, "E").Value & "; "
    End If

    If Not IsEmpty(Cells(i, "F").Value) Then
    Cells(i, "G").Value = Cells(i, "G").Value & Range("F1").Value & Cells(i, "F").Value & "; "
    End If
    Next i
    i = 2
    Columns("A:G").AutoFit
    Next Ws
    [/VBA]
    my site: www.ecboardco.com
    was built w/ a majority of the assistance from the board members here... thanks VBAX.

    Just because I see something, doesn't mean that what's actually happening is what I see.

    You don't get from 0-90 by standing still!

  2. #2
    And- even if I change the Function to
    lLrwt = Ws.cells(rows.count, "A").end(xlup).row, which does find the last row, changes are only happening on the ActiveSheet.
    So I suppose my next ? is how do I tell the the code to activate the next sheet in the For...Each so the code performs the instructions on that next sheet?
    my site: www.ecboardco.com
    was built w/ a majority of the assistance from the board members here... thanks VBAX.

    Just because I see something, doesn't mean that what's actually happening is what I see.

    You don't get from 0-90 by standing still!

  3. #3
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Hi Doug,
    You can simply pass WS to the function to get the last row in column A for each sheet, but you need to carefully specify the sheet where you are reading writing your data.
    [VBA] For Each Ws In Workbooks("TGSProductsAttrib.xls").Worksheets
    lLrws = LR(ws, "A") [/VBA]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •