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:" and not the Customer name.
Example:
Dealer: Josh
I have tried
Cells(5, 1).Characters(1, 7).Font.Bold = True
But it only seems to work on non referenced cells only. How could I get this to work on a referenced cell?
Instead of referencing you could simply obtain the cells and place it in a variable, and basically append it. From here you can use the .font.bold functionality to bold a specific part. Lets say on page 2, you have "Dealer: " in cell a1, and "Josh" in b1. Here is an example how it could be done:
Requirements:
My understanding is that the OP needs to have in cell
A5
the result of the formula="Dealer: " & CustomerName
showing theDealer:
part in bold characters. Now, what it is not clear, is the nature of theCustomerName
part of the formula. This solution assumes it corresponds to aDefined 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 NameCustomerName
has a workbook scope and is hidden. Located atA5
is the formula="Dealer: " & CustomerName
The Fig.1 shows the report with the data forCustomer 1
.Fig.1
Now if we change the Customer Number in cell
E3
to4
, the report will show the data of the customer selected; without running any VBA procedure. Unfortunately as the cellA5
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 forCustomer 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
inA5
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” atB2:C3
(see Fig.3). Enter inB2
Dealer:
and inC2
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 theDefined Name
calledRptDealer
.Following the above preparations , now we can create the Shape that will be linked to the Output Range named
RptDealer
. Select at cellA5
in worksheetReport
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 linkedShape
.The code above can be called using this procedure:
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
As they already told you can't format a partial cell value if this latter derives from a formula/function in that same cell
However there may be some workarounds that may suit your needs
Unluckily I can't actually grasp your real environment so here are some blind shots:
1st "environment"
You have a VBA code running that at some point writes in a cell like:
and you want to have the
"Dealer:"
part boldthe most straightforward way would then be
but you could also use the
Worksheet_Change()
event handler as follows:your VBA code is only
while placing the following code in the relevant worksheet code pane:
where
On Error GoTo ExitSub
andExitSub: Application.EnableEvents = True
shouldn't be necessary, but I left them as a good practice whenApplication.EnableEvents = False
id used2nd "environment"
You have cell(s) in your excel worksheet containing a formula, like:
where
CustomerName
is a named rangeand your VBA code is going to modify the content of that named range
in this case the
Worksheet_Change()
sub would be triggered by the named range value change and not by the cell containing the formulaso I'd go checking if the changed cell is a
valid
one (i.e. corresponding to awell known
named range) and then go with a sub that scans a predefined range and finds and format all cells with formulas that use that `named range, like follows (comments should help you):You can use the below function to bold some input text within a formula
So in your cell you can now type =Bold("Dealer: ")&CustomerName
To be precise – this will only embolden alphabetical characters (a to z and A to Z) all others will be left unchanged. I haven’t tested it on different platforms, but seems to work on mine. May not be supported for all fonts.
Edit:
Have made an effort to refactor the above to show how it works, rather than have it peppered with magical numbers.
For reference on the unicode characters used see here http://www.fileformat.info/info/unicode/block/mathematical_alphanumeric_symbols/list.htm
The wikipedia page on UTF16 shows the algorithm for converting from Unicode to two UTF16 code points
https://en.wikipedia.org/wiki/UTF-16