Macro to Concatenate with If / Else Function

earva

New Member
Joined
Feb 11, 2016
Messages
1
I'm trying to write a macro to concatenate five columns of information (Columns A - F) into Column G.
Everything in Column A starts with either ABC, DEF, or GHI, and I'd like the macro to skip Column E for rows where Column A starts with DEF.
So if Column A starts with ABC or GHI, concatenate A,B,C,D,E,F into Column G.
But if Column A starts with DEF, concatenate A,B,C,D,F into Column G.

Does anyone know how to do this? I'm using the below Macro currently:

Sub ConcatCols_First()
'concatenate columns A to F in column G

Dim LastRow As Long

With Worksheets("Test")
LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
With .Range("G2:G" & LastRow)
.Formula = "=A2&B2&C2&D2&E2&F2"
.Value = .Value
End With
End With

End Sub
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
You really don't need a macro, I think you just need this formula

=IF(A2="DEF",A2&B2&C2&D2&F2,A2&B2&C2&D2&E2&F2)

if you want to use your macro, just change it so it puts the formula above in, instead of the one you have it entering.

I hope this helps.

Ken
 
Upvote 0

Forum statistics

Threads
1,216,075
Messages
6,128,662
Members
449,462
Latest member
Chislobog

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