+ Reply to Thread
Results 1 to 5 of 5

If / ElseIf Then Macro to check if a cell (partly) contains a specified text value.

Hybrid View

Jongering If / ElseIf Then Macro to... 07-17-2013, 03:45 AM
NickyC Re: If / ElseIf Then Macro to... 07-17-2013, 03:50 AM
Jongering Re: If / ElseIf Then Macro to... 07-17-2013, 04:08 AM
NickyC Re: If / ElseIf Then Macro to... 07-17-2013, 04:18 AM
Jongering Re: If / ElseIf Then Macro to... 07-17-2013, 04:34 AM
  1. #1
    Registered User
    Join Date
    06-12-2012
    Location
    Amsterdam
    MS-Off Ver
    Excel 2010 (Windows) Excel 2011 (Mac)
    Posts
    30

    If / ElseIf Then Macro to check if a cell (partly) contains a specified text value.

    Hi,

    Another little Excel roadblock for me today. Can someone help me out? My problem is described in the simple code below. Thanks!

    Sub Checkloop()
    
    'This sub presents a value in a specific cell based on the value in the cell before it.
    'The only problem is that I want the loop to check if the specified cell *contains* a certain text. Right now it only works
    'if the specified cells are exactly equal to the texts gives below. How do I change the code so that it properly checks if the cell contains a value
    'Example: If a cell A1 contains"ORT on monday and wednesday" I want this cell to be recognised based on the fact that it contains the letter "ORT"
    'PLEASE HELP!
    
        Do
            If ActiveCell.Offset(0, -1) = "ORT" Then
                   ActiveCell.Value = "ORT"
                   
        ElseIf ActiveCell.Offset(0, -1) = "Urgent WZ" Then
                   ActiveCell.Value = "URGENT"
            
        ElseIf ActiveCell.Offset(0, -1) = "SPOED AH" Then
                   ActiveCell.Value = "SPOED"
                   
        ElseIf ActiveCell.Offset(0, -1) = "HEE WZ" Then
                   ActiveCell.Value = "HEE"
    
            Else
                ActiveCell.FormulaR1C1 = "Nothing's up"
                
        End If
    
        ActiveCell.Offset(1, 0).Select
    
    'define until which cell to loop
        Loop Until IsEmpty(ActiveCell.Offset(0, -1))
        
    End Sub

  2. #2
    Forum Expert
    Join Date
    06-09-2010
    Location
    Australia
    MS-Off Ver
    Excel 2013
    Posts
    1,715

    Re: If / ElseIf Then Macro to check if a cell (partly) contains a specified text value.

    Hi
    in these circumstance i use

    if replace(activecell, "ORT","") <> Activecell
    this picks up any instance of "ORT" in the cell

  3. #3
    Registered User
    Join Date
    06-12-2012
    Location
    Amsterdam
    MS-Off Ver
    Excel 2010 (Windows) Excel 2011 (Mac)
    Posts
    30

    Re: If / ElseIf Then Macro to check if a cell (partly) contains a specified text value.

    Hi Nicky,

    Thanks for the reply. How can I best use that in my macro? Or more precisely, how do I change your line to check if the value contains ORT in the prior column. If that is the case I would like the Macro te write "ORT" in the current column(column B) and nut not replace the existing texts in column A.

  4. #4
    Forum Expert
    Join Date
    06-09-2010
    Location
    Australia
    MS-Off Ver
    Excel 2013
    Posts
    1,715

    Re: If / ElseIf Then Macro to check if a cell (partly) contains a specified text value.

    try

    If Replace(ActiveCell.Offset(0, -1), "ORT", "") <> ActiveCell.Offset(0, -1) Then ActiveCell.Value = "ORT"

  5. #5
    Registered User
    Join Date
    06-12-2012
    Location
    Amsterdam
    MS-Off Ver
    Excel 2010 (Windows) Excel 2011 (Mac)
    Posts
    30

    Re: If / ElseIf Then Macro to check if a cell (partly) contains a specified text value.

    Thanks Nicky,

    Works like a charm! The eventuel code has now become:

    Sub NEWLOOP()
    
    Do
    If Replace(ActiveCell.Offset(0, -1), "ORT", "") <> ActiveCell.Offset(0, -1) Then
    ActiveCell.Value = "ORT"
                   
    ElseIf Replace(ActiveCell.Offset(0, -1), "ORT", "") <> ActiveCell.Offset(0, -1) Then
    ActiveCell.Value = "URGENT"
            
    ElseIf Replace(ActiveCell.Offset(0, -1), "ORT", "") <> ActiveCell.Offset(0, -1) Then
    ActiveCell.Value = "SPOED"
                   
    ElseIf Replace(ActiveCell.Offset(0, -1), "ORT", "") <> ActiveCell.Offset(0, -1) Then
    ActiveCell.Value = "HEE"
    
    Else
    ActiveCell.FormulaR1C1 = "tja"
                
    End If
    
    ActiveCell.Offset(1, 0).Select
    
    'pas deze aan naar de eerste rij (patientnummertje)
        Loop Until IsEmpty(ActiveCell.Offset(0, -1))
        
    End Sub
    Many thanks from sunny Amsterdam!

+ 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. [SOLVED] VBA macro to check if one cell has text and paste a formula in a different cell
    By vitt4300 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-10-2013, 01:38 PM
  2. Code stepping in to elseif statement when elseif is definitely not true
    By DFrank231 in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 02-13-2013, 05:31 PM
  3. Macro to check column A and copy text from cell above
    By robertguy in forum Excel General
    Replies: 0
    Last Post: 08-20-2012, 09:22 AM
  4. [SOLVED] IF Cell.Value partly equals
    By jordan2322 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 05-17-2012, 01:23 AM
  5. Make text partly bold, but failed
    By vdongen in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 10-10-2010, 09:05 AM

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