如何使用vba-excel组合形状?

我想结合基于范围select的形状。 像这张照片 可能吗? 在这里我附上了图片:

结合图片 在这里,我附上我的代码

Sub cohabitationButton_Click() '''''split range Dim s() As String Dim txt As String Dim i As Long s = Split(Selection.Address(False, False), ",") For i = LBound(s) To UBound(s) Dim r As range: Set r = range(s(i)) With r l = .Left - 5 t = .Top - 5 w = .Width + 10 h = .Height + 10 End With ShapeName = "ex" With ActiveSheet.Shapes.AddShape(msoShapeFlowchartTerminator, l, t, w, h) .Fill.Visible = msoFalse .Line.Weight = 1 .Line.DashStyle = msoLineDash .Line.ForeColor.RGB = BASICCOLOR .Name = ShapeName End With Next i End Sub 

Excel中不可能组合形状。 但是这里是一个例子,你可以在你的select中绘制组合的边框。 这可能是你的select。

所以select你的例子,我们最终得到这个:
在这里输入图像说明

 Sub DrawCombinedBordersOnly() '''''split range Dim s() As String Dim txt As String Dim i As Long Dim rngOverlappings As Range 'Draw borders around all selected ranges Selection.BorderAround LineStyle:=xlDot, Weight:=xlThin s = Split(Selection.Address(False, False), ",") For i = LBound(s) To UBound(s) Dim r As Range: Set r = Range(s(i)) Dim j As Long For j = LBound(s) To UBound(s) 'find overlapping areas If i <> j And Not Application.Intersect(r, Range(s(j))) Is Nothing Then If rngOverlappings Is Nothing Then Set rngOverlappings = Application.Intersect(r, Range(s(j))) Else Set rngOverlappings = Union(rngOverlappings, Application.Intersect(r, Range(s(j)))) End If End If Next j Next i ' remove borders from overlappings If Not rngOverlappings Is Nothing Then rngOverlappings.Borders.LineStyle = xlNone End If End Sub