【发布时间】:2016-08-10 12:30:15
【问题描述】:
【问题讨论】:
标签: excel macros excel-formula vba
【问题讨论】:
标签: excel macros excel-formula vba
这是一个示例,说明如何在两个 Ranges 之间画一条线并对其进行格式化,例如红色和虚线。您可以根据应用程序所需的任何条件调整测试代码以选择两个Ranges。注意测试代码假设rng1在左边,rng2在右边:
Option Explicit
Sub Test()
Dim rng1 As Range
Dim rng2 As Range
'assume L->R line where rng1 is left and rng2 is right
'you can set these ranges based on any logic you have
Set rng1 = Sheet1.Range("B2")
Set rng2 = Sheet1.Range("G5")
DrawLineBetweenCells rng1, rng2
End Sub
Sub DrawLineBetweenCells(rng1 As Range, rng2 As Range)
Dim sngBeginX As Single
Dim sngBeginY As Single
Dim sngEndX As Single
Dim sngEndY As Single
Dim ws As Worksheet
Dim shp As Shape
'get worksheet of source range (assume target is same sheet...)
Set ws = rng1.Parent
'right middle edge of start cell
sngBeginX = rng1.Left + rng1.Width
sngBeginY = rng1.Top + (rng1.Height / 2)
'left middle edge of end cell
sngEndX = rng2.Left
sngEndY = rng2.Top + (rng1.Height / 2)
'draw line
Set shp = ws.Shapes.AddLine(sngBeginX, sngBeginY, sngEndX, sngEndY)
'format line
shp.Line.ForeColor.RGB = RGB(255, 0, 0)
shp.Line.EndArrowheadStyle = msoArrowheadDiamond
shp.Line.DashStyle = msoLineDash
End Sub
例子:
【讨论】:
对公式使用条件格式并设置边框底部的格式。 您不能格式化单元格的内部,只能格式化外部,因此请记住这一点。
【讨论】: