I have a cell that is referenced as =\"Dealer: \" & CustomerName
.
CustomerName is a dictionary referenced name. How could I go along of bolding only \"Dealer:\"
Requirements:
My understanding is that the OP needs to have in cell A5
the result of the formula ="Dealer: " & CustomerName
showing the Dealer:
part in bold characters.
Now, what it is not clear, is the nature of the CustomerName
part of the formula. This solution assumes it corresponds to a Defined Name
with workbook scope (let me know if different).
I assume that the reason for using a formula and not writing directly the result of the formula and formatting the A5
cell with a VBA procedure is to allow users to see the data from different customers just by a calculation change in the workbook, rather than by running a VBA procedure.
Let say that we have the following data in a worksheet named Report
, were the Defined Name CustomerName
has a workbook scope and is hidden.
Located at A5
is the formula ="Dealer: " & CustomerName
The Fig.1 shows the report with the data for Customer 1
.
Fig.1
Now if we change the Customer Number in cell E3
to 4
, the report will show the data of the customer selected; without running any VBA procedure. Unfortunately as the cell A5
contains a formula its contents font cannot be partially formatted to show “Dealer: ” in bold characters. The Fig.2 shows the report with the data for Customer 4
.
Fig.2
The solution proposed herewith is to Dynamically display the contents of a cell or range in a graphic object
To implement this solution we need to recreate the desired Output Range and add a Shape
in A5
that will contain a link to the Output Range.
Assuming that we don’t want this Output Range to be seen in the same worksheet were the report is, and keeping mind that the Output Range cells cannot be hidden; let’s create this Output Range in another worksheet named “Customers Data” at B2:C3
(see Fig.3). Enter in B2
Dealer:
and in C2
enter the formula =Customer Name
then format each cell as required (B2
font bold, C3
can have a different font type if you like – let’s apply font italic for this sample). Ensure the range have the appropriated width so the text does not overflows the cells.
Fig.3
It’s suggested to create a Defined Name
for this range. The code below creates the Defined Name
called RptDealer
.
Const kRptDealer As String = "RptDealer" ‘Have this constant at the top of the Module. It is use by two procedures
Sub Name_ReportDealerName_Add()
'Change Sheetname "Customers Data" and Range "B2:C2" as required
With ThisWorkbook.Sheets("Customers Data")
.Cells(2, 2).Value = "Dealer: "
.Cells(2, 2).Font.Bold = True
.Cells(2, 3).Formula = "=CustomerName" 'Change as required
.Cells(2, 3).Font.Italic = True
With .Parent
.Names.Add Name:=kRptDealer, RefersTo:=.Sheets("Customers Data").Range("B2:C2") ', _
Visible:=False 'Visible is True by Default, use False want to have the Name hidden to users
.Names(kRptDealer).Comment = "Name use for Dealer\Customer picture in report"
End With
.Range(kRptDealer).Columns.AutoFit
End With
End Sub
Following the above preparations , now we can create the Shape that will be linked to the Output Range named RptDealer
. Select at cell A5
in worksheet Report
and follow the instructions for Dynamically display cell range contents in a picture or if you prefer use the code below to add and format the linked Shape
.
Sub Shape_DealerPicture_Set(rCll As Range)
Const kShpName As String = "_ShpDealer"
Dim rSrc As Range
Dim shpTrg As Shape
Rem Delete Dealer Shape if present and set Dealer Source Range
On Error Resume Next
rCll.Worksheet.Shapes(kShpName).Delete
On Error GoTo 0
Rem Set Dealer Source Range
Set rSrc = ThisWorkbook.Names(kRptDealer).RefersToRange
Rem Target Cell Settings & Add Picture Shape
With rCll
.ClearContents
If .RowHeight < rSrc.RowHeight Then .RowHeight = rSrc.RowHeight
If .ColumnWidth < rSrc.Cells(1).ColumnWidth + rSrc.Cells(2).ColumnWidth Then _
.ColumnWidth = rSrc.Cells(1).ColumnWidth + rSrc.Cells(2).ColumnWidth
rSrc.CopyPicture
.PasteSpecial
Selection.Formula = rSrc.Address(External:=1)
Selection.PrintObject = msoTrue
Application.CutCopyMode = False
Application.Goto .Cells(1)
Set shpTrg = .Worksheet.Shapes(.Worksheet.Shapes.Count)
End With
Rem Shape Settings
With shpTrg
On Error Resume Next
.Name = "_ShpDealer"
On Error GoTo 0
.Locked = msoFalse
.Fill.Visible = msoFalse
.Line.Visible = msoFalse
.ScaleHeight 1, msoTrue
.ScaleWidth 1, msoTrue
.LockAspectRatio = msoTrue
.Placement = xlMoveAndSize
.Locked = msoTrue
End With
End Sub
The code above can be called using this procedure:
Sub DealerPicture_Apply()
Dim rCll As Range
Set rCll = ThisWorkbook.Sheets("Report").Cells(5, 1)
Call Shape_DealerPicture_Set(rCll)
End Sub
The end result is a Picture that behaves like a formula as it is linked to the Output Range containing the formula and format desired (see Fig.4)
Fig.4