Concatenate the values on condition

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
Hi, Posting a small sample would be better, some members can't or won't download data from external source.
Would a udf be acceptable?
 
Upvote 0
Hi, Posting a small sample would be better, some members can't or won't download data from external source.
Would a udf be acceptable?


Ya no problem.. but will it provide the desired output.. as i got a udf which concats non blanks cells but the delimiters again its problem.
 
Upvote 0
Here is the macro
Code:
Sub ConcatenateColumns()


Dim LR As Long, TR As Long, LC As Long, TC As Long
Dim Resultstring As String


LR = Range("A" & Rows.Count).End(xlUp).Row
LC = Range("A1").Offset(0, Columns.Count - 1).End(xlToLeft).Column


For TR = 2 To LR
Resultstring = Range("A" & TR) & " " & Range("B" & TR) & ": "


    For TC = 4 To LC Step 2
    
    If Cells(TR, TC) <> "" Then
    Resultstring = Resultstring & Cells(TR, TC - 1) & ": " & Cells(TR, TC) & ";"
    End If
       
    Next TC


If Resultstring <> "" Then Cells(TR, TC) = Left(Resultstring, Len(Resultstring) - 1)
Next TR


End Sub
 
Upvote 0
Here is the macro
Code:
Sub ConcatenateColumns()


Dim LR As Long, TR As Long, LC As Long, TC As Long
Dim Resultstring As String


LR = Range("A" & Rows.Count).End(xlUp).Row
LC = Range("A1").Offset(0, Columns.Count - 1).End(xlToLeft).Column


For TR = 2 To LR
Resultstring = Range("A" & TR) & " " & Range("B" & TR) & ": "


    For TC = 4 To LC Step 2
    
    If Cells(TR, TC) <> "" Then
    Resultstring = Resultstring & Cells(TR, TC - 1) & ": " & Cells(TR, TC) & ";"
    End If
       
    Next TC


If Resultstring <> "" Then Cells(TR, TC) = Left(Resultstring, Len(Resultstring) - 1)
Next TR


End Sub

Thank u thanks alot u saved my time..

Really appreciate your help... cheers
 
Upvote 0
Here is the macro
Code:
Sub ConcatenateColumns()


Dim LR As Long, TR As Long, LC As Long, TC As Long
Dim Resultstring As String


LR = Range("A" & Rows.Count).End(xlUp).Row
LC = Range("A1").Offset(0, Columns.Count - 1).End(xlToLeft).Column


For TR = 2 To LR
Resultstring = Range("A" & TR) & " " & Range("B" & TR) & ": "


    For TC = 4 To LC Step 2
    
    If Cells(TR, TC) <> "" Then
    Resultstring = Resultstring & Cells(TR, TC - 1) & ": " & Cells(TR, TC) & ";"
    End If
       
    Next TC


If Resultstring <> "" Then Cells(TR, TC) = Left(Resultstring, Len(Resultstring) - 1)
Next TR


End Sub


Hi i am facing a problem in the line

LR = Range("A" & Rows.Count).End(xlUp).Row
LC = Range("A1").Offset(0, Columns.Count - 1).End(xlToLeft).Column

if the change the cell reference to

LR = Range("K" & Rows.Count).End(xlUp).Row
LC = Range("K1").Offset(0, Columns.Count - 1).End(xlToLeft).Column

its shoes Error Run Time Error 1004
 
Upvote 0
Change this code,

LC = Range("K1").Offset(0, Columns.Count - 1).End(xlToLeft).Column

as

LC = Cells(1, Columns.Count).End(xlToLeft).Column
 
Upvote 0
You could also try this user-defined function, which uses a similar method. Place the code into a standard module then use in your worksheet like a normal function, noting that it is designed for a single-row range input.
Use in your sheet like this (in any cell) and copy down.

=Concat(A2:T2)

Rich (BB code):
Function Concat(r As Range) As String
  Dim i As Long
  Dim tmp As String
  
  For i = 4 To r.Columns.Count Step 2
    If Not IsEmpty(r.Cells(i)) Then tmp = tmp & ";" & r.Cells(i - 1).Value & ": " & r.Cells(i).Value
  Next i
  Concat = r.Cells(1).Value & " " & r.Cells(2).Value & ": " & Mid(tmp, 2)
End Function
 
Upvote 0

Forum statistics

Threads
1,215,831
Messages
6,127,145
Members
449,363
Latest member
Yap999

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