+ Reply to Thread
Results 1 to 3 of 3

Do Until Loop Dilemma

  1. #1
    mellowe
    Guest

    Do Until Loop Dilemma

    Hi All!

    I have some code that will loop through a range of cells in column "F"
    to find blank cells and if there are any to populate that cell with a
    "0" value which it does BUT.. I am not sure how to add into my code
    that I want the loop only to continue as long as the cell value in col.
    "A" is not blank. At the moment the code will loop continuously until
    all blank cells in col "F" are populated with a "0" which takes ages!!

    Please help as I am unsure of the code needed here!! Thanks!!

    Dim StartRow1 As Long
    Dim myRng As Range
    Dim FoundCell As Range
    Dim WhatToFind As String
    WhatToFind = ""

    With ActiveSheet
    StartRow1 = 2
    Set myRng = .Range("F:F")

    With myRng
    Do

    Set FoundCell = .Cells.Find(What:=WhatToFind, _
    After:=.Cells(.Cells.Count), LookIn:=xlFormulas, _
    LookAt:=xlWhole, SearchOrder:=xlByRows, _
    SearchDirection:=xlNext, MatchCase:=False)

    If FoundCell Is Nothing Then
    Exit Do 'done
    End If

    FoundCell = "0"

    Loop
    End With
    End With


  2. #2
    Dave Peterson
    Guest

    Re: Do Until Loop Dilemma

    You can use select a range and do Edit|goto|special|blanks to get that range of
    empty cells.

    In code it could look like:

    Option Explicit
    Sub testme()
    Dim myRng As Range
    Dim wks As Worksheet

    Set wks = ActiveSheet
    With wks
    'using column A to get the last used cell
    Set myRng = Nothing
    On Error Resume Next
    Set myRng = .Range("F1:F" & .Cells(.Rows.Count, "A").End(xlUp).Row) _
    .Cells.SpecialCells(xlCellTypeBlanks)
    On Error GoTo 0

    If myRng Is Nothing Then
    MsgBox "No empty cells in column F!"
    Exit Sub
    End If

    myRng.Value = 0
    End With

    End Sub

    Or if you only wanted the blank cells in column F that had something in column
    A:

    Option Explicit
    Sub testme2()
    Dim myRng As Range
    Dim wks As Worksheet
    Dim myCell As Range

    Set wks = ActiveSheet
    With wks
    'using column A to get the last used cell
    Set myRng = Nothing
    On Error Resume Next
    Set myRng = .Range("F1:F" & .Cells(.Rows.Count, "A").End(xlUp).Row) _
    .Cells.SpecialCells(xlCellTypeBlanks)
    On Error GoTo 0

    If myRng Is Nothing Then
    MsgBox "No empty cells in column F!"
    Exit Sub
    End If

    For Each myCell In myRng.Cells
    If IsEmpty(.Cells(myCell.Row, "A").Value) Then
    'do nothing
    Else
    myCell.Value = 0
    End If
    Next myCell
    End With

    End Sub

    (I was kind of confused about which you wanted.)


    mellowe wrote:
    >
    > Hi All!
    >
    > I have some code that will loop through a range of cells in column "F"
    > to find blank cells and if there are any to populate that cell with a
    > "0" value which it does BUT.. I am not sure how to add into my code
    > that I want the loop only to continue as long as the cell value in col.
    > "A" is not blank. At the moment the code will loop continuously until
    > all blank cells in col "F" are populated with a "0" which takes ages!!
    >
    > Please help as I am unsure of the code needed here!! Thanks!!
    >
    > Dim StartRow1 As Long
    > Dim myRng As Range
    > Dim FoundCell As Range
    > Dim WhatToFind As String
    > WhatToFind = ""
    >
    > With ActiveSheet
    > StartRow1 = 2
    > Set myRng = .Range("F:F")
    >
    > With myRng
    > Do
    >
    > Set FoundCell = .Cells.Find(What:=WhatToFind, _
    > After:=.Cells(.Cells.Count), LookIn:=xlFormulas, _
    > LookAt:=xlWhole, SearchOrder:=xlByRows, _
    > SearchDirection:=xlNext, MatchCase:=False)
    >
    > If FoundCell Is Nothing Then
    > Exit Do 'done
    > End If
    >
    > FoundCell = "0"
    >
    > Loop
    > End With
    > End With


    --

    Dave Peterson

  3. #3
    mellowe
    Guest

    Re: Do Until Loop Dilemma

    Brilliant!! Thanks Dave wanted the blank cells in column F that had
    something in column
    A so the latter code worked perfect!


+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1