VBA - IF Contains then wrap with Chr in another cell

Cepheus

New Member
Joined
Feb 16, 2006
Messages
4
Good-day.

I have 2 columns of data (D, E), each with multiple rows of data in (number of rows changes depending on task):

1st column (D) = base term (blue widgets)
2nd column (E) = Exact, Phrase or Broad (can you tell what I do for living!?)

I need something that copies the value in column D, pastes it into column F and then wraps it accordingly where:

column E=Exact -> Square Brackets (i.e [blue widgets])
column E=Phrase -> Quotation Marks (i.e. "blue widgets")
column E=Broad -> nothing (i.e. blue widgets)

Have so far used:

Sub CellQuotes()
Dim cell As Range
For Each cell In Range("E20:E1048576").Cells
If cell.Value = "Exact" Then
cell.Value = Chr(91) & cell.Value & Chr(93)
End If
If cell.Value = "Phrase" Then
cell.Value = Chr(34) & cell.Value & Chr(34)
End If
Next
End Sub

but this wraps (correctly) the values in column D, rather than copy, paste then wrap in column F.

Using VBA because of the special characters ("), but first time trying to get to grips with it.

As you will have noticed, I'm also trying to run this over all rows with data - feel sure there's a better way to do this than just maxing the row range as above.

virtual pint for anyone who can help! :)

C
 

VoG

Legend
Joined
Jun 19, 2002
Messages
63,651
Try

Code:
Sub CellQuotes()
Dim cell As Range, LR As Long
LR = Range("E" & Rows.Count).End(xlUp).Row
For Each cell In Range("E20:E" & LR)
    If cell.Value = "Exact" Then
        cell.Offset(, 1).Value = Chr(91) & cell.Offset(, -1).Value & Chr(93)
ElseIf cell.Value = "Phrase" Then
    cell.Offset(, 1).Value = Chr(34) & cell.Offset(, -1).Value & Chr(34)
End If
Next cell
End Sub
 

AlphaFrog

MrExcel MVP
Joined
Sep 2, 2009
Messages
16,166
Code:
Sub CellQuotes()

    Dim cell As Range
    
    For Each cell In Range("E20", Range("E" & Rows.Count).End(xlUp))

        Select Case LCase(Trim(cell.Value))
            Case "exact"
                cell.Offset(, 1).Value = Chr(91) & cell.Offset(, -1).Value & Chr(93)
            Case "phrase"
                cell.Offset(, 1).Value = Chr(34) & cell.Offset(, -1).Value & Chr(34)
            Case "broad"
                cell.Offset(, 1).Value = cell.Offset(, -1).Value
        End Select
        
    Next cell
    
End Sub
 

Forum statistics

Threads
1,082,114
Messages
5,363,244
Members
400,722
Latest member
DrewPop24

Some videos you may like

This Week's Hot Topics

  • populate from drop list with multiple tables
    Hi All, i have a drop list that displays data, what i want is when i select one of those from the list to populate text from different tables on...
  • Find list of words from sheet2 in sheet1 before a comma and extract text vba
    Hi Friends, Trying to find the solution on my task. But did not find suitable one to the need. Here is my query and sample file with details...
  • Dynamic Formula entry - VBA code sought
    Hello, really hope one of you experts can help with this - i've spent hours on this and getting no-where. .I have a set of data (more rows than...
  • Listbox Header
    Have a named range called "AccidentsHeader" Within my code I have: [CODE]Private Sub CommandButton1_Click() ListBox1.RowSource =...
  • Complex Heat Map using conditional formatting
    Good day excel world. I have a concern. Below link have a list of countries that carries each country unique data. [URL...
  • Conditional formatting
    Hi good morning, hope you can help me please, I have cells P4:P54 and if this cell is equal to 1 then i want row O to say "Fully Utilised" and to...
Top