If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > PC based Database Applications > Microsoft Excel > if statement with or doesnt work

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 09-08-10, 10:22
mikezx10 mikezx10 is offline
Registered User
 
Join Date: Oct 2003
Posts: 226
if statement with or doesnt work

I would like to clear the contents of data entry cells these cells would not have "+" or "=sum" in the formula. In my if statement i tired using or, but it is not working:

If Left(ActiveCell.Formula, 4) <> "=Sum" Or InStr(ActiveCell.Formula, "+") = 0 Then
Selection.ClearContents
End if
Reply With Quote
  #2 (permalink)  
Old 09-08-10, 10:39
Colin Legg Colin Legg is offline
Registered User
 
Join Date: Sep 2008
Location: London, UK
Posts: 495
Hi,

I think it should be a logical AND, not OR.

If the cell does not contain "+" and it does not start with "=SUM" then clear the contents.

Code:
Sub foo()
    Dim rngCell As Range
 
    For Each rngCell In Range("A1:A10").Cells
 
        'make sure the cell has a formula
        If rngCell.HasFormula Then
 
            'make sure it does not contain a +
            If VBA.InStr$(1, rngCell.Formula, "+", vbTextCompare) = 0 Then
 
                'make sure it does not begin with a SUM function
                If VBA.Left$(rngCell.Formula, 4) <> "=SUM" Then
 
                    rngCell.ClearContents
 
                End If
 
            End If
 
        End If
 
    Next rngCell
 
End Sub
Hope that helps...
__________________
Colin

RAD Excel Blog

Other tutorials:
Array Formulas | Deleting Rows with VBA
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On