Supportnet Computer
Planet of Tech

Supportnet / Forum / Tabellenkalkulation

Excel Diagramme mit Makros automatisch formatieren





Frage

Hallo zusammen, ich habe in Excel VBA mittels "Makro aufzeichnen" versucht, ein Makro zu erstellen, was neu erstellte Diagramme immer auf die gleiche Art und Weise formatieren soll, damit man sich die mühsame Kleinstarbeit sparen kann - auf Knopfdruck. Der Button ist bereits integriert und funktioniert. ;-) Leider das dazugehörige Makro aber nicht. Es sagt "Objektvariable oder Blockvariable nicht festgelegt". Gibt es einen Hinweis, woran das liegen kann bzw. einen eleganten Weg, die Formatierung vorzunehmen? Merci vielmals und VG

Antwort 1 von JoeKe

Moin youhoo,

es wäre einfacher dir zu helfen, wenn du dein Makro hier mal posten könntest.

MfG

JöKe

Antwort 2 von youhoo

OK, eigentlich wollte ich euch den Rattenschwanz sparen, aber es geht wohl nicht anders... ;-)

Bittesehr:

Private Sub onActEYChart()
ActiveChart.Axes(xlValue).Select
Selection.TickLabels.AutoScaleFont = True
With Selection.TickLabels.Font
.Name = "EY Gothic Comp Book"
.FontStyle = "Standard"
.Size = 10
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleNone
.ColorIndex = xlAutomatic
.Background = xlAutomatic
End With
ActiveChart.Axes(xlCategory).Select
Selection.TickLabels.AutoScaleFont = True
With Selection.TickLabels.Font
.Name = "EY Gothic Comp Book"
.FontStyle = "Standard"
.Size = 10
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleNone
.ColorIndex = xlAutomatic
.Background = xlAutomatic
End With
ActiveChart.PlotArea.Select
With Selection.Border
.ColorIndex = 16
.Weight = xlThin
.LineStyle = xlContinuous
End With
With Selection.Interior
.ColorIndex = 2
.PatternColorIndex = 1
.Pattern = xlSolid
End With
ActiveChart.Axes(xlValue).MajorGridlines.Select
With Selection.Border
.ColorIndex = 23
.Weight = xlHairline
.LineStyle = xlContinuous
End With
With Selection.Border
.ColorIndex = 48
.Weight = xlHairline
.LineStyle = xlContinuous
End With
ActiveChart.Legend.Select
ActiveChart.Legend.LegendEntries(2).Select
ActiveChart.ChartArea.Select
ActiveChart.Legend.Select
Selection.AutoScaleFont = True
With Selection.Font
.Name = "EY Gothic Comp Book"
.FontStyle = "Standard"
.Size = 10
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleNone
.ColorIndex = xlAutomatic
.Background = xlAutomatic
End With
Selection.Position = xlBottom
ActiveChart.ChartArea.Select
ActiveChart.Legend.Select
With Selection.Border
.Weight = xlHairline
.LineStyle = xlNone
End With
Selection.Shadow = False
Selection.Interior.ColorIndex = xlAutomatic
ActiveChart.Axes(xlCategory).Select
ActiveChart.ChartArea.Select
ActiveWindow.Visible = False
Windows("To Do Databook.xls").Activate
Range("G11").Select
ActiveSheet.ChartObjects("Diagramm 1").Activate
ActiveChart.ChartArea.Select
End Sub

Antwort 3 von JoeKe

Hallo youhoo,

so wie ich das im Moment sehe müsste dein Makro eigentlich laufen.
Das einzige was mich ein wenig irritiert sind diese Zeilen:
Zitat:
Windows("To Do Databook.xls").Activate
Range("G11").Select
ActiveSheet.ChartObjects("Diagramm 1").Activate
ActiveChart.ChartArea.Select


Und warum blendest du das Fenster am Ende aus?
Zitat:
ActiveWindow.Visible = False


Wo startest du dein Makro? Im Diagramm oder aus einem Tabellenblatt herraus?

Wenn die Fehlermeldung erscheint und du gehst auf debuggen, welche Zeile ist dann markiert?

MfG
JöKe

Antwort 4 von youhoo

Das hilft doch schon viel weiter.
Danke!

Ich habe die Zeilen mal rausgenommen, weil ich schätze, dass da noch Aktionen reingerutscht sind, die ich eigentlich nicht mit ins Makro aufnehmen wollte.

Beim Debuggen bleibe ich schon an der ersten Zeile hängen, also bei ActiveChart.Axes(xlValue).Select.

Scheint ihm nicht zu passen. Eine Idee? Danke!

Antwort 5 von coros

Hi youhoo ,

das in der ersten Zeile bereits ein Fehler auftritt, liegt daran, dass es sich bei dem Eintrag "xlValue" um eine Variable handelt, die vorher erst einmal erzeugt werden muss. Da das in Deinem Makro nicht geschieht, muss anstelle der Variablen dann der Name des Diagramms stehen oder Du erzeugst vorher noch diese Variable entweder über eine Schleife oder durch nur eine Wertzuweisung . Ansonsten kommt es zu besagter Fehlermeldung.

MfG,
coros
Da hier der einzige Lohn für die Helfer eine Rückmeldung ist, wäre es nett, wenn Du ein
Feedback abgeben könntest, ob der Lösungsvorschlag Dein Problem gelöst hat.

Antwort 6 von youhoo

Äh, ja. Klingt logisch.
Da du ja jetzt weisst, worum es sich bei dem Objekt dreht, was würdest du denn vorschlagen, wie ich die Variable am besten definiere?
Ich schätze, mit der Variable xlCategory wird es sich weiter unten um das selbe Problem handeln??

MERCI!

Antwort 7 von coros

Hi youhoo,

vergiss das mit der Variablen, ich habe mir das mal eben angesehen, xlValue gibt die Achse an. Damit ist das korrekt, was dort steht. Nochmal eine Frage, Du schreibst, Du hast Dir das Makro aufgezeichnet. Hast Du versucht das Makro etwas zu kürzen und eventuell etwas herausgelöscht?

MfG,
coros
Da hier der einzige Lohn für die Helfer eine Rückmeldung ist, wäre es nett, wenn Du ein
Feedback abgeben könntest, ob der Lösungsvorschlag Dein Problem gelöst hat.

Antwort 8 von JoeKe

Hi youhoo,

startest du dein Makro aus einem Tabellenblat oder aus dem Diagrammblatt?
Wenn du es aus dem Tabellenblatt startest kommt es auch zu der Fehlermeldung.


MfG

JöKe

Antwort 9 von youhoo

OK, ich habs nochmal neu sauber aufgezeichnet, nicht dass da irgendwas falsch gelaufen ist. Das sieht nun so aus:

Beim Buttondrücken sagt er wieder das mit der Objektvariablen. Wenn ich das Makro so aufrufe, dann meldet er aus dem Debug Modus, dass er den Select Befehl aus der 1. Zeile nicht ausführen konnte.

Private Sub BlaBla()
ActiveChart.Axes(xlValue).AxisTitle.Select
Selection.AutoScaleFont = True
With Selection.Font
.Name = "EY Gothic Comp Demi"
.FontStyle = "Standard"
.Size = 10
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleNone
.ColorIndex = xlAutomatic
.Background = xlAutomatic
End With
Selection.Font.Bold = False
ActiveChart.Axes(xlValue).Select
Selection.TickLabels.AutoScaleFont = True
With Selection.TickLabels.Font
.Name = "EY Gothic Comp Book"
.FontStyle = "Standard"
.Size = 10
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleNone
.ColorIndex = xlAutomatic
.Background = xlAutomatic
End With
ActiveChart.Axes(xlCategory).Select
Selection.TickLabels.AutoScaleFont = True
With Selection.TickLabels.Font
.Name = "EY Gothic Comp Book"
.FontStyle = "Standard"
.Size = 10
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleNone
.ColorIndex = xlAutomatic
.Background = xlAutomatic
End With
ActiveChart.PlotArea.Select
With Selection.Border
.ColorIndex = 16
.Weight = xlThin
.LineStyle = xlContinuous
End With
Selection.Interior.ColorIndex = xlNone
ActiveChart.Axes(xlValue).MajorGridlines.Select
With Selection.Border
.ColorIndex = 48
.Weight = xlHairline
.LineStyle = xlContinuous
End With
ActiveChart.Legend.Select
With Selection.Border
.Weight = xlHairline
.LineStyle = xlNone
End With
Selection.Shadow = False
Selection.Interior.ColorIndex = xlAutomatic
Selection.AutoScaleFont = True
With Selection.Font
.Name = "EY Gothic Comp Book"
.FontStyle = "Standard"
.Size = 10
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleNone
.ColorIndex = xlAutomatic
.Background = xlAutomatic
End With
Selection.Position = xlBottom
ActiveChart.Axes(xlCategory).Select
ActiveChart.SeriesCollection(2).Select
With Selection.Border
.Weight = xlThin
.LineStyle = xlNone
End With
Selection.Shadow = False
Selection.InvertIfNegative = False
Selection.Interior.ColorIndex = xlAutomatic
ActiveChart.SeriesCollection(1).Select
With Selection.Border
.Weight = xlThin
.LineStyle = xlNone
End With
Selection.Shadow = False
Selection.InvertIfNegative = False
Selection.Interior.ColorIndex = xlAutomatic
ActiveChart.Axes(xlCategory).Select
ActiveChart.SeriesCollection(1).Select
ActiveChart.PlotArea.Select
ActiveChart.ChartArea.Select
End Sub

Antwort 10 von youhoo

Achso, und ich habe das Makro aufgezeichnet, als ich das Diagramm markiert hatte und markiere das zu ändernde Diagramm nun auch brav, wenn ich das Makro ausführe.

Antwort 11 von youhoo

Gibt es ggf. auch andere Möglichkeiten, ein beliebiges Diagramm bzgl. Farbe, Schriftart, usw. durch ein Makro auf Knopfdruck automatisch an ein definiertes Layout anzupassen?

Antwort 12 von JoeKe

Hi youhoo,

meier Meinung nach funktioniert dein Makro.

Wo befindet sich dein Button zum formatieren?
Der Button sollte sich auf der Symbolleiste befinden.

Speichere dein Makro mal (wenn noch nicht geschehen) in der persönlichen Makroarbeitsmappe als einfache SUB (nicht Private Sub).

MfG

JöKe

Antwort 13 von youhoo

Bzgl. Diagramm/Tabellenblatt: es geht beides nicht. Aber ich nutze kein Diagrammblatt, d.h. es sollte besser bei Diagrammen in Tabellenblättern laufen...

Antwort 14 von youhoo

Schade, auch mit Sub statt Private Sub kommt nach wie vor die Fehlermeldung mit der nicht festgelegten Objektvariable... :-(

Antwort 15 von JoeKe

Hi,

zu Antwort 13

stimmt hab da wohl noch die verwirrenden Zeilen aus dem ersten Makro im Kopf gehabt.

Antwort 16 von JoeKe

Hi youhoo,

leider weiß ich jetzt auch nicht mehr weiter. Hab inzwischen selbst mal so ein Makro aufgezeichnet und hatte hinter her keine Probleme damit.
Wenn du willst kannst du mir ja mal eine Beispieldatei mit deinem Makro zu mailen. jkemmesies@arcor.de


MfG

JöKe

Antwort 17 von JoeKe

Hi youhoo,

die Antwort-Mail ist unterwegs.
Dein Makro funktioniert soweit. Am besten du siehst dir die Beispiel Datei an.
Für den Fall das die Übertragung nicht klappt und alle anderen interessierten hier noch mal das Makro:


Sub Makro1()
Application.ScreenUpdating = False
Dim i As Integer
For i = 1 To ActiveSheet.ChartObjects.Count
If i > 0 Then
ActiveSheet.ChartObjects(1).Activate
End If
Next i
ActiveChart.Axes(xlValue).AxisTitle.Select
Selection.AutoScaleFont = True
With Selection.Font
.Name = "EY Gothic Comp Demi"
.FontStyle = "Standard"
.Size = 10
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleNone
.ColorIndex = xlAutomatic
.Background = xlAutomatic
End With
Selection.Font.Bold = False
ActiveChart.Axes(xlValue).Select
Selection.TickLabels.AutoScaleFont = True
With Selection.TickLabels.Font
.Name = "EY Gothic Comp Book"
.FontStyle = "Standard"
.Size = 10
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleNone
.ColorIndex = xlAutomatic
.Background = xlAutomatic
End With
ActiveChart.Axes(xlCategory).Select
Selection.TickLabels.AutoScaleFont = True
With Selection.TickLabels.Font
.Name = "EY Gothic Comp Book"
.FontStyle = "Standard"
.Size = 10
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleNone
.ColorIndex = xlAutomatic
.Background = xlAutomatic
End With
ActiveChart.PlotArea.Select
With Selection.Border
.ColorIndex = 16
.Weight = xlThin
.LineStyle = xlContinuous
End With
Selection.Interior.ColorIndex = xlNone
ActiveChart.Axes(xlValue).MajorGridlines.Select
With Selection.Border
.ColorIndex = 48
.Weight = xlHairline
.LineStyle = xlContinuous
End With
ActiveChart.Legend.Select
With Selection.Border
.Weight = xlHairline
.LineStyle = xlNone
End With
Selection.Shadow = False
Selection.Interior.ColorIndex = xlAutomatic
Selection.AutoScaleFont = True
With Selection.Font
.Name = "EY Gothic Comp Book"
.FontStyle = "Standard"
.Size = 10
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleNone
.ColorIndex = xlAutomatic
.Background = xlAutomatic
End With
Selection.Position = xlBottom
ActiveChart.Axes(xlCategory).Select
ActiveChart.SeriesCollection(2).Select
With Selection.Border
.Weight = xlThin
.LineStyle = xlNone
End With
Selection.Shadow = False
Selection.InvertIfNegative = False
Selection.Interior.ColorIndex = xlAutomatic
ActiveChart.SeriesCollection(1).Select
With Selection.Border
.Weight = xlThin
.LineStyle = xlNone
End With
Selection.Shadow = False
Selection.InvertIfNegative = False
Selection.Interior.ColorIndex = xlAutomatic
ActiveChart.Axes(xlCategory).Select
ActiveChart.SeriesCollection(1).Select
ActiveChart.PlotArea.Select
ActiveChart.ChartArea.Select
End Sub

Am Anfang wird die Bezeichnung des Diagramms ermittelt und das gefundene Diagramm aktiviert. Danach werden die Formatierungen durchgeführt. Das Diagramm muss sich auf dem Tabellenblatt befinden. Die Größenachse (y) mus einen Titel haben (in diesem Fall bedingt durch die Formatierung).

MfG

JöKe

PS: Application.ScreenUpdating = False unterdrückt das lästige Bildschirmflackern beim Formatieren.

Antwort 18 von youhoo

Danke im Übrigen nocheinmal, auch für den Mailservice. Ich habe das Problem einfach behoben, indem ich die Funktionen in eine eigene Menüleiste ausgegliedert habe. So ist es von den ganzen Änderungen des ursprünglichen Makros unabhängig und macht keine Probleme bzgl. angesprochener Variablen.

Ich möchte kostenlos eine Frage an die Mitglieder stellen:


Ähnliche Themen:


Suche in allen vorhandenen Beiträgen: