2017-04-24 35 views
0

我想使用VBA創建excel圖表。但是,x軸始終顯示行號而不是行中的值。這是我現在的代碼。難道我做錯了什麼? y軸是正確的,但VBA圖表x軸:爲什麼它沒有行號。而不是價值?

If CheckBox4.Value = False Then 
If CheckBox1.Value = True Then 'S11 S22 
    ActiveSheet.Shapes.AddChart.Select 
    With ActiveChart 
     .HasTitle = True 
     .ChartTitle.Text = "S11 & S22" 
     .ChartType = xlXYScatterLinesNoMarkers 
     .Axes(xlCategory, xlPrimary).HasTitle = True 
     .Axes(xlCategory, xlPrimary).AxisTitle.Characters.Text = "Frequency/GHz" 
     .Axes(xlValue, xlPrimary).HasTitle = True 
     .Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = "/dB" 
     .SetSourceData Source:=Range("B2:T5") 

     Do Until .SeriesCollection.Count = 0 
      .SeriesCollection(1).Delete 
     Loop 

     .SeriesCollection.NewSeries 
     .SeriesCollection(1).Name = "S11" 
     .SeriesCollection(1).XValues = Range("A9:A" & j) 
     .SeriesCollection(1).Values = Range("B9:B" & j) 
      .SetElement (msoElementPrimaryCategoryGridLinesMinorMajor) 
      .SetElement (msoElementPrimaryValueGridLinesMinorMajor) 
     ' .SetElement (msoElementPrimaryCategoryAxisLogScale) 
     .SeriesCollection.NewSeries 
     .SeriesCollection(2).Name = "S22" 
     .SeriesCollection(2).XValues = Range("A9:A" & j) 
     .SeriesCollection(2).Values = Range("H9:H" & j) 
     With .Parent 
      .Height = 300 
      .Width = 500 
      .Top = 50 
      .Left = 600 
     End With 
    End With 
End If 

提前謝謝你們!

回答

1

嘗試replcaing

.SeriesCollection(1).XValues = Range("A9:A" & j) 

有:

.SeriesCollection(1).XValues = "=" & Range("A9:A" & j).Address(False, False, xlA1, xlExternal) 
+0

嗯,它仍然沒有工作!當在調試模式下運行X軸時,X軸仍然反映行號(在我的情況下爲9-200),而不是單元格內的值 – Jane

+0

,「j」的值是多少?如果你添加了'Debug.Print Range(「A9:A」&j).Address(False,False,xlA1,xlExternal)'你會得到什麼? –

+0

我找到了解決辦法!發現第j行的最後一個值是「END」,它不是數字,因此圖表是根據行號繪製的。我用j = j -1,它工作!無論如何,非常感謝您的幫助! ^^ – Jane

相關問題