Live-Forum - Die aktuellen Beiträge
Anzeige
Archiv - Navigation
1448to1452
Aktuelles Verzeichnis
Verzeichnis Index
Übersicht Verzeichnisse
Vorheriger Thread
Rückwärts Blättern
Nächster Thread
Vorwärts blättern
Anzeige
HERBERS
Excel-Forum (Archiv)
20+ Jahre Excel-Kompetenz: Von Anwendern, für Anwender
Inhaltsverzeichnis

VBA Code aus 2013 funzt nicht in 2007

VBA Code aus 2013 funzt nicht in 2007
05.10.2015 13:14:50
Nilix
Hallo zusammen,
ich habe ein Excelfile erstellt das bei mir in 2013 gut läuft, aber bei Kollegen mit 2007 bleibt das Macro stehen und mault beim Farbcode.

Range(farbe).Select
Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlLessEqual, _
Formula1:="=$AL$1"
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Font
.Color = RGB(255, 255, 255)
End With
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.Color = RGB(255, 0, 0)
End With

Habe die Farben schon auf RGB Farben geänder, wie ihr seht. Geht immer noch nicht.
Wenn ich in Excel 2007 die Anlage der bedingten Formatierung aufzeichne, sieht das ganze so aus: _

Sub Makro2()
' Makro2 Makro
Selection.FormatConditions.Add Type:=xlExpression, Formula1:="=B12"
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Font
.ThemeColor = xlThemeColorLight1
.TintAndShade = 4.99893185216834E-02
End With
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.Color = 255
.TintAndShade = 0
End With
Selection.FormatConditions(1).StopIfTrue = False
End Sub

So oder so ähnlich sah der Code erst im 2013 auch aus, aber in 2007 geöffnet läuft er nicht mehr, deswegen habe ich erst die Farbanweisung geändert. Aber nichts hilft.
Woran liegt das und wie bekomme ich das Problem abgestellt?
Vielen Dank für eure Hilfe.
Nilix

12
Beiträge zum Forumthread
Beiträge zu diesem Forumthread

Betreff
Datum
Anwender
Anzeige
AW: VBA Code aus 2013 funzt nicht in 2007
05.10.2015 17:54:18
Michael
Hi Nilix,
ich habe nur den Range bestückt ...
Sub BedingteFormatierung()
Dim farbe As String
farbe = "A2:L20"
Range(farbe).Select
Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlLessEqual, _
Formula1:="=$A$1"
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Font
.Color = RGB(255, 255, 255)
End With
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.Color = RGB(255, 0, 0)
End With
End Sub

... und es läuft bei mir in 2007 problemlos.
Der Fehler ist nicht nachvollziehbar. Vielleicht so: .Color = vbWhite
Gruß,
Michael

