Convert vba to formula with Named Ranges

detriez

Board Regular
Joined
Sep 13, 2011
Messages
193
Office Version
  1. 365
Platform
  1. Windows
I need to convert this to a formula
If a cell is not blank, concatenate the Header (nMerge1, nMerge2, nMerge3 etc) and the cell value
I use a named range for the header because the header will always change.

The challenge is, columns may be inserted or deleted which can change the column reference.. Would a named range be applicable here too?
Not every cell in the column will be populated so I cant use End(xlUP).Row

VBA Code:
lr = ActiveSheet.Cells(Rows.Count, "B").End(xlUp).Row + 1

    Application.ScreenUpdating = False
    With Range("AL2:AL" & lr)
        .FormulaR1C1 = "=IF(RC[1]<>"""",CONCATENATE((R1C39&"": ""),RC[1])&CHAR(10),"""")&" & _
                       "IF(RC[2]<>"""",CONCATENATE((R1C40&"": ""),RC[2])&CHAR(10),"""")&" & _
                       "IF(RC[3]<>"""",CONCATENATE((R1C41&"": ""),RC[3])&CHAR(10),"""")&" & _
                       "IF(RC[4]<>"""",CONCATENATE((R1C42&"": ""),RC[4])&CHAR(10),"""")

My formula.. Should this be AND or OR?
Excel Formula:
=IF(AN2<>"",nMerge1&": "&AN2,"")&CHAR(10)
IF(AN2<>"",nMerge2&": "&Ap2,"")&CHAR(10)
IF(AN2<>"",nMerge3&": "&AP2,"")&CHAR(10)
IF(AN2<>"",nMerge4&": "&AQ2,"")&CHAR(10)

This should be the end result
1620692564191.png
 
Hi Fluff.. This works great.
I worried that adding or removing a column before Column M would interfere with the cell and column references but Excel seems to be compensating.

This will end up being an Excel 365 shared workbook so (unless I'm wrong) VBA is not an option.
What would be a good way to copy the formula down to the last row?
 
Upvote 0

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
What would be a good way to copy the formula down to the last row?
Double click the bottom right hand corner of the cell. It will then fill down to the next blank cell in a neighbouring col.
 
Upvote 0
Can you think of a way to automate that so the formula populates down to the last used row?
I'd hate to copy the cell down to 1,048,576
 
Upvote 0
As long as you don't have blank cells in a neighbouring col (say AL) then it will just copy down to the last used cell.
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,182
Members
453,020
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