代码之家  ›  专栏  ›  技术社区  ›  bishwarup990

VBA从图形的x轴隐藏空列名

  •  0
  • bishwarup990  · 技术社区  · 4 年前

    Sub Chart()
     Dim rng As Range, cht As ChartObject, LstRow As Long, i As Long
     Dim sh As Worksheet, lastCol As Long, lastC As Long, ColAddr As String
      Set sh = ActiveSheet      'use here your sheet
       LstRow = sh.Cells(Cells.Rows.Count, 1).End(xlUp).Row 'last row in A:A
       'Last column on the third row
       For i = 2 To LstRow
            lastC = sh.Cells(i, Cells.Columns.Count).End(xlToLeft).Column
            If lastC > lastCol Then lastCol = lastC
       Next i
       ColAddr = sh.Cells(1, lastCol).Address 'last column address (on first row)
    
       Set rng = sh.Range(sh.Range("E2"), sh.Cells(LstRow, lastCol))
       Set cht = ActiveSheet.ChartObjects.Add(Left:=100, Top:=80, Width:=350, _
                                                                    Height:=250)
    
       cht.chart.SetSourceData Source:=rng
       cht.chart.ChartType = xlColumnClustered
        sh.ChartObjects(sh.ChartObjects.Count).chart.SeriesCollection(1).XValues = _
                                                "='" & sh.name & "'!$F$1:" & ColAddr
    End Sub
    

    enter image description here

    但是我只想显示x轴标签中有数据的列。所以基本上根据我的需要X轴标签应该只包含像201520162017201820192023这样的标签,而不是2020202022。

    0 回复  |  直到 4 年前