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
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
You just find last row like this. No need to specify which column.
VBA Code:
Sub test()

Dim Last_Row As Long
   
On Error Resume Next
Last_Row = Cells.Find(What:="*", _
                After:=Range("A1"), _
                LookIn:=xlFormulas, _
                LookAt:=xlPart, _
                SearchOrder:=xlByRows, _
                SearchDirection:=xlPrevious).Row
On Error GoTo 0
   
Debug.Print Last_Row

End Sub
 
Upvote 0
Thanks Zot,
I'm sorry I wasn't more clear.
I cant use VBA as this will be an Excel 365 online sheet.
I need to do this via formulas

BTW...
I do like the way you find the last column.. I can use that part for other sheets
 
Upvote 0
I actually do not really understand what you really want but only want to find last row using excel formula :unsure: :)
The formula I found is using MATCH
Book2
ABCDEFGHIJKLM
111111
222222
333333
444444
555555
666666
77777
8888
9999
101010
111111
121212
1313
14141297 146
1514
Sheet1
Cell Formulas
RangeFormula
H14:M14I14=IFNA(MATCH(2,1/(B:B<>""),1),"")
H15H15=MAX(H14:M14)
Press CTRL+SHIFT+ENTER to enter array formulas.
 
Upvote 0
I think I really wrote a confusing post with multiple questions.
Let me try again.

I need a formula that will concatenate the column header (row 1) which I have set up as a named range (nMerge1, nMerge2, nMerge3 etc) and the cell value if the cell value is not blank and add a carriage return line feed after each one.

How can I string this formula together?

=IF(AN2<>"",nMerge1&": "&AN2,"")&CHAR(10)
IF(Ao2<>"",nMerge2&": "&Ao2,"")&CHAR(10)
IF(AP2<>"",nMerge3&": "&AP2,"")&CHAR(10)
IF(AQ2<>"",nMerge4&": "&AQ2,"")&CHAR(10)
 
Upvote 0
Not sure where the named ranges come in as you are looking at individual cells, but maybe
Excel Formula:
=TEXTJOIN(CHAR(10),,AN2:AQ2)
 
Upvote 0
Thanks Fluff
I am using Named Ranges for the "header cells" in row 1 because the user may insert/delete columns which could mess up the references to the headers..
Is there a better way to handle this?
 
Upvote 0
If you are using named ranges why are you also using cell references?
 
Upvote 0
Good question. The end result I'm looking achieve is:

IF a cell is not blank, concatenate inot AM the Header and the cell value so, something like this:

1620735804428.png
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,322
Members
452,635
Latest member
laura12345

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