+ Reply to Thread
Results 1 to 21 of 21

Add an 'x' to column U if

Hybrid View

  1. #1
    Registered User
    Join Date
    08-19-2011
    Location
    UK
    MS-Off Ver
    2016
    Posts
    45

    Add an 'x' to column U if

    Hello,

    After a fair bit of searching i'm unable to find / alter a suitable macro.

    Aim:
    - Look at A2:O2 and if 'this word or words' AND 'this word or words' appear in any cell then add an 'x' to U2.

    Misc:
    - spread sheet has thousands of rows
    - there will be a number of terms to search for. Such as ('hello' AND 'goodbye', 'red' AND 'blue, 'blue' AND 'water')

    Would much prefer a macro as there are already a number of formulas involved.

    Many thanks in advance
    Last edited by Greenal; 09-29-2011 at 07:12 AM.

  2. #2
    Forum Expert Bob Phillips's Avatar
    Join Date
    09-03-2005
    Location
    Wessex
    MS-Off Ver
    Office 2003, 2010, 2013, 2016, 365
    Posts
    3,284

    Re: Add an 'x' to column U if

    Public Sub CountWords()
    Const FORMULA_FIND As String = _
        "=SUMPRODUCT(--(ISNUMBER(FIND(""<word>"",A<row>:O<row>))))"
    Const LOOK_FOR_1 As String = "hello"
    Const LOOK_FOR_2 As String = "goodbye"
    Dim Lastrow As Long
    Dim i As Long
    Dim total As Long
    
        Application.ScreenUpdating = False
        
        With ActiveSheet
    
            For i = 1 To .UsedRange.Rows.Count
            
                total = total + Application.Evaluate(Replace(Replace(FORMULA_FIND, _
                                                                "<word>", LOOK_FOR_1), _
                                                        "<row>", i)) _
                              + Application.Evaluate(Replace(Replace(FORMULA_FIND, _
                                                                "<word>", LOOK_FOR_2), _
                                                        "<row>", i))
            Next i
        End With
        
        MsgBox total
        Application.ScreenUpdating = True
    End Sub

  3. #3
    Registered User
    Join Date
    08-19-2011
    Location
    UK
    MS-Off Ver
    2016
    Posts
    45

    Re: Add an 'x' to column U if

    Hi Bob,

    Many thanks for such as quick reply.

    I've looked through the code you supplied - would it be possible for you to add my three examples into the code so i can get an understanding of the 'template' i need to use?

    Thanks again.
    Last edited by NBVC; 09-26-2011 at 11:50 AM. Reason: deleted quote

  4. #4
    Forum Expert Bob Phillips's Avatar
    Join Date
    09-03-2005
    Location
    Wessex
    MS-Off Ver
    Office 2003, 2010, 2013, 2016, 365
    Posts
    3,284

    Re: Add an 'x' to column U if

    Change the constants LOOK_FOR_1 and LOOK_FOR_2

  5. #5
    Registered User
    Join Date
    08-19-2011
    Location
    UK
    MS-Off Ver
    2016
    Posts
    45

    Re: Add an 'x' to column U if

    Quote Originally Posted by Bob Phillips View Post
    Change the constants LOOK_FOR_1 and LOOK_FOR_2
    Ah right, thanks.

    Would this mean that i would need to have a separate macro for each 'hello' and 'goodbye' (each entry of terms that i'm seeing if they appear in the same row)?

  6. #6
    Forum Expert Bob Phillips's Avatar
    Join Date
    09-03-2005
    Location
    Wessex
    MS-Off Ver
    Office 2003, 2010, 2013, 2016, 365
    Posts
    3,284

    Re: Add an 'x' to column U if

    You probably need to state your EXACT requirements.

  7. #7
    Registered User
    Join Date
    08-19-2011
    Location
    UK
    MS-Off Ver
    2016
    Posts
    45

    Re: Add an 'x' to column U if

    Quote Originally Posted by Bob Phillips View Post
    You probably need to state your EXACT requirements.
    As per opening post:

    Aim:
    - Look at A2:O2 and if 'this word or words' AND 'this word or words' appear in any cell then add an 'x' to U2.

    Misc:
    - spread sheet has thousands of rows
    - there will be a number of terms to search for. Such as ('hello' AND 'goodbye', 'red' AND 'blue, 'blue' AND 'water')

    Further info to clarify:

    I need the code to look at each row for certain terms. The number of these terms are unknown at the moment.

    When i refer to terms i mean if 'word a' AND 'word b' appear in a row (columns A to O) then add an 'x' to column U on the same row as the terms appeared.

    Real life scenario: If the words 'hello' and 'goodbye' appear in any cells on one row then input an 'x' in the U column of that row.

    Hope that helps and is clear enough - many thanks

  8. #8
    Forum Expert Bob Phillips's Avatar
    Join Date
    09-03-2005
    Location
    Wessex
    MS-Off Ver
    Office 2003, 2010, 2013, 2016, 365
    Posts
    3,284

    Re: Add an 'x' to column U if

    I don't see that explaining this statement in your previous post ... Would this mean that i would need to have a separate macro for each 'hello' and 'goodbye' (each entry of terms that i'm seeing if they appear in the same row)?

  9. #9
    Registered User
    Join Date
    08-19-2011
    Location
    UK
    MS-Off Ver
    2016
    Posts
    45

    Re: Add an 'x' to column U if

    Quote Originally Posted by Bob Phillips View Post
    I don't see that explaining this statement in your previous post ... Would this mean that i would need to have a separate macro for each 'hello' and 'goodbye' (each entry of terms that i'm seeing if they appear in the same row)?
    Hi Bob,

    By this I was meaning would i need to have a separate saved macro for each 'term' and 'term2' that i was querying.

    So in effect i was asking could all the possible 'term' and term2', 'term3' and 'term4', 'term5' and term6' be controllable from just a single macro run once as opposed to your previous code which to the untrained eye looked as if it handled one query at a time?

    Hope that helps?

  10. #10
    Forum Expert Bob Phillips's Avatar
    Join Date
    09-03-2005
    Location
    Wessex
    MS-Off Ver
    Office 2003, 2010, 2013, 2016, 365
    Posts
    3,284

    Re: Add an 'x' to column U if

    My code looks for word pairs, which is how I read your original requirement. I am still not sure if you want to look for a number of word pairs in separate queries, or any number of words in a single query. And how does this procedure get told what words to search for?

  11. #11
    Registered User
    Join Date
    08-19-2011
    Location
    UK
    MS-Off Ver
    2016
    Posts
    45

    Re: Add an 'x' to column U if

    Quote Originally Posted by Bob Phillips View Post
    My code looks for word pairs, which is how I read your original requirement. I am still not sure if you want to look for a number of word pairs in separate queries, or any number of words in a single query. And how does this procedure get told what words to search for?
    Hi Bob,

    - yes, want to look for a number of independent word pairs in one query / macro
    - i could add a list of the word pairs into the code as and when more are needed to be added or make the macro look at a table which is populated with word pairs? Either would be great.

    Many thanks.

  12. #12
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Add an 'x' to column U if

    sub snb()
     sq=split("hello|goodbye_red|blue_blue|water","_")
     
     sn =sheets(1).cells(1).currentregion.resize(,15)
     su=cells(1,21).resize(ubound(sn))
     
     for j=0 to ubound(sq)
       sp=split(sq(j),"|")
       for jj=2 to ubound(sn)
        if instr(join(application.index(sn,jj)),sp(0))*instr(join(application.index(sn,jj)),sp(1))> 0 then su(jj,1)="x"
       next
     next
     
     cells(1,21).resize(ubound(sn))=su
    End Sub
    Use F8 to step through the code.
    Last edited by snb; 09-28-2011 at 08:58 AM.



  13. #13
    Registered User
    Join Date
    08-19-2011
    Location
    UK
    MS-Off Ver
    2016
    Posts
    45

    Re: Add an 'x' to column U if

    snb thank you also for adding to the thread.

    I've just ran the macro you have provided, but it's turned up the following error:
    -------------
    Compile error:
    Syntax error
    -------------
    whilst highlighting this line of code
    sp=split(sq(j,"|")
    Is it something i'm doing wrong or is it the code?

    Thanks again

  14. #14
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Add an 'x' to column U if

    sp=split(sq(j),"|")

+ 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