Excel如何在单元格内显示图表(折线图)

2017-07-11 17:51:00
zstmtony
原创
760

Excel的图表是非常强大的,但很少有人在单元格内放置图表,而且图表对象也是比较重。但不用图表对象,能否实现折线图呢

就有这些Excel强人,硬生生地给实现了。

看看他们是如何做到的


作者:Excel小子


blob.png

操作动画:

Excel小子2QQ图片20161109212345.gif


实现代码:

Sub 宏3()

'

' 宏3 宏

'


'

Dim sh As Shape, t, q

Dim rg As Range, r As Range, rr As Range, i

For Each sh In ActiveSheet.Shapes

    sh.Delete

Next sh


For t = 1 To Range("a1").CurrentRegion.Columns.Count

    For q = 1 To Range("a1").CurrentRegion.Rows.Count

    Set rg = Cells(q, t)

If Len(rg.Value) > 0 Then


i = i + 1

If i = 1 Then

    Set r = rg

Else

    

      ActiveSheet.Shapes.AddConnector(msoConnectorStraight, r.Left + r.Width / 2, r.Top + r.Height / 2, rg.Left + rg.Width / 2, _

        rg.Top + rg.Height / 2).Select

        With Selection.ShapeRange.Line

        .Visible = msoTrue

        .ForeColor.RGB = RGB(255, 0, 0)

        .Transparency = 0

    End With

    With Selection.ShapeRange.Line

        .Visible = msoTrue

        .Weight = 2.25

    End With

    

        Set r = rg

        End If

     End If

Next q

Next t


End Sub


作者:江苏大侠

blob.png


演示动画:

江苏大侠QQ图片20161109211721.gif


实现 代码:

Function s(rng As Range)

    On Error Resume Next

    Set ce = rng(rng.Count).Offset(0, 1)

    For Each Shp In ce.Worksheet.Shapes

        ce.Worksheet.Shapes(ce.Address(, , xlR1C1) & "shape").Delete

    Next

    c = rng.Count

    w = ce.Width - 0.2

    h = ce.Height - 0.2

    ma = Application.Max(rng)

    With ce.Worksheet.Shapes

        x1 = ce.Left

        y1 = ce.Top + ce.Height - rng(1) / ma * h

        For i = 2 To c

            x2 = x1 + w / (c - 1)

            y2 = ce.Top + ce.Height - rng(i) / ma * h

            Set Shp = .AddLine(x1, y1, x2, y2)

            Shp.Name = ce.Address(, , xlR1C1) & "shape"

            x1 = x2

            y1 = y2

        Next

    End With

    s = ""

End Function


分享