Concat cells while retaining formats - Convert Macro into UDF (If Possibe)

hrayani

Well-known Member
Joined
Jul 23, 2010
Messages
1,490
Office Version
  1. 2016
Platform
  1. Windows
Hello Friends

I want to concatenate cells with its format as it is

I found this macro while searching on the net... Can this be converted into a UDF so that we can use a formula.

EDIT:
Also it does not allow to concat values in columns & does not seperates with a comma or anything

VBA Code:
Sub MergeFormatCell()
'Updateby Extendoffice
    Dim xSRg As Range
    Dim xDRg As Range
    Dim xRgEachRow As Range
    Dim xRgEach As Range
    Dim xRgVal As String
    Dim I As Integer
    Dim xRgLen As Integer
    Dim xSRgRows As Integer
    Dim xAddress As String
    On Error Resume Next
    xAddress = ActiveWindow.RangeSelection.Address
    Set xSRg = Application.InputBox("Please select cell columns to concatenate:", "KuTools For Excel", xAddress, , , , , 8)
    If xSRg Is Nothing Then Exit Sub
    xSRgRows = xSRg.Rows.Count
    Set xDRg = Application.InputBox("Please select cells to output the result:", "KuTools For Excel", , , , , , 8)
    If xDRg Is Nothing Then Exit Sub
    Set xDRg = xDRg(1)
    For I = 1 To xSRgRows
        xRgLen = 1
        With xDRg.Offset(I - 1)
            .Value = vbNullString
            .ClearFormats
            Set xRgEachRow = xSRg(1).Offset(I - 1).Resize(1, xSRg.Columns.Count)
            For Each xRgEach In xRgEachRow
                .Value = .Value & Trim(xRgEach.Value) & " "
            Next
            For Each xRgEach In xRgEachRow
                xRgVal = xRgEach.Value
                With .Characters(xRgLen, Len(Trim(xRgVal))).Font
                .Name = xRgEach.Font.Name
                .FontStyle = xRgEach.Font.FontStyle
                .Size = xRgEach.Font.Size
                .Strikethrough = xRgEach.Font.Strikethrough
                .Superscript = xRgEach.Font.Superscript
                .Subscript = xRgEach.Font.Subscript
                .OutlineFont = xRgEach.Font.OutlineFont
                .Shadow = xRgEach.Font.Shadow
                .Underline = xRgEach.Font.Underline
                .ColorIndex = xRgEach.Font.ColorIndex
                End With
                xRgLen = xRgLen + Len(Trim(xRgVal)) + 1
            Next
        End With
    Next I
End Sub

Regards,

Humayun
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
Any text placed in a cell from a formula (whether the functions used in the formula are built-in or UDFs) cannot have parts of that text formatted differently than the rest of the text. You can only do that with macros or event code. With that said, it may be possible to have a change event code procedure duplicate the functionality of your formulas and that event code procedure could do the formatting you want... from the user's standpoint, the actions of the event code will look the same as the actions that a formula produces. If that is something you would want to pursue, we need to know your formula, what cells it will be placed in and what formatting you want to do to the text. There is probably some other stuff we might need to know that will become apparent once we start coding the event procedure.
 
Upvote 0
Thanks Rick for the reply,

I would like to post a sample data to let you know what exactly I want.

I would want the C10 answer to be like this Alpha, Bravo, Peter, Humayun

Please have a look below... The original data as too big so I have prepared a dummy data to let you have an idea
Book1
ABCDEF
1Ref #Actual DATERESULTRef #Names
252720-May-20Alpha527Alpha
352821-May-20Bravo528Bravo
4530  530Charlie
5535 535John
645626-May-20Peter456Peter
7785 785Rick
8901 901Tony
9101229-May-20Humayun1012Humayun
10Required >Alpha, Bravo, Peter, Humayun
Sheet1
Cell Formulas
RangeFormula
D4D4=IF(ISBLANK(B4),"",INDEX($E$2:$E$9,MATCH(A4,$F$2:$F$9,0)))
C2:C9C2=IF(B2<>"",INDEX(names,MATCH(A2,ref,0)),"")
Named Ranges
NameRefers ToCells
names=Sheet1!$F$2:$F$9D4, C2:C9
ref=Sheet1!$E$2:$E$9D4, C2:C9
Cells with Conditional Formatting
CellConditionCell FormatStop If True
C2:C9Expression=TODAY()<B2textNO
C2:C9Expression=TODAY()>=B2textNO
 
Upvote 0
Most of that makes sense, but I have a couple of questions...

1) Since your example is a mock-up, I need to check... is all the information shown actually all on the same worksheet in your real workbook?

2) I am unclear how the single formula in cell D4 figures into, well, anything. For example, can you explain why it is part of your named ranges?
 
Upvote 0
1) Since your example is a mock-up, I need to check... is all the information shown actually all on the same worksheet in your real workbook?


In my real worksheet the actual dates & ref # are extracted (with formulas) from a different worksheet but the same workbook

2) I am unclear how the single formula in cell D4 figures into, well, anything. For example, can you explain why it is part of your named ranges?

Please ignore that... I was just testing the formula
 
Upvote 0
In my real worksheet the actual dates & ref # are extracted (with formulas) from a different worksheet but the same workbook
In order to write the code correctly, I need to know about different worksheets that are referenced in a formula, so you need to show us your actual formulas that you are actually using.
 
Upvote 0
Hi Rick,

Sorry for coming back late

These are the actual formulas.... in worksheet called MONTHLY CALENDAR

For Ref #
=IFERROR(AGGREGATE(15,6,orders_ref/((pp_sample_target_date=F6)*ISNA(MATCH(orders_ref,D6:D6,0))),1),"")

For Actual Dates
=IFERROR(IF(COUNTIFS(orders_ref,D7,pp_sample_actual_date,"")=0,INDEX(pp_sample_actual_date,MATCH(D7,orders_ref,0)),""),"")

For Result
=IF(D7<>"","PP SAMPLE - "&INDEX(orders_po,MATCH(D7,orders_ref,0))&" "&INDEX(orders_supplier,MATCH(D7,orders_ref,0))&" ("&INDEX(orders_quality,MATCH(D7,orders_ref,0))&")","")

Please note that these 3 formulas are for one category i.e. pp_sample but all the data is coming from one single worksheet (within the workbook) called DATABASE

I am working on 7 categories in total as of now. only the name ranges will be changed for the other categories.
Just to give you an example..... the other category is dpi

so for dpi the actual formulas will be

For For Ref #
=IFERROR(AGGREGATE(15,6,orders_ref/((dpi_target_date=F6)*ISNA(MATCH(orders_ref,D17:D17,0))),1),"")

For Actual Dates
=IFERROR(IF(COUNTIFS(orders_ref,D18,dpi_actual_date,"")=0,INDEX(dpi_actual_date,MATCH(D18,orders_ref,0)),""),"")


For Result
=IF(D18<>"","DPI - "&INDEX(orders_po,MATCH(D18,orders_ref,0))&" "&INDEX(orders_supplier,MATCH(D18,orders_ref,0))&" ("&INDEX(orders_quality,MATCH(D18,orders_ref,0))&")","")

Next all the data in the result columns are having conditional formats to turn green, red or remain black as it is

Please also note that target_date in this case is looking at date in CELL F6 which will not be the case for other columns

Kindly let me know if any other info is required

Regards,

Humayun
 
Upvote 0

Forum statistics

Threads
1,214,429
Messages
6,119,433
Members
448,897
Latest member
ksjohnson1970

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