2016-01-28 64 views
0

我期待格式化Excel的2010多個選擇圖表使用VBA。下面的代碼在只選擇一個圖表時工作,但如果選擇多個圖表,則會出現「運行時錯誤」91「對象變量或未設置塊變量」。任何想法如何運行宏數選擇圖表?VBA:格式化多個選定圖表

Sub ChartFormat5_Click() 


''Adjust chart area 

'Size 
Selection.Width = 631.9 
Selection.Height = 290.1 

'Border 
With Selection.Format.Line 
    .Visible = msoTrue 
    .ForeColor.ObjectThemeColor = msoThemeColorText1 
    .ForeColor.TintAndShade = 0 
    .ForeColor.Brightness = 0 
    .Transparency = 0 
    .Weight = 1 
    .DashStyle = msoLineSolid 
End With 

'Font 
With Selection.Format.TextFrame2.TextRange.Font 
    .Name = "Calibri" 
    .Size = 10 
    .Fill.Visible = msoTrue 
    .Fill.ForeColor.ObjectThemeColor = msoThemeColorText1 
    .Fill.ForeColor.TintAndShade = 0 
    .Fill.ForeColor.Brightness = 0 
    .Fill.Transparency = 0 
    .Fill.Solid 
End With 


End Sub 

謝謝!

回答

1

這將處理活動圖表或所有選定的圖表。第一個例程決定要處理的內容(活動圖表或選定圖表)以及每個第二個過程。

Sub FormatCharts() 
    Dim obj As Object 

    If Not ActiveChart Is Nothing Then 
    FormatOneChart ActiveChart 
    Else 
    For Each obj In Selection 
     If TypeName(obj) = "ChartObject" Then 
     FormatOneChart obj.Chart 
     End If 
    Next 
    End If 
End Sub 

Sub FormatOneChart(cht As Chart) 
    ' do all your formatting here, based on cht not on ActiveChart 
End Sub 

不要選擇圖表的某些部分,只是完全參照它們。取而代之的

ActiveChart.ChartArea.Select 
With Selection.Format.Line 

使用本

With cht.ChartArea.Format.Line 

注:這是VBA: Formatting Multiple Selected Charts (Chart, Plot, Legend, etc.)

1

經過一番審訊正錯誤,我想通了,如何讓你只有一個或多個選擇的圖表它的工作。這很簡單,但是當我測試它時,它就很有用。

注意,我打破了實際圖表區格式到一個獨立的子。

Sub ChartFormat5_Click() 

    Select Case TypeName(Selection) 

     Case Is = "ChartArea" `only 1 selected 

      FormatChart Selection 

     Case Is = "DrawingObjects" 'more than 1 selected 

      Dim cht As ChartObject 
      For Each cht In Selection 
       FormatChart cht.Chart.ChartArea 
      Next 

    End Select 

End Sub 

Sub FormatChart(chtArea As ChartArea) 

With chtArea 

    'size 
    .Width = 631.9 
    .Height = 290.1 

    With .Format 
     'Border 
     With .Line 
      .Visible = msoTrue 
      .ForeColor.ObjectThemeColor = msoThemeColorText1 
      .ForeColor.TintAndShade = 0 
      .ForeColor.Brightness = 0 
      .Transparency = 0 
      .Weight = 1 
      .DashStyle = msoLineSolid 
     End With 

     'Font 
     With .TextFrame2.TextRange.Font 
      .Name = "Calibri" 
      .Size = 10 
      With .Fill 
       .Visible = msoTrue 
       With .ForeColor 
        .ObjectThemeColor = msoThemeColorText1 
        .TintAndShade = 0 
        .Brightness = 0 
       End With 
       .Transparency = 0 
       .Solid 
      End With 
     End With 
    End With 
End With 

End Sub 
+0

斯科特重複 - 這個偉大的工程!但是,現在我遇到了麻煩,如果我只是試圖運行一個選定圖形的宏(即5個圖中的1個)...感謝您的幫助! –

+0

@JKo_FinanceUse - 你有沒有看到我的評論在約''變化ws.ChartObjects代碼爲「Selection' –

+0

http://stackoverflow.com/users/1569536/scott-holtzman - 是啊,所以我換成了與選擇ws.ChartObjects但是當我嘗試運行單個選定圖表時,出現運行時錯誤'438'對象不支持此屬性或方法。實際上,我收到錯誤消息的行是「For Each cht In Selection」。 。! –

0

海蘭嘗試:

Sub ChartFormat5_Click_v02() 

    For i = 1 To Application.Sheets.Count 

    Application.Sheets(i).Activate 

    For j = 1 To ActiveSheet.ChartObjects.Count 

     ActiveSheet.ChartObjects(j).Activate 


     'your code here 

    Next j 

    Next i 

End Sub 
+0

安德烈 - 它看起來是隻選擇工作表中的一個圖表...我想自動格式化只有那些我選擇 –

+0

其實,這個腳本格式中的所有表的所有圖表。如果你只想格式化選擇的圖表,@Scott Holtzman的答案是更好的選擇。 –