Consulting

Results 1 to 7 of 7

Thread: Solved: Not sure how to fix this loop? Help Please

  1. #1

    Solved: Not sure how to fix this loop? Help Please

    Can someone help? I want to check 5 cells whether they're blank or not.
    They're (4,3) (6,3) (8,3) and (4,7) (6,7)

    The probelm is after row 6, there is only one cell left to check, which is (8,3). However, the codes below will check (8,7) and definately throw out an error message. Is there a way to fix the codes and make Excel to bypass (8,7)? I'm new to this so my codes might not be the perfect way to do what I want.

    Row = 4
    For I = 1 To 3
    If (IsEmpty(Cells(Row, 3))) Or (IsEmpty(Cells(Row, 7))) Then
    MsgBox ("No Blank")
    Exit Sub
    Else
    Row = Row + 2
    End If
    Next I

  2. #2
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    Maybe:

    Sub Exa1()
    Dim Row As Long, i As Long
        
        For Row = 4 To 8 Step 2
            If Not Row = 8 And (IsEmpty(Cells(Row, 3)) Or IsEmpty(Cells(Row, 7))) Then
                MsgBox "no blank"
                Exit Sub
            ElseIf Row = 8 And IsEmpty(Cells(Row, 3)) Then
                MsgBox "no blank"
                Exit Sub
            End If
        Next
    End Sub

  3. #3
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,876
    alternatively[vba]Sub blah()
    For Each cll In Range("C4,C6,C8,G4,G6").Cells
    If IsEmpty(cll) Then
    MsgBox ("No Blank")
    Exit Sub
    End If
    Next cll
    'MsgBox "ok"
    End Sub
    [/vba] or a bit shorter:[vba]For Each cll In Range("C4,C6,C8,G4,G6").Cells
    If IsEmpty(cll) Then MsgBox ("No Blank"): Exit Sub
    Next cll
    [/vba]
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  4. #4
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    Quote Originally Posted by p45cal
    alternatively
    ...and nicer

  5. #5
    Thanks a lot p45. It works like a charm.

    By the way, if the user instead hits the spacebar to delete the cell, this script won't be able to throw out the error because of the empty space in the blank cell. What can I do to make the VBA codes to detect this?Thanks again

  6. #6
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,876
    [VBA]For Each cll In Range("C4,C6,C8,G4,G6").Cells
    If Trim(cll.Value) = "" Then MsgBox ("No Blank"): Exit Sub
    Next cll
    [/VBA]
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  7. #7
    thanks big guy

Posting Permissions

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