Concatenate multiple column and its heading

gleamng

Board Regular
Joined
Oct 8, 2016
Messages
98
Office Version
  1. 365
  2. 2021
  3. 2019
  4. 2016
  5. 2013
  6. 2011
  7. 2010
  8. 2007
  9. 2003 or older
Platform
  1. Windows
  2. MacOS
  3. Mobile
  4. Web
hi everyone, below is my excel worksheet

i need help with a vba to concatenate from Column D to AY into Col AZ with some conditions
a. if a cell is 0 or blank, it should be omitted
b. Col C and D be merged with space in between
c. Col E to AY should be merged with their heanding in cell 1
d. Senicolon ";" should be seperator in between each cell value
e. Result in col AZ be formatted as text format #,##0.00
i have tried my a formula which i have in col AZ, it works but its too long and cumbersome; the need for a vba code to help. below is my excel sheet for further details

Book1
ABCDEFGHIJKLMNOPQRSTUVWXYZAAABACADAEAFAGAHAIAJAKALAMANAOAPAQARASATAUAVAWAXAYAZ
1NAME BASIC SOCIETY COOP ZEDVANCELOAN TRACTOR STOCKCORP OURSMFB KWHACOOP KAMHOUSE IYERUOKIN ILUDUN-ORO ILORINMFB IBOLO FMBLOAN-3 FMBLOAN-2B FMBLOAN-2A FMBLOAN-1C FMBLOAN-1B FIRSTHERITAGE EMIRATE-CONTRIBU DIRECT CREDIT CONFIDENCE B.GAMBARI B.FULANI APEKS NHF PAYE WATER D-LEVY VET-LEVY QRTRENT PHARM PERMSEC PERMCOP NUJ NACHPN MWAN MUSWOW MOSQUE MOJ MDCAN KWEHO IMAN IGBOMINA HLT UNION AMLSNN AMELTAN AHAPN AAEUN merged ded
2SAMU ALAJO54,613.40 CHW 25,000.00----------------------709.97669.45150.008.33----------------1,092.27---- CHW-25,000.00;
3BODUNRIN ELEHA261,859.35 CHW 82,000.00----------------------2,958.5821,788.12200.008.33----------------6,827.49---- CHW-82,000.00;
4UMOBANO SARAH261,859.35 WEH 98,000.00----------------------2,958.5821,788.12200.008.33----------------6,827.49---- WEH-98,000.00;
5DARAMOYE ADENIKE193,699.60 WEH 69,000.00----------------------2,154.8912,929.08200.008.33----------------4,972.83---- WEH-69,000.00;
6SHOLA DURO108,087.75 FIN 16,000.00----------------------1,405.143,964.39200.008.33----------------2,161.76---- FIN-16,000.00;
7AVAGADROS BUKOYE83,223.40 EDU 17,000.00----------------------1,081.902,621.37200.008.33----------------1,664.47---- EDU-17,000.00;
8TANKPAFU EMIWOROGI244,118.50 FIN 80,000.00----------------------3,173.5416,485.62200.008.33--------2,000.00-------4,882.37---- FIN-80,000.00;
9KPADA JEBBA847,299.85 FIN 40,000.00----------------------4,236.5095,201.92200.008.33-------2,000.00---11,000.00--------- FIN-40,000.00;
10MOJOYIN KALE105,095.50 MCI 19,000.00----------------------1,366.243,792.41200.008.33----------------2,101.91---- MCI-19,000.00;
11KOKO IGBONA260,686.60 HPR 3,000.00----------------------3,150.5523,584.30200.008.33----------------7,270.50---- HPR-3,000.00;
Sheet1
Cell Formulas
RangeFormula
AZ2:AZ11AZ2=IF(D2<1,"",C2&"-"&TEXT(D2, "#,##0.00")&"; ")&IF(E2<1,"",$E$1&"-"&TEXT(E2, "#,##0.00")&"; ")&IF(F2<1,"",$F$1&"-"&TEXT(F2, "#,##0.00")&"; ")&IF(G2<1,"",$G$1&"-"&TEXT(G2, "#,##0.00")&"; ")&IF(H2<1,"",$H$1&"-"&TEXT(H2, "#,##0.00")&"; ")&IF(I2<1,"",$I$1&"-"&TEXT(I2, "#,##0.00")&"; ")&IF(J2<1,"",$J$1&"-"&TEXT(J2, "#,##0.00")&"; ")&IF(K2<1,"",$K$1&"-"&TEXT(K2, "#,##0.00")&"; ")&IF(L2<1,"",$L$1&"-"&TEXT(L2, "#,##0.00")&"; ")&IF(M2<1,"",$M$1&"-"&TEXT(M2, "#,##0.00")&"; ")&IF(N2<1,"",$N$1&"-"&TEXT(N2, "#,##0.00")&"; ")&IF(O2<1,"",$O$1&"-"&TEXT(O2, "#,##0.00")&"; ")&IF(P2<1,"",$P$1&"-"&TEXT(P2, "#,##0.00")&"; ")


thanking you for your help and support
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
VBA Code:
Sub ConcatCells()
    lr = Range("A" & Rows.Count).End(xlUp).Row
    result = ""
    For r = 2 To lr
        result = Cells(r, 3).Value & " " & Format(Cells(r, 4).Value, "#,##0.00") & ";"
        For c = 5 To 51
            If Cells(r, c).Value <> "" And Cells(r, c) >= 1 Then
                result = result & Cells(1, c).Value & "-" & Format(Cells(r, c).Value, "#,##0.00") & ";"
            End If
        Next c
        Cells(r, 52).Value = result
    Next r
End Sub
 
Upvote 0
Solution
Thanks a million, it worked exactly as I wanted.
 
Upvote 0
Thanks 👍 once again for your help, @ iggydarsa.
I run into an error (compile error: variable not defined) and lr = is highlighted once I added option explicit above the macro.
Please kindly help me on this also. Best regards.
 
Upvote 0
Thanks 👍 once again for your help, @ iggydarsa.
I run into an error (compile error: variable not defined) and lr = is highlighted once I added option explicit above the macro.
Please kindly help me on this also. Best regards.

In addition also, if any cell in column c is blank, it should not contatenate col c and d.
Thanks a lot
 
Upvote 0
VBA Code:
Option Explicit

Sub ConcatCells()
    Dim lr As Long
    Dim result As String
    Dim r As Long
    Dim c As Long
    lr = Range("A" & Rows.Count).End(xlUp).Row
    result = ""
    For r = 2 To lr
        If Cells(r, 3).Value <> "" Then result = Cells(r, 3).Value & " " & Format(Cells(r, 4).Value, "#,##0.00") & ";"
        For c = 5 To 51
            If Cells(r, c).Value <> "" And Cells(r, c) >= 1 Then
                result = result & Cells(1, c).Value & "-" & Format(Cells(r, c).Value, "#,##0.00") & ";"
            End If
        Next c
        Cells(r, 52).Value = result
    Next r
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,182
Members
453,021
Latest member
Mohamed Magdi Tawfiq Emam

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