Anzeige
AW: VBA Code aus 2013 funzt nicht in 2007
06.10.2015 14:54:18
Nilix
Hallo,
danke Michael, Dein Code hatte mich auf die Idee gebracht die Range in eine Variable zu schreiben , als String zu deklarieren und dann zum markieren des zu formatierenden Bereichs aufzurufen.
Habe nun mal den vollständigen Code angehängt da das Problem leider immer noch besteht und ich keine Lösung mehr weiß. Bitte schaut euch das nochmal an, habe viel Zeit investiert und nun geht es nicht :(
Vielen DAnk.
Sub BedingteFormatierung()
' Sucht die letzte Zeile mit inhalt.
LastRow = Cells.Find(What:="*", After:=[A1], _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious).Row
' Sucht die letzte Spalte mit inhalt.
LastColumn = Cells.Find(What:="*", After:=[A1], _
SearchOrder:=xlByColumns, _
SearchDirection:=xlPrevious).Column
'bereich festlegen
Dim farbe As String
farbe = "$AI$9:" & Cells(LastRow - 5, LastColumn - 61).Address
Dim f60 As String
f60 = "$AI$9:$AI$" & LastRow
Dim f70 As String
f70 = "$AJ$9:$AJ$" & LastRow
Dim f80 As String
f80 = "$AK$9:$AK$" & LastRow
Dim f90 As String
f90 = "$AL$9:$AL$" & LastRow
Dim lz As String
lz = "$AW$9:$AW$" & LastRow
Range(farbe).Select
' alle formate loeschen
Selection.FormatConditions.Delete
'Format Rot/Weiß
Range(farbe).Select
Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlLessEqual, _
Formula1:="=$AL$1"
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Font
.Color = RGB(255, 255, 255)
End With
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.Color = RGB(255, 0, 0)
End With
'Format Orange
Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlGreater, _
Formula1:="=$AL$1"
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Font
.Color = RGB(0, 0, 0)
End With
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.Color = RGB(255, 128, 0)
End With
'Format Gelb
Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlGreater, _
Formula1:="=$AL$2"
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Font
.Color = RGB(0, 0, 0)
End With
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.Color = RGB(255, 255, 0)
End With
'Format Grün
Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlGreater, _
Formula1:="=$AL$3"
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Font
.Color = RGB(0, 0, 0)
End With
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.Color = RGB(0, 238, 0)
End With
'Format Weiß
Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlGreater, _
Formula1:="=$AL$4"
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Font
.Color = RGB(0, 0, 0)
End With
'Format Grau in Grau
Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlEqual, _
Formula1:="=$AL$6"
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Font
.Color = RGB(131, 139, 131)
End With
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.Color = RGB(131, 139, 131)
End With
'Größer gleich  60
Range(f60).Select
Selection.FormatConditions.Add Type:=xlExpression, Formula1:="=$J9>=60"
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Font
'hier stopt Excel die ausführung(fehlercode habe ich im moment nicht)
.Color = RGB(131, 139, 131)
End With
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.Color = RGB(131, 139, 131)
End With
'Größer gleich  70
Range(f70).Select
Selection.FormatConditions.Add Type:=xlExpression, Formula1:="=$J9>=70"
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Font
.Color = RGB(131, 139, 131)
End With
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.Color = RGB(131, 139, 131)
End With
'Größer gleich  80
Range(f80).Select
Selection.FormatConditions.Add Type:=xlExpression, Formula1:="=$J9>=80"
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Font
.Color = RGB(131, 139, 131)
End With
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.Color = RGB(131, 139, 131)
End With
'Größer gleich  90
Range(f90).Select
Selection.FormatConditions.Add Type:=xlExpression, Formula1:="=$J9>=90"
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Font
.Color = RGB(131, 139, 131)
End With
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.Color = RGB(131, 139, 131)
End With
'Format Grau in Schwarz
Selection.FormatConditions.Add Type:=xlExpression, Formula1:="=$J9>95"
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Font
.Color = RGB(0, 0, 0)
End With
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.Color = RGB(131, 139, 131)
End With
'Formatierung der zeit
Range(lz).Select
Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlBetween, _
Formula1:="=0", Formula2:="=90"
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.Color = RGB(0, 128, 0)
End With
Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlBetween, _
Formula1:="=90", Formula2:="=110"
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.Color = RGB(255, 255, 0)
End With
Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlGreater, _
Formula1:="=110"
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.Color = RGB(255, 0, 0)
End With
'Ungleich test Weiß
Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _
"=$H9""test"""
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Font
.Color = RGB(255, 255, 255)
End With
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.Color = RGB(255, 255, 255)
End With
Selection.FormatConditions(1).StopIfTrue = False
End Sub

Anzeige
alles ist nix ohne Datei
06.10.2015 15:08:46
Michael
Hi Nilix,
um Dich zu zitieren: hatte mich auf die Idee gebracht die Range in eine Variable zu schreiben - das WAR doch schon so, oder was sonst war Range(farbe)?
Egal.
Es ist mühsam, eine Datei mit irgendwelchen Werten zu füllen, nur um einen Code zu testen: deshalb lade bitte eine mit anonymisierten Daten (und ohne Bezüge auf andere Datein) hoch.
Dann sehe ich's mir nochmal an.
Gruß,
Michael

AW: alles ist nix ohne Datei
06.10.2015 16:38:49
Nilix
Ja bei der variablen farbe aber nicht bei den anderen, und sie waren nicht als String deklariert.
Im Anhang das Anonymisierte file
Danke

Anzeige
weiß nicht...
06.10.2015 17:26:40
Michael
Hallo Nilix,
ähm, es bleibt in der Tat in der Zeile stehen.
Ich habe mal vbWhite eingesetzt, das geht aber auch nicht.
Ich stelle mal auf offen, weil ich aus dem Haus muß.
Schöne Grüße,
Michael

AW: weiß nicht...
06.10.2015 22:52:42
Nilix
Michael, ich danke Dir das Du Dich meiner Sache an nimmst.
Mir ist nämlich nicht klar warum es in der Zeile stehen bleibt und ich habe schon sämtliche Farbcode Schreibweisen getestet.
ich glaube fast das es daran nicht liegt, da Excel 2007 ja sogar die mehrstellige Farbcode Schreibweise die auch in den minus Bereich geht beim über den macrorecorder verwendet. diese Schreibweise der Farbe bekommst du auch wenn Du mit 2013 aufzeichnest, das kann also nicht das Problem sein.

Anzeige
Jetzt aber
07.10.2015 16:15:43
Michael
Hi Nilix,
da liegt der Hase im Pfeffer: sieh Dir mal bei markierter Spalt AW die bedingte Formatierung an: da hast Du (geschätzt) 50 Einträge. Bis X2003 gab es nur 3 Möglichkeiten, bei X2007 anscheinend um die 50 und bei X2013 eben noch mehr.
Der Punkt ist, daß Du eingangs die bedingte Formatierung des Range(farbe) entfernts - in diesem Range ist allerdings *nicht* die Spalte AW enthalten.
Füge nach Range(lz).Select die Zeile ein:
Selection.FormatConditions.Delete

und alles ist in Butter.
Es grüßt der Excel-Buddha

Anzeige
AW: Jetzt aber
08.10.2015 10:52:09
Nilix
Hi Michael,
woooow Perfekt es läuft! Danke Dir! Da wär ich nie drauf gekommen, viel zu kompliziert gedacht.
Danke, Danke, Danke!
Kannst Du mir noch sagen wie ich es schreiben muss wenn excel erst prüfen soll ob "bericht" oder "bericht.xlsx" geöffnet ist und dann das den jeweiligen bericht schließen soll.
im moment habe ich die zeile so drin
Workbooks("bericht.xlsx").Close
das gibt aber eine fehlemeldung wenn "bericht" geöffnet ist. Das ist der Fall wen du den Bericht aus unserem system ausgeleitet hast , Ihn geöffnet aber noch nicht abgespeichert hast, dann hat er keine .XLSX endung.
deswegen unterscheide ich beim start des scriptes 3 fälle:
Sub get_data()
Dim LastRow As Long
Dim LastCell As String
Dim WorkbookName As String
Dim Msg As String, Ans As Variant
Dim MyInput
Dim wbk As Workbook
Application.ScreenUpdating = False
If Range("B9").Value  "" Then
'If MsgBox("Möchten Sie alle Daten ersetzen?", vbYesNo) = vbNo Then Exit Sub
Else
GoTo routine
End If
routine:
WorkbookName = ActiveWorkbook.Name
For Count = 1 To Workbooks.Count
If Workbooks(Count).Name = "bericht" Then GoTo copyroutine1
If Workbooks(Count).Name = "bericht.xlsx" Then GoTo copyroutine2 Else
Next
If MsgBox("Kein Bericht geöffnet, möchten Sie aus Zentraldatei aktualisieren?",  _
vbYesNo) = vbNo Then Exit Sub
GoTo copyroutine3
copyroutine1:
Call Laufzeit_entfernen
Call alle_inhalte_entfernen
If Windows("bericht").Activate Then GoTo Copystart Else
MsgBox "Dateiformat Unbekannt."
GoTo Quit
copyroutine2:
Call Laufzeit_entfernen
Call alle_inhalte_entfernen
If Windows("bericht.xlsx").Activate Then GoTo Copystart Else
MsgBox "Dateiformat Unbekannt."
GoTo Quit
copyroutine3:
Application.DisplayAlerts = False
If Dir("\\.....\bericht.xlsx") _
 "" Then
Workbooks.Open Filename:="\\...\bericht.xlsx", UpdateLinks:=3, Notify:= _
False
Else
MsgBox "Report nicht gefunden! Überprüfen Sie bitte ob Report  _
im richtigen verzeichniss abgelegt wurde."
GoTo Quit
End If
Windows(WorkbookName).Activate
Call Laufzeit_entfernen
Call alle_inhalte_entfernen
Windows("bericht.xlsx").Activate
'If WorksheetFunction.CountA(Cells) > 0 Then
Copystart:
Findest Du das so gut oder könnte man dies eleganter/ einfacher lösen. Möchte ja was lernen :)
Vielen Dank für Deine Hilfe Excel-Buddha

Anzeige
AW: Jetzt aber
08.10.2015 11:03:51
Nilix
Ach ja und noch etwas.
Wie kann ich das ermitteln des letzten feldes so auslagern das ich die gefüllten variablen in _ allen modulen nutzen kann.

Sub LinienInhalte()
LastRow = Cells.Find(What:="*", After:=[A1], _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious).Row
' Search for any entry, by searching backwards by Columns.
LastColumn = Cells.Find(What:="*", After:=[A1], _
SearchOrder:=xlByColumns, _
SearchDirection:=xlPrevious).Column
LastCellStatus = Cells(LastRow - 5, LastColumn).Address
Problem ist vieleicht auch das ich das für das Excelfile aus dem ich kopiere und für das excelfile in das reincopiert wird nutze. Ich muss die variablen entweder unterscheiden oder nach dem rauskopieren nochmal überschreiben.

Anzeige
ganz langsam
08.10.2015 11:55:19
Michael
Hi Nilix,
freut mich, wenn es funzt.
Ich fühle mich zeitlich überfordert, mir Deine copyroutinen anzusehen: wenn es funktioniert, soll es ja recht sein.
Das zweite löst man über globale Variablen (also solche, die nicht innerhalb einer Sub deklariert sind).
Codeschnipsel anbei:
Option Explicit
Public LCSvon As String, LCSnach As String
Public Function LastCellStatus(wkb As String, blatt As String) As String
Dim LastRow As Long, LastColumn As Long
LastRow = Workbooks(wkb).Sheets(blatt).Cells.Find(What:="*", After:=[A1], _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious).Row
' Search for any entry, by searching backwards by Columns.
LastColumn = Workbooks(wkb).Sheets(blatt).Cells.Find(What:="*", After:=[A1], _
SearchOrder:=xlByColumns, _
SearchDirection:=xlPrevious).Column
LastCellStatus = Cells(LastRow - 5, LastColumn).Address
End Function
Sub VariablenSchreiben()
LCSvon = LastCellStatus(ActiveWorkbook.Name, "Tabelle1")
LCSnach = LastCellStatus(ActiveWorkbook.Name, "Tabelle2")
End Sub
Sub VariablenZugriff()
MsgBox LCSvon & " - " & LCSnach
End Sub

Dazu könnte man die Funktionalität zum Ermitteln der letzten Zeile/Spalte in eine Function stecken, der man sowohl den Namen der zu prüfenden Datei als auch den Namen des zu prüfenden Blatts übergibt.
Die ermittelte Adresse kannst Du dann in globalen Variablen speichern, auf die Du auch von anderen Modulen aus Zugriff hast (dafür sind sie Public).
Schöne Grüße,
Michael

Anzeige
AW: VBA Code aus 2013 funzt nicht in 2007
07.10.2015 16:13:39
Nilix
Keiner mehr eine Idee :(

299 Forumthreads zu ähnlichen Themen

Anzeige
Anzeige

Beliebteste Forumthreads (12 Monate)

Anzeige

Beliebteste Forumthreads (12 Monate)

Anzeige
Anzeige
Anzeige