2012-06-26 169 views
2

有沒有辦法讓Excel圖表在隱藏的行中繪製數據,但不是在隱藏的列中?我已經知道如何使用「選擇數據」選項,右鍵單擊圖表時,轉到「隱藏和空單元格」選項,該選項提供了「隱藏行和列顯示數據」的選項。然而,我找不到一種方式來顯示隱藏行中的數據,而沒有在隱藏列中顯示數據,並希望有人能夠提出一個VBA解決方案。如何在Excel圖表中顯示隱藏行,但不顯示隱藏列?

非常感謝,

傑夫

+0

這裏可能有更好的方法,但是您可以從頭創建圖表並循環遍歷數據範圍中的列,選擇僅添加可見列。或者,您可以選中「隱藏行和列中顯示數據」,並使用VBA遍歷系列值來查看列是否隱藏,如果是,則從圖表中刪除該系列。 –

+0

謝謝Scott。這絕對是值得一試的。不過,我並不太確定如何創建這樣的代碼。是否有一些通用代碼可以爲您提供的第二個選項提供,假設列D:AE全部填充,並且在不同情況下,不同列將被隱藏? –

回答

0

我只是灌輸這件事給你。作爲一個警告,使用圖表可能會非常棘手。此代碼只適用於某些類型的圖表,並且您可能需要進行一些調整才能使用您的特定數據集。

Option Explicit 

Sub RemoveHiddenColumns() 

Dim myChart As ChartObject 
Set myChart = ActiveSheet.ChartObjects("Chart 1") 'place in here whichever chart you need to reference, asssumes the chart is on the activesheet 

myChart.Activate 'first activate the chart 

Dim i As Integer 

For i = 1 To ActiveChart.SeriesCollection.Count 'loop through each series 

    Dim strText As String, strCol As String, strSht As String, intCol As Integer 

    strText = Split(ActiveChart.SeriesCollection(i).Formula, ",")(2) 'extract sheet name and column of series 
    strSht = Split(strText, "!")(0) 'get sheet name of series 
    strCol = Split(strText, "!")(1) 'get column range of series 

    Dim wks As Worksheet 
    Set wks = Worksheets(strSht) 

    If wks.Range(strCol).EntireColumn.Hidden = True Then 'if the column is hidden 
     ActiveChart.SeriesCollection(i).Delete 'remove the series 
    End If 

Next 


End Sub 
+0

非常感謝。這是一個巨大的幫助,我真的很感謝你的努力! –

+0

這是一個很好的問題。我喜歡解決它:) –