+ Reply to Thread
Results 1 to 8 of 8

Using If - Or statements

Hybrid View

  1. #1
    Registered User
    Join Date
    02-05-2010
    Location
    Canada
    MS-Off Ver
    Excel 2007
    Posts
    13

    Using If - Or statements

    I'll show you what I have that works - and what I'm trying to do that doesn't.

    This code works. It checks to see that ShortPostal Or ProtTerritory do not equal certain cells...it evaluates to False if they do not equal to the values in these cells. These are separated by an OR because if either of them doesn't meet the condition then I want to evaluate to false....

        ShortPostal = Mid(InSheet.Cells(theRow, 1).Value, 1, 6)
        ProtTerritory = InSheet.Cells(theRow, 4).Value
         
        Dim i As Integer
        Dim isGroup As Boolean
        isGroup = True
        For i = 1 To 9
            If (Mid(InSheet.Cells(theRow + i, 1).Value, 1, 6) <> ShortPostal _
            Or InSheet.Cells(theRow + i, 4).Value <> ProtTerritory) _
            Then
                isGroup = False
                Exit For
            End If
        Next i
    Now I want to check more than just these two variables....I want to check 3 more and I'm not sure how to do this....here's what I've got:


    ShortPostal = Mid(InSheet.Cells(theRow, 1).Value, 1, 6)
    ProtTerritory = InSheet.Cells(theRow, 4).Value
    SemiTerritory = InSheet.Cells(theRow, 5).Value
    UnProtTerritory = InSheet.Cells(theRow, 6).Value
    SBUZone = InSheet.Cells(theRow, 7).Value


    Dim i As Integer
    Dim isGroup As Boolean
    isGroup = True
    For i = 1 To 9
    If (Mid(InSheet.Cells(theRow + i, 1).Value, 1, 6) <> ShortPostal _
    Or InSheet.Cells(theRow + i, 4).Value <> ProtTerritory) _
    At this point, do I add the following?
    Or InSheet.Cells(theRow + i, 4).Value <> ProtTerritory) _
    Or InSheet.Cells(theRow + i, 5).Value <> SemiTerritory) _
    Or InSheet.Cells(theRow + i, 6).Value <> UnProtTerritory) _
    Or InSheet.Cells(theRow + i, 7).Value <> SBUZone) _

    Then
    isGroup = False
    Exit For
    End If
    Next i

    When I try to do the above it doesn't work - is there a smarter way to go about this? I'm very new to VBA so I'm convinced I'm missing something obvious - any help would be apprecated!

    Thanks!

    -Tara

  2. #2
    Forum Expert Domski's Avatar
    Join Date
    12-14-2009
    Location
    A galaxy far, far away
    MS-Off Ver
    Darth Office 2010
    Posts
    3,950

    Re: Using If - Or statements

    In theory what you have suggested looks fine.

    Dom
    "May the fleas of a thousand camels infest the crotch of the person who screws up your day and may their arms be too short to scratch..."

    Use code tags when posting your VBA code: [code] Your code here [/code]

    Remember, saying thanks only takes a second or two. Click the little star to give some Rep if you think an answer deserves it.

  3. #3
    Forum Expert Domski's Avatar
    Join Date
    12-14-2009
    Location
    A galaxy far, far away
    MS-Off Ver
    Darth Office 2010
    Posts
    3,950

    Re: Using If - Or statements

    If you are very new to VBA I think Mike Alexander's tutorials might be a help:

    http://www.datapigtechnologies.com/ExcelMain.htm

    Dom

  4. #4
    Registered User
    Join Date
    02-05-2010
    Location
    Canada
    MS-Off Ver
    Excel 2007
    Posts
    13

    Re: Using If - Or statements

    Quote Originally Posted by Domski View Post
    In theory what you have suggested looks fine.

    Dom
    I keep getting a Complie error: Expected: Then or GoTo

    ShortPostal = Mid(InSheet.Cells(theRow, 1).Value, 1, 6)
    ProtTerritory = InSheet.Cells(theRow, 4).Value
    SemiTerritory = InSheet.Cells(theRow, 5).Value
    UnProtTerritory = InSheet.Cells(theRow, 6).Value
    SBUZone = InSheet.Cells(theRow, 7).Value


    Dim i As Integer
    Dim isGroup As Boolean
    isGroup = True
    For i = 1 To 9
    If (Mid(InSheet.Cells(theRow + i, 1).Value, 1, 6) <> ShortPostal _
    Or InSheet.Cells(theRow + i, 4).Value <> ProtTerritory) _
    Or InSheet.Cells(theRow + i, 5).Value <> SemiTerritory) _ Right at this point
    Or InSheet.Cells(theRow + i, 6).Value <> UnProtTerritory) _
    Or InSheet.Cells(theRow + i, 7).Value <> SBUZone) _
    Exit For
    End If
    Next i

  5. #5
    Forum Expert Domski's Avatar
    Join Date
    12-14-2009
    Location
    A galaxy far, far away
    MS-Off Ver
    Darth Office 2010
    Posts
    3,950

    Re: Using If - Or statements

    Try:

    ShortPostal = Mid(InSheet.Cells(theRow, 1).Value, 1, 6)
    ProtTerritory = InSheet.Cells(theRow, 4).Value
    SemiTerritory = InSheet.Cells(theRow, 5).Value
    UnProtTerritory = InSheet.Cells(theRow, 6).Value
    SBUZone = InSheet.Cells(theRow, 7).Value
    
    Dim i As Integer
    Dim isGroup As Boolean
    isGroup = True
    For i = 1 To 9
    If Mid(InSheet.Cells(theRow + i, 1).Value, 1, 6) <> ShortPostal _
    Or InSheet.Cells(theRow + i, 4).Value <> ProtTerritory _
    Or InSheet.Cells(theRow + i, 5).Value <> SemiTerritory _
    Or InSheet.Cells(theRow + i, 6).Value <> UnProtTerritory _
    Or InSheet.Cells(theRow + i, 7).Value <> SBUZone Then
    Next i

    Try to use code tags, it makes any code you are posting much easier to read.

    Dom

  6. #6
    Registered User
    Join Date
    02-05-2010
    Location
    Canada
    MS-Off Ver
    Excel 2007
    Posts
    13

    Re: Using If - Or statements

    Sorry - I'll use the code tags...

    I accidentally left of the Then statement from the last piece of code I posted....

    Here's what I'm working with:

        ShortPostal = Mid(InSheet.Cells(theRow, 1).Value, 1, 6)
        ProtTerritory = InSheet.Cells(theRow, 4).Value
        SemiTerritory = InSheet.Cells(theRow, 5).Value
        UnProtTerritory = InSheet.Cells(theRow, 6).Value
        SBUZone = InSheet.Cells(theRow, 7).Value
        
        
        Dim i As Integer
        Dim isGroup As Boolean
        isGroup = True
                For i = 1 To 9
                    If (Mid(InSheet.Cells(theRow + i, 1).Value, 1, 6) <> ShortPostal _
                    Or InSheet.Cells(theRow + i, 4).Value <> ProtTerritory) _
                    Or InSheet.Cells(theRow + i, 5).Value <> SemiTerritory) _
                    Or InSheet.Cells(theRow + i, 6).Value <> UnProtTerritory) _
                    Or InSheet.Cells(theRow + i, 7).Value <> SBUZone) _
                    Then isGroup = False
                    End If
                    Exit For
        Next i
    Or InSheet.Cells(theRow + i, 5).Value <> SemiTerritory) _Compile error: Expected: Then or GoTo

  7. #7
    Forum Expert Domski's Avatar
    Join Date
    12-14-2009
    Location
    A galaxy far, far away
    MS-Off Ver
    Darth Office 2010
    Posts
    3,950

    Re: Using If - Or statements

    Only on my mobile but if you pop IsGroup=False after the Then in the code I posted last it should work I think.

  8. #8
    Registered User
    Join Date
    02-05-2010
    Location
    Canada
    MS-Off Ver
    Excel 2007
    Posts
    13

    Re: Using If - Or statements

    Quote Originally Posted by Domski View Post
    Only on my mobile but if you pop IsGroup=False after the Then in the code I posted last it should work I think.
    That looks like it did it! Thank you!!!

+ 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