要求:
我的理解是,OP 需要在单元格 A5 中包含公式 ="Dealer: " & CustomerName 的结果,以粗体字符显示 Dealer: 部分。
现在,尚不清楚的是公式中CustomerName 部分的性质。此解决方案假定它对应于具有工作簿范围的Defined Name(如果不同,请告诉我)。
我假设使用公式而不是直接编写公式的结果并使用 VBA 程序格式化A5 单元格的原因是允许用户仅通过计算更改来查看来自不同客户的数据工作簿,而不是通过运行 VBA 过程。
假设我们在名为Report 的工作表中有以下数据,定义名称CustomerName 具有工作簿范围并且被隐藏。
位于A5 是公式="Dealer: " & CustomerName
Fig.1 显示了带有Customer 1 数据的报告。
图1
现在如果我们将单元格E3中的客户编号更改为4,报告将显示所选客户的数据;无需运行任何 VBA 程序。不幸的是,由于单元格A5 包含一个公式,它的内容字体不能部分格式化为以粗体字符显示“经销商:”。 Fig.2 显示了带有Customer 4 数据的报告。
图2
这里提出的解决方案是Dynamically display the contents of a cell or range in a graphic object
要实现此解决方案,我们需要重新创建所需的输出范围并在 A5 中添加一个 Shape,其中将包含指向输出范围的链接。
假设我们不希望在报告所在的同一工作表中看到此输出范围,并记住 输出范围单元格不能隐藏;让我们在另一个名为“客户数据”的工作表中创建此输出范围,地址为B2:C3(见图 3)。输入B2 Dealer: 和C2 输入公式=Customer Name 然后根据需要格式化每个单元格(B2 字体粗体,C3 可以有不同的字体类型,如果你喜欢 - 让我们应用斜体字体这个样本)。确保范围具有适当的宽度,以免文本溢出单元格。
图3
建议为此范围创建Defined Name。下面的代码创建了名为RptDealer 的Defined Name。
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
按照上述准备工作,现在我们可以创建将链接到名为RptDealer 的输出范围的形状。在工作表Report 中的单元格A5 处选择并按照Dynamically display cell range contents in a picture 的说明进行操作,或者如果您更喜欢使用下面的代码来添加和格式化链接的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
上面的代码可以用这个过程调用:
Sub DealerPicture_Apply()
Dim rCll As Range
Set rCll = ThisWorkbook.Sheets("Report").Cells(5, 1)
Call Shape_DealerPicture_Set(rCll)
End Sub
最终结果是一个行为类似于公式的图片,因为它链接到包含所需公式和格式的输出范围(见图 4)
图4