+ Reply to Thread
Results 1 to 4 of 4

Isempty with If function in VBA

Hybrid View

  1. #1
    Registered User
    Join Date
    06-21-2012
    Location
    Chennai
    MS-Off Ver
    Excel 2007
    Posts
    81

    Isempty with If function in VBA

    Hello Experts,

    Need your help in understanding why i am failing in the below VBA code

    What i want results is if B15 is not empty and one of the cell in Range(C15:i15) is empty, give the error - code is working fine in the first if condition

    If B15 is empty and one of the cell in C15:I15 is not empty, give the error - Not working

    Kndly help me out on this.

    Sub Macro4()
    
    'Giving results what i want
    If IsEmpty(Range("B15").Value) = False And IsEmpty(Range("C15").Value) = True Or IsEmpty(Range("D15").Value) = True Or IsEmpty(Range("E15").Value) = True Or IsEmpty(Range("F15").Value) = True Or IsEmpty(Range("G15").Value) = True Or IsEmpty(Range("H15").Value) = True Or IsEmpty(Range("I15").Value) = True Then
    
    MsgBox ("Error")
    
    Else
    MsgBox ("No error")
    
    End If
    
    'Not giving results what i want
    
    If IsEmpty(Range("C15").Value) = False Or IsEmpty(Range("D15").Value) = False Or IsEmpty(Range("E15").Value) = False Or IsEmpty(Range("F15").Value) = False Or IsEmpty(Range("G15").Value) = False Or IsEmpty(Range("H15").Value) = False Or IsEmpty(Range("I15").Value) = False And IsEmpty(Range("B15").Value) = True Then
    
    MsgBox ("Error")
    
    Else
    MsgBox ("No error")
    
    End If
    
    End Sub

  2. #2
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Isempty with If function in VBA

    For 1st part
    If you want to test for not empty

    If Not IsEmpty(Range("B15").Value) Then

    Sub Macro4()
    
    'Giving results what i want
    If IsEmpty(Range("B15").Value) Then
        If IsEmpty(Range("C15").Value) Or IsEmpty(Range("D15").Value) _
        Or IsEmpty(Range("E15").Value) Or IsEmpty(Range("F15").Value) _
        Or IsEmpty(Range("G15").Value) Or IsEmpty(Range("H15").Value) _
        Or IsEmpty(Range("I15").Value) Then
    
            MsgBox ("Error")
        
        Else
            MsgBox ("No error")
        
        End If
    End If
    'Not giving results what i want
    
    'If IsEmpty(Range("C15").Value) = False Or IsEmpty(Range("D15").Value) = False Or IsEmpty(Range("E15").Value) = False Or IsEmpty(Range("F15").Value) = False Or IsEmpty(Range("G15").Value) = False Or IsEmpty(Range("H15").Value) = False Or IsEmpty(Range("I15").Value) = False And IsEmpty(Range("B15").Value) = True Then
    '
    'MsgBox ("Error")
    '
    'Else
    'MsgBox ("No error")
    '
    'End If
    
    End Sub
    Another way of writing:
    Option Explicit

    Sub Macro4()
    
        'Giving results what i want
        If Not IsEmpty(Range("B15").Value) Then
            If IsEmpty(Range("C15").Value) Or IsEmpty(Range("D15").Value) _
            Or IsEmpty(Range("E15").Value) Or IsEmpty(Range("F15").Value) _
            Or IsEmpty(Range("G15").Value) Or IsEmpty(Range("H15").Value) _
            Or IsEmpty(Range("I15").Value) Then
        
                MsgBox ("Error")
            
            End If
        
        ElseIf IsEmpty(Range("B15").Value) Then
            If Not IsEmpty(Range("C15").Value) Or Not IsEmpty(Range("D15").Value) _
            Or Not IsEmpty(Range("E15").Value) Or Not IsEmpty(Range("F15").Value) _
            Or Not IsEmpty(Range("G15").Value) Or Not IsEmpty(Range("H15").Value) _
            Or Not IsEmpty(Range("I15").Value) Then
            MsgBox ("Error")
        
        Else
            MsgBox ("No error")
        
            End If
        End If
    End Sub
    Last edited by AB33; 11-27-2016 at 12:02 PM.

  3. #3
    Forum Expert gmr4evr1's Avatar
    Join Date
    11-24-2014
    Location
    Texas
    MS-Off Ver
    Office 2010 and 2007
    Posts
    3,448

    Re: Isempty with If function in VBA

    In the 2nd part of the code, you didn't have B15. Try this...
    Sub Macro4()
    
    'Giving results what i want
    If IsEmpty(Range("B15").Value) = False And IsEmpty(Range("C15").Value) = True Or IsEmpty(Range("D15").Value) = True Or IsEmpty(Range("E15").Value) = True Or IsEmpty(Range("F15").Value) = True Or IsEmpty(Range("G15").Value) = True Or IsEmpty(Range("H15").Value) = True Or IsEmpty(Range("I15").Value) = True Then
    
    MsgBox ("Error")
    
    Else
    MsgBox ("No error")
    
    End If
    
    'Not giving results what i want
    
    If IsEmpty(Range("B15").Value) = True And IsEmpty(Range("C15").Value) = False Or IsEmpty(Range("D15").Value) = False Or IsEmpty(Range("E15").Value) = False Or IsEmpty(Range("F15").Value) = False Or IsEmpty(Range("G15").Value) = False Or IsEmpty(Range("H15").Value) = False Or IsEmpty(Range("I15").Value) = False And IsEmpty(Range("B15").Value) = True Then
    
    MsgBox ("Error")
    
    Else
    MsgBox ("No error")
    
    End If
    
    End Sub
    1N73LL1G3NC3 15 7H3 4B1L17Y 70 4D4P7 70 CH4NG3 - 573PH3N H4WK1NG
    You don't have to add Rep if I have helped you out (but it would be nice), but please mark the thread as SOLVED if your issue is resolved.

    Tom

  4. #4
    Registered User
    Join Date
    06-21-2012
    Location
    Chennai
    MS-Off Ver
    Excel 2007
    Posts
    81

    Re: Isempty with If function in VBA

    That was so quick help. will let you know the results in some time
    thanks a lot

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] How to use IsEMPTY in for-next loop?
    By GIS2013 in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 09-02-2013, 07:30 AM
  2. Not isempty()
    By nikko50 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 05-08-2013, 01:19 PM
  3. [SOLVED] Do Until IsEmpty issue
    By Turtleman10 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 10-29-2012, 01:53 PM
  4. isempty loop
    By comp23 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 07-10-2012, 03:32 PM
  5. Do Until Cell IsEmpty
    By fa888 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 10-19-2010, 11:29 PM
  6. IsEmpty problems
    By papermoon in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-11-2010, 11:14 AM
  7. IsEmpty
    By Determined in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 02-05-2007, 07:44 PM
  8. Can you do IsEmpty(Range)?
    By Sharkbait in forum Excel General
    Replies: 6
    Last Post: 08-08-2006, 05:45 PM

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