【问题标题】:How to draw a dashed line between two cells if a condition is met in Excel如果在 Excel 中满足条件,如何在两个单元格之间绘制虚线
【发布时间】:2016-08-10 12:30:15
【问题描述】:

我的 Excel 文件如下所示:

如果满足特定条件,我将如何在单元格 (xxxx) 和单元格 (aaa) 之间绘制一条红色虚线?

【问题讨论】:

    标签: excel macros excel-formula vba


    【解决方案1】:

    这是一个示例,说明如何在两个 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
    

    例子:

    【讨论】:

      【解决方案2】:

      对公式使用条件格式并设置边框底部的格式。 您不能格式化单元格的内部,只能格式化外部,因此请记住这一点。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-12-05
        相关资源
        最近更新 更多