Consulting

Page 1 of 2 1 2 LastLast
Results 1 to 20 of 39

Thread: Solved: Find the first Row

  1. #1
    VBAX Tutor
    Joined
    Sep 2008
    Posts
    213
    Location

    Solved: Find the first Row

    I have the following code to find the last row

    [VBA] LastRow = .Cells(.Rows.Count, "G").End(xlUp).Row[/VBA]

    What would I change to get the first row?

  2. #2
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    [VBA]Dim FirstRow As Long
    With ActiveSheet.Range("B:B")
    With .Cells(1, 1)
    FirstRow = IIf(.Value = "", .End(xlDown).Row, .Row)
    End With
    End With[/VBA]

  3. #3
    VBAX Tutor david000's Avatar
    Joined
    Mar 2007
    Location
    Chicago
    Posts
    276
    Location
    [VBA]
    Sub Test()
    Dim LastRow As Range
    Dim FirstRow As Long

    Set LastRow = Range("d" & Rows.Count).End(xlUp)
    FirstRow = LastRow.End(xlUp).Row

    Range("D" & FirstRow).Select
    End Sub

    [/VBA]

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Not necessarily the first row, just the row before the first blank space above the last row.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  5. #5
    VBAX Mentor MaximS's Avatar
    Joined
    Sep 2008
    Location
    Stoke-On-Trent
    Posts
    360
    Location
    you can also try this:

    [VBA]
    Dim FirstRow As Long
    FirstRow = Cells.Find(What:="*", After:=[A1], _
    SearchOrder:=xlByRows, SearchDirection:=xlNext).Row
    [/VBA]

  6. #6
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,729
    Location
    How do you want to define "First Row"?

    For example, if there is Data in

    1. A1:A10, first row is A1
    2. A4:A10, first row is A4 (correct?)
    3. A4:A10, and A20:A30 first row is A4 (correct?)

    Paul

  7. #7
    VBAX Tutor
    Joined
    Sep 2008
    Posts
    213
    Location
    Once again I didnt explain myself very well. Paul. Choice 3 would best describe 'First row'. The First new row would start from the first empty cell after a subtotal in column L.

  8. #8
    Moderator VBAX Master georgiboy's Avatar
    Joined
    Mar 2008
    Location
    Kent, England
    Posts
    1,198
    Location
    [VBA]Sub FirstRow()

    For Each r In Range("A:A").Cells
    If r.Value <> "" Then
    r.Select
    Exit Sub
    End If
    Next

    End Sub[/VBA]
    Click here for a guide on how to add code tags
    Click here for a guide on how to mark a thread as solved
    Click here for a guide on how to upload a file with your post

    Excel 365, Version 2403, Build 17425.20146

  9. #9
    VBAX Tutor
    Joined
    Sep 2008
    Posts
    213
    Location
    Sorry Georgiboy, Im not sure how to fit that in to the following code that I currently use.
    Id like to change the I1 part, to I " Row First empty cell after previous subtotal in Column I"
    I
    [VBA]Private Sub CommandTraderTotal_Click()

    ' Subtotal Trader PL

    Dim I_LastRow As Integer

    With Sheet3

    I_LastRow = .Cells(.Rows.Count, "I").End(xlUp).Row

    .Cells(I_LastRow + 0, "L").Formula = "=SUM(I1:" & I_LastRow & ")"
    End With

    End Sub[/VBA]

  10. #10
    Moderator VBAX Master georgiboy's Avatar
    Joined
    Mar 2008
    Location
    Kent, England
    Posts
    1,198
    Location
    Not sure what you are trying to achieve here but does this shed any light

    [VBA]Private Sub CommandTraderTotal_Click()

    ' Subtotal Trader PL

    Dim I_LastRow As Integer

    For Each r In Sheet3.Range("I:I").Cells
    If r.Value <> "" Then
    I_LastRow = r.Row
    GoTo jump1
    End If
    Next

    jump1:

    Sheet3.Range("L" & I_LastRow).Value = WorksheetFunction.Sum(Range("i1:i" & I_LastRow))

    End Sub[/VBA]

    Hope this helps
    Click here for a guide on how to add code tags
    Click here for a guide on how to mark a thread as solved
    Click here for a guide on how to upload a file with your post

    Excel 365, Version 2403, Build 17425.20146

  11. #11
    VBAX Tutor
    Joined
    Sep 2008
    Posts
    213
    Location
    Ive made up a quick example of what Im trying to achieve.
    I need the range to be totaled to only start from after the previous subtotal, not from I1 every time as the code below does.
    "=SUM(I1:" & I_LastRow & ")"

  12. #12
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Just add this formula to K2 and copy down

    =IF(COUNTIF($B2:$B$2000,$B2)>1,"",SUMIF($B:$B,$B2,$J:$J))
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  13. #13
    VBAX Tutor
    Joined
    Sep 2008
    Posts
    213
    Location
    sorry XLD, Im not sure what this is supposed to do.....

  14. #14
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    It calculates subtotal values by formula. Insert it and see.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  15. #15
    Moderator VBAX Master georgiboy's Avatar
    Joined
    Mar 2008
    Location
    Kent, England
    Posts
    1,198
    Location
    Works fine for me.
    Click here for a guide on how to add code tags
    Click here for a guide on how to mark a thread as solved
    Click here for a guide on how to upload a file with your post

    Excel 365, Version 2403, Build 17425.20146

  16. #16
    VBAX Tutor
    Joined
    Sep 2008
    Posts
    213
    Location
    Ok, I figured it out. It does what I need it to do, thanks for that, but the problem with using this method is it leaves zeros in columns yet to be filled. I use a macro to cut and paste the days subtotals to a different spreadsheet, (which is why I needed to find the last row) taking all the extra zeros with it as well as leaving column K without the formulas. This would mean I have to add the formula to column K each day before I add all the totals.....

  17. #17
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    =IF(OR($B2="",COUNTIF($B2:$B$2000,$B2)>1),"",SUMIF($B:$B,$B2,$J:$J))
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  18. #18
    VBAX Tutor
    Joined
    Sep 2008
    Posts
    213
    Location
    Thanks XLD, that works better, but I still have the problem of having to add the formula to colum K after I use a macro to cut and paste it to another spreadsheet , is there anyway to imbed the formula into column K so its permanent?

  19. #19
    VBAX Tutor
    Joined
    Sep 2008
    Posts
    213
    Location
    Actually Ive tried this and it almost works. The Problem is, its using the row with the previous subtotal, not the row below it. (I know that the code is referring to the last row of the previous subtotal, I just dont know how to make it 'Last row of previous subtotal + 1 row)
    [VBA]' Subtotal Trader PL


    Dim J_LastRow As Integer
    Dim J_FirstRow As Integer

    With Sheet3

    ' Find the last row to be totaled

    J_LastRow = .Cells(.Rows.Count, "J").End(xlUp).Row

    ' Find the first row after the previous Subtotal

    J_FirstRow = .Cells(.Rows.Count, "L").End(xlUp).Row

    .Cells(J_LastRow + 0, "L").Formula = "=SUM(J" & J_FirstRow & ":J" & J_LastRow & ")"
    End With

    End Sub[/VBA]

  20. #20
    VBAX Contributor
    Joined
    Jul 2004
    Location
    Gurgaon, India
    Posts
    148
    Location
    Hi,

    Try,

    [vba]Sub SubTotalTrader()
    Dim TradAdd As String, NetAdd As String, f As String, lRow As Long
    lRow = Range("b" & Rows.Count).End(xlUp).Row
    TradAdd = Range("b2:b" & lRow).Address(ReferenceStyle:=xlR1C1)
    NetAdd = Range("j2:j" & lRow).Address(ReferenceStyle:=xlR1C1)
    f = "=if(rc[-10]<>r[1]c[-10],sumif(" & TradAdd & ",rc[-10]," & NetAdd & "),""zzz"")"
    With Range("l2:l" & lRow)
    .FormulaR1C1 = f
    .Value = .Value
    .Replace What:="zzz", Replacement:="", LookAt:=xlWhole
    End With
    End Sub[/vba]

    HTH

Posting Permissions

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