2017-11-27 4 views
0

私は、入力されたデータを自動的にコピーし、新しいレコードをテーブルに追加し、そのレコードのセルを再現する小さなコントロールパネルを作成しました。挿入のために、マクロはテーブルの最後の空行を検索し、そこにデータを挿入します。Excelでレコード/セルをグラフにリンクするにはどうすればよいですか? FullSeriesCollection

ここで、別のワークシートのグラフに同じレコードをテーブルのレコードとリンクして追加したいとします。

残念ながら、私のコードは期待どおりに動作せず、理由はわかりません。あなたが私を助けることを願っています!

Sub DatensatzAnlegen() 

'Find next clear row 
Range("A6:M6").Select 
Selection.Copy 
CurrentRow = 13 
Do Until Range("A" & CurrentRow) = "" 
    CurrentRow = CurrentRow + 1 
Loop 
Cells(CurrentRow, 1).Activate 
ActiveSheet.Paste 
Range("E9:M9").Select 
Selection.Copy 
Cells(CurrentRow, 14).Activate 
ActiveSheet.Paste 

'Recolor cell of the new record 
Cells(CurrentRow, 1).Select 
    With Selection.Interior 
    .Pattern = xlSolid 
    .PatternColorIndex = xlAutomatic 
    .ThemeColor = xlThemeColorAccent1 
    .TintAndShade = 0.799981688894314 
    .PatternTintAndShade = 0 
End With 

'Link data with chart 
Sheets("Diagramm").ChartObjects("DiagrammA").Activate 
ActiveChart.SeriesCollection.NewSeries 
ActiveChart.FullSeriesCollection.Name = Sheets("Übersicht").Cells(CurrentRow, 1) 'DOES NOT WORK 
ActiveChart.FullSeriesCollection.XValues = Sheets("Übersicht").Cells(CurrentRow, 2) 'DOES NOT WORK 
ActiveChart.FullSeriesCollection.Values = Sheets("Übersicht").Cells(CurrentRow, 3) 'DOES NOT WORK 

'Clear control panel 
ActiveSheets.Übersicht 
Range("A6:M6").Select 
Selection.ClearContents 
Range("E9:M9").Select 
Selection.ClearContents 
End Sub 

答えて

0

以下のコードの断片は"Diagramm"ワークシートでの​​グラフにデータが付加された行を追加します。

:あなたはSelectionSelectに、不要なあまりにも多くを持っているとActiveSheet。 ... With Datawsなどのコードのコメント内の

詳細な説明を:

Set ChtObj = Chtws.ChartObjects("DiagrammA") 

With文を使用して:その代わり、ChartObjectを設定するように、完全修飾オブジェクトを使用します。あなたの速い応答のための

コード

Option Explicit 

Sub DatensatzAnlegen() 

Dim CurrentRow As Long 
Dim Dataws As Worksheet 
Dim Chtws As Worksheet 
Dim ChtObj As ChartObject 
Dim Ser As Series 

' set the worksheet with the data for the chart 
Set Dataws = ThisWorkbook.Sheets("Übersicht") 

' set the worksheet where the chart is located 
Set Chtws = ThisWorkbook.Sheets("Diagramm") 

With Dataws ' always qualify all your Range and cells objects 
    'Find next clear row 
    CurrentRow = .Range("A13").End(xlDown).Row + 1 
    .Range("A6:M6").Copy Destination:=.Range("A" & CurrentRow) 

    ' the same should apply for the block below 
    ' .Range("E9:M9").Copy Destination:=.Range("A" & CurrentRow + 1) 

    'Recolor cell of the new record 
    With .Cells(CurrentRow, 1).Interior 
     .Pattern = xlSolid 
     .PatternColorIndex = xlAutomatic 
     .ThemeColor = xlThemeColorAccent1 
     .TintAndShade = 0.799981688894314 
     .PatternTintAndShade = 0 
    End With 
End With 

' set the chart object 
Set ChtObj = Chtws.ChartObjects("DiagrammA") 

'Link data with chart 
With ChtObj 
    Set Ser = .Chart.SeriesCollection.NewSeries ' add a new series to chart 

    With Ser 
     .Name = "=" & Dataws.Cells(CurrentRow, 1).Address(False, False, xlA1, xlExternal) 
     .XValues = "=" & Dataws.Cells(CurrentRow, 2).Address(False, False, xlA1, xlExternal) 
     .Values = "=" & Dataws.Cells(CurrentRow, 3).Address(False, False, xlA1, xlExternal) 
    End With 
End With 

' rest of your code 

End Sub 
+0

ありがとう!私は、テーブルに新しいレコードを追加しようとするたびに、A14のデータが上書きされるという新たな問題があると考えています。 .Range( "A6:M6")。コピー先:=。範囲( "A"&CurrentRow)は機能しません。私は1004ランタイムエラーが発生します。 – RuilGaga

関連する問題