Concatenate 2 column values if conditions met using VBA

edwardj3

New Member
Joined
Jan 16, 2018
Messages
41
Office Version
  1. 365
Platform
  1. Windows
Hi all,

I want to concatenate 2 columns if column A is Not blank and column B cells contains the text "Enquiry" which is part of a longer string but will always be present.

I want to go down all of Column A and Column B and return result column A but this isn't happening with the code below. Any suggestions to resolve this would be appreciated.

Sub concat()

Dim LR As Long 'LR = Last Row
LR = Cells(Rows.Count, 1).End(xlUp).Row

Range("a1").Select

For Each cell In ActiveSheet.Range("a1:a" & LR)
If cell <> "" And (ActiveCell.Offset(0, 1)) Like "*Enq*" Then
cell.Value = cell.Value & ActiveCell.Offset(0, 1).Value
End If
Next cell

End Sub
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
You are using activecell.offset(,1)

But activecell is A1

try
VBA Code:
Sub concat()

    Dim LR As Long                               'LR = Last Row
    LR = Cells(Rows.Count, 1).End(xlUp).Row

    'Range("a1").Select

    For Each cell In ActiveSheet.Range("a1:a" & LR)
        If cell <> "" And cell.Offset(0, 1) Like "*Enq*" Then
            cell.Value = cell.Value & cell.Offset(0, 1).Value
        End If
    Next cell

End Sub
 
Upvote 0
You are using activecell.offset(,1)

But activecell is A1

try
VBA Code:
Sub concat()

    Dim LR As Long                               'LR = Last Row
    LR = Cells(Rows.Count, 1).End(xlUp).Row

    'Range("a1").Select

    For Each cell In ActiveSheet.Range("a1:a" & LR)
        If cell <> "" And cell.Offset(0, 1) Like "*Enq*" Then
            cell.Value = cell.Value & cell.Offset(0, 1).Value
        End If
    Next cell

End Sub

Thank you for providing the updated code. Worked exactly as I hoped.
 
Upvote 0

Forum statistics

Threads
1,214,971
Messages
6,122,520
Members
449,088
Latest member
RandomExceller01

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