+ Reply to Thread
Results 1 to 4 of 4

Need to say 'contains' instead of equals

  1. #1
    Registered User
    Join Date
    05-16-2014
    MS-Off Ver
    Excel 2003
    Posts
    7

    Need to say 'contains' instead of equals

    Hi-

    I have some code that currently finds cells with specific text and then merges them with the 3 blank cells to the right (in order to make the text 1 line as it wraps around in the first column). I want to change this so that instead of saying if the cell equals the text, to instead say if it contains the text to merge the cells (as there will be minor tweaks to the text every few months and if I can use contain instead I won't have to constantly update the code as I know the beginning will stay the same).

    So for example on the first one (of the 3 I'm searching for) I'd like to say if the cell contains "Other grades include: " as opposed to saying exactly what the cell equals.

    Any help is greatly appreciated!

    Here's the code:
    Sub merge_cells(wb)


    Dim ws As Worksheet
    Dim C As Range
    For Each ws In Sheets
    For Each C In ws.Range("a2:a" & ws.Range("A" & Rows.Count).End(xlUp).Row)

    On Error GoTo 10

    If C.Value = "Other grades include: AU, AUX DX, FEX, I, IP, P, PS, S, SX, U, UX" Then
    C.Select
    Range(ActiveCell.Offset(0, 0), ActiveCell.Offset(0, 3)).Merge
    ActiveCell.Select
    End If

    If C.Value = "Data reflect grades posted as of 'January 17, 2014'" Then
    C.Select
    Range(ActiveCell.Offset(0, 0), ActiveCell.Offset(0, 3)).Merge
    ActiveCell.Select
    End If


    If C.Value = "Report produced by the Office of Institutional Research." Then
    C.Select
    Range(ActiveCell.Offset(0, 0), ActiveCell.Offset(0, 3)).Merge
    ActiveCell.Select
    End If




    Next
    10
    Next

    End Sub

  2. #2
    Valued Forum Contributor
    Join Date
    09-01-2013
    Location
    Dallas, TX
    MS-Off Ver
    Excel 2010
    Posts
    324

    Re: Need to say 'contains' instead of equals

    You would simply change this line to change the = to LIKE and to add * Wildcards like so


    Change original:
    Formula: copy to clipboard
    Please Login or Register  to view this content.


    to updated:
    Formula: copy to clipboard
    Please Login or Register  to view this content.

    Line above says if any contains the words "Other grades include:" then proceed to merge
    You can remove the wildcard from the front if you know the cells searching will always start with the phrase. Then wild card will pick up at the end.



    You can also do this for the other if statement lines.


    Full code modified:

    Please Login or Register  to view this content.
    Last edited by playaller; 05-28-2014 at 01:59 PM.


    Shelton A.
    If Helpful, Add Reputaion!

  3. #3
    Registered User
    Join Date
    05-16-2014
    MS-Off Ver
    Excel 2003
    Posts
    7

    Re: Need to say 'contains' instead of equals

    That did it! Thank you so much!!

  4. #4
    Valued Forum Contributor
    Join Date
    09-01-2013
    Location
    Dallas, TX
    MS-Off Ver
    Excel 2010
    Posts
    324

    Re: Need to say 'contains' instead of equals

    You're more than welcome. Glad I could help and thank you for the reputation!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. if column B equals "cash" then column H equals 0
    By im3d in forum Excel General
    Replies: 4
    Last Post: 04-26-2011, 04:43 PM
  2. If, Then Else and Equals.
    By listerofsmeg in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-11-2010, 07:18 AM
  3. [SOLVED] if a:a (range) equals january and c:c equals gas then add g:g ($)
    By BCOz in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 12-29-2005, 03:45 PM
  4. Replies: 1
    Last Post: 06-01-2005, 07:05 PM

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