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
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
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
 
Upvote 0
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
 
Upvote 0

Forum statistics

Threads
1,223,275
Messages
6,171,123
Members
452,381
Latest member
Nova88

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top