Consulting

Results 1 to 10 of 10

Thread: Solved: Row question

  1. #1
    VBAX Tutor
    Joined
    Feb 2006
    Posts
    295
    Location

    Solved: Row question

    hi,

    i have data that begins in A5. so i would use the following and the macro will stop when it reaches a blank row in column A.

    [VBA]For I = 5 To 9000
    If Range("A" & I).FormulaR1C1 = "" Then
    Range("A5").Select
    MsgBox "COMPLETED"
    Exit Sub
    End If[/VBA]

    the problem i have is that sometimes the data can begin anywhere in column A, beginning with row 5 and downward, 9000 being the max.

    how can i modify my coding to run when it sees the first row populated and then stop with the next blank?

    thanks

  2. #2
    Moderator VBAX Master austenr's Avatar
    Joined
    Sep 2004
    Location
    Maine
    Posts
    2,033
    Location
    Peace of mind is found in some of the strangest places.

  3. #3
    VBAX Regular
    Joined
    Mar 2005
    Location
    Helena, MT
    Posts
    90
    Location
    Try something like this
    Sub TestMe()
    Dim firstRow As Integer
    firstRow = Range("A5").End(xlDown).Row
    For I = firstRow To 9000
    If Range("A" & I).FormulaR1C1 = "" Then
        Cells(firstRow, 1).Select
        MsgBox "COMPLETED"
        Exit Sub
    End If
    Next I
    End Sub
    lenze

  4. #4
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    or maybe
    [VBA]
    Sub TestMe()
    Dim i As Long
    For i = 5 To 9000
    If Cells(i, 1) <> "" Then
    'Do something
    Cells(i, 3).Interior.ColorIndex = 6
    Else
    MsgBox "COMPLETED"
    Exit Sub
    End If
    Next i
    End Sub

    [/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'

  5. #5
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    How about just ..

    [vba]Range("A5").End(xldown).Offset(1).Select[/vba]

    ?? Maybe you don't need the Select, but that will give you the first blank after A5; unless A5 is the only value in that column, it will give you the last row in column A. It's faster than a loop for sure.

    For ALL cells with a value in that column starting from row 5 and going down ...

    [vba]Range("A5", Cells(Rows.Count, 1).end(xlup)).Select[/vba]

    HTH

  6. #6
    Hi

    Perhaps
    [VBA]
    Sub ccc()
    If IsEmpty(Range("A5")) Then
    firstrow = Range("A4").End(xlDown).Row
    Else
    firstrow = 5
    End If
    Range("a" & firstrow & ":A" & Cells(Rows.Count, 1).End(xlUp).Row).Select

    End Sub
    [/VBA]

    Covers the situation where row 5 has data, but then again, does not. Selects the entire range in column A with data.


    Tony

  7. #7
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Don't you love the questions that have such disparate solutions?
    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'

  8. #8
    VBAX Regular
    Joined
    Mar 2005
    Location
    Helena, MT
    Posts
    90
    Location
    Quote Originally Posted by mdmackillop
    Don't you love the questions that have such disparate solutions?
    Yes, but what happened to
    vzachin

    lenze

  9. #9
    Administrator
    Chat VP VBAX Guru johnske's Avatar
    Joined
    Jul 2004
    Location
    Townsville, Australia
    Posts
    2,872
    Location
    Quote Originally Posted by mdmackillop
    Don't you love the questions that have such disparate solutions?
    And here's another
    [vba]Option Explicit

    Sub trythis()

    Dim Cell As Range

    For Each Cell In Range("A5:A9000")
    If Not Cell.HasFormula Then
    Range("A5").Select
    MsgBox "COMPLETED"
    Exit For
    End If
    Next

    End Sub[/vba]Where I assume that, by using FormulaR1C1 in the OPs code, they're maybe looking for cells without data generated by w/s formulas...
    You know you're really in trouble when the light at the end of the tunnel turns out to be the headlight of a train hurtling towards you

    The major part of getting the right answer lies in asking the right question...


    Made your code more readable, use VBA tags (this automatically inserts [vba] at the start of your code, and [/vba ] at the end of your code) | Help those helping you by marking your thread solved when it is.

  10. #10
    VBAX Tutor
    Joined
    Feb 2006
    Posts
    295
    Location
    sorry for being missing in action. the post from lenze is what i am looking for. thanks for all the wonderful help

Posting Permissions

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