Results 1 to 3 of 3
  1. #1
    Lounger
    Join Date
    Jul 2001
    Location
    Agoura Hills, California, USA
    Posts
    28
    Thanks
    0
    Thanked 0 Times in 0 Posts

    How Do I write multiple If Not condition (Excel VBA)

    Quick one! I want to have Excel VBA check to see if there exists information in two locations, and if so, do [foo], otherwise, skip it.

    Here's what I've done so far--to give you an idea:

    If Not Sheets("DATA").Range("O1") Is Nothing And Not Sheets("DATA").Range("P1") Is Nothing Then
    If Sheets("DATA").Range("O1") <> Sheets("DATA").Range("P1") Then
    Sheets("MAIN").Range("A19") = "Date/Time Not Aligned Mac/Windows Reg."
    Else: Resume Next
    End If
    Else: Resume Next
    End If

    Basically, if O1 AND P1 exist, compare them. Otherwise, resume next. In nothing speak, it's more like If Not O1 is Nothing AND If Not P1 is Nothing, then [foo]. Of course, I can't have the two If's.

    Thanks in advance...

  2. #2
    Silver Lounger
    Join Date
    Mar 2001
    Location
    Springfield, Ohio, USA
    Posts
    2,136
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: How Do I write multiple If Not condition (Excel VBA)

    Here's a couple of ways. HTH --Sam
    <pre>Sub Foo()
    With Sheets("DATA")
    If .Range("O1") <> "" And .Range("O1") = .Range("P1") Then
    MsgBox "Foo"
    Else
    MsgBox "NoFoo"
    End If
    End With
    End Sub

    Sub foo2()
    Dim strCurrent As String
    strCurrent = ActiveSheet.Name
    Sheets("DATA").Activate
    If [O1] <> "" And [O1] = [P1] Then
    MsgBox "Foo"
    Else
    MsgBox "NoFoo"
    End If
    Sheets(strCurrent).Activate
    End Sub</pre>

    <font face="Comic Sans MS">Sam Barrett, CACI </font face=comic>
    <small>And the things that you have heard... commit these to faithful men who will be able to teach others also. 2 Timothy 2:2</small>

  3. #3
    Lounger
    Join Date
    Jul 2001
    Location
    Agoura Hills, California, USA
    Posts
    28
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: How Do I write multiple If Not condition (Excel VBA)

    Thanks, that was almost what I was after. Unfortunately, the two items I was comparing aren't necessarily the ~same~, so I had to adjust to simply:

    If .Range ("O1") <> "" And .Range ("P1") <> "" Then

    ...and that took care of it. Thanks so much though, this little thing cranks away now!

Posting Permissions

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