Combine Cell Strings

JonRowland

Active Member
Joined
May 9, 2003
Messages
416
Office Version
  1. 365
Platform
  1. Windows
The following code combines strings in Cols C&D per row.

VBA Code:
 With Range("C2:C" & Range("A" & Rows.Count).End(xlUp).Row)
        .NumberFormat = "@"
        .Value = Evaluate(.Offset(0, 0).Address & "&"",""&" & .Offset(, 1).Address)
    End With

I am looking to expand this and combine B,C &D when there is text in B. If not then just combine C&Dand thought I would alter to
VBA Code:
    With Range("B2:B" & Range("A" & Rows.Count).End(xlUp).Row)
    
    If .Value = Empty Then
        .NumberFormat = "@"
        .Value = Evaluate(.Offset(0, 1).Address & "&"", ""&" & .Offset(, 2).Address)
    Else
        .NumberFormat = "@"
        .Value = Evaluate(.Offset(0, 0).Address & "&"", ""&" & .Offset(, 1).Address & "&"", ""&" & .Offset(, 2).Address)
End If
    End With

This should be if B is blank then comine C&D
If B is not blank then combine B,C&D.

But this ends up with Type Mismatch error. Am I missing something or is this a method that can't be used?

Thanks
Jon
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
I've just realised (silly me) I could use ISEMPTY but then I'm dealing with a range so not going to work as I had hoped and think I will revert to a loop instead.
 
Upvote 0
How about
VBA Code:
    With Range("B2:B" & Range("A" & Rows.Count).End(xlUp).Row)
        .NumberFormat = "@"
        .Value = Evaluate("textjoin("", "",," & .Resize(, 3).Address & ")")
    End With
 
Upvote 0
Fluff,
How about
VBA Code:
    With Range("B2:B" & Range("A" & Rows.Count).End(xlUp).Row)
        .NumberFormat = "@"
        .Value = Evaluate("textjoin("", "",," & .Resize(, 3).Address & ")")
    End With

Your solution appears to combine text from every row into one column and repeats for the range. I may have not explained myself clearly.

COL BCOL CCOL D
TEXT1TEXT2
Text1Text2Text3

1st row - as B is blank/empty, I would like in B the strings in C&D eg TEXT1,TEXT2
2nd row - as B isn't empty, I I would like in B Text1,Text2,Text3
 
Upvote 0
My fault, for not testing the code, will cols C & D always have data, or could they be blank?
 
Upvote 0
Based on your attempted code, could it be this?

VBA Code:
Sub test()
  With Range("B2:B" & Range("A" & Rows.Count).End(xlUp).Row)
    .NumberFormat = "@"
    .Value = Evaluate(Replace(Replace(Replace("if(#="""","""",#&"","")&%&"",""&^", "#", .Address), "%", .Offset(, 1).Address), "^", .Offset(, 2).Address))
  End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,839
Messages
6,121,891
Members
449,058
Latest member
Guy Boot

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