+ Reply to Thread
Results 1 to 3 of 3

if conditions meet a certain criteria...

Hybrid View

  1. #1
    Registered User
    Join Date
    02-04-2012
    Location
    Philippines
    MS-Off Ver
    Excel 2007
    Posts
    40

    if conditions meet a certain criteria...

    i have this code here, my problem is if the condition meet then run my macro 1


    Private Sub Worksheet_Change(ByVal Target As Range)
    
    For i = 1 To 8
        Cells(i, "H1").Text = "Each"
        If Cells(i, "H1").VFalue = "Each" Then
            Run Macro1
        Else
        
          Next i
        
        End If
    
    
    End Sub
    example is if(or(A1="pie",B1="american") then execute Macro 1
    else if not meet
    goto A2 and B2.
    until the end of the row.

    Thanks.

  2. #2
    Forum Expert
    Join Date
    07-20-2011
    Location
    Mysore, India.
    MS-Off Ver
    Excel 2019
    Posts
    8,634

    Re: if conditions meet a certain criteria...

    Below is the code.Try it

    Private Sub Worksheet_Change(ByVal Target As Range)
    
    For i = 1 To 8
       IF Cells(i, "A")= "pie" and Cells(i, "B")= "american" Then
            Run "Macro1"
       End If
    
    Next i
        
    End Sub

  3. #3
    Valued Forum Contributor smuzoen's Avatar
    Join Date
    10-28-2011
    Location
    Brisbane, Australia
    MS-Off Ver
    Excel 2003/2007/2010
    Posts
    610

    Re: if conditions meet a certain criteria...

    I am not sure what you are trying to achieve however I am not sure worksheet change event is what you want - it is essentially to detect a change in a worksheet and if condition is met then do something. For example
    Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Intersect(Target, Range("A1:H1")) Is Nothing Then
    Select Case Target.Value
    Case Is = "pie"
    Call macro1
    Case Is = "american"
    Call macro2
    Case Else
    MsgBox "Not found"
    End Select
    End If
    End Sub
    
    Sub macro1()
    MsgBox "Macro1"
    End Sub
    Sub macro2()
    MsgBox "Macro2"
    End Sub
    So if you enter "american" anywhere in range A1 to H1 then macro2 is run and if you enter "pie" anywhere from A1 to H1 in the worksheet then macro1 is called.
    What you want to do is check A1 and B1 for the words "american" or "pie" and if so run macro1 if not then check A2 and B2 and so on till you get to the end of the row. That is more suited to a module rather than a worksheet change event. For example
    Sub test()
    For k = 1 To Cells(Rows.Count, "A").End(xlUp).Row
    If Range("A" & k) = "pie" Or Range("B" & k) = "american" Then
    Call macro1
    Exit For ' will stop execution as have found a value
    End If
    Next
    End Sub
    Sub macro1()
    MsgBox "Macro1"
    End Sub
    I would suggest you upload a sample workbook as your question is ambiguous - in your code you are looking for the word "each" however you go on to talk about different strings in the thread. I would suggest you are a little more specific and give a sample workbook showing how your workbook is set up and what you want to happen when certain conditions are met.
    Hope this helps.
    Anthony
    Pack my box with five dozen liquor jugs
    PS: Remember to mark your questions as Solved once you are satisfied. Please rate the answer(s) by selecting the Star in the lower left next to the Triangle. It is appreciated?

+ 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