Bedingte Formatierung - Zelle als Zielwert wählen

Programmierung unter AOO/LO (StarBasic, Python, Java, ...)

Moderator: Moderatoren

Benny88
Beiträge: 6
Registriert: So, 03.03.2019 05:11

Bedingte Formatierung - Zelle als Zielwert wählen

Beitrag von Benny88 »

Hallo,

ich habe ein kleines Problem mit der Bedingten Formatierung als Makro.
Ist es möglich als Ziel eine berechnete Zelle
z.B. durch

Code: Alles auswählen

oSSFZiel1 = oSSSheet.getcellbyposition(18,nSSFRow-9)
oder

Code: Alles auswählen

oSSFZ1 = oSSSheet.getcellbyposition(18,nSSFRow-9)
oSSFZiel1 = oSSFZ1.getCellAddress
zu wählen?
Die bedingte Formatierung wird richtig gesetzt, nur das als Werte 0 da steht (oder bei der Nutzung von "" in .Value dann oSSFZiel1)
Ich habe eine abgespeckte Version der Tabelle auch im Anhang.

Und hier einmal das komplette Makro:

Code: Alles auswählen

REM  *****  BASIC  *****

Sub Add_Row

oSoap = thisComponent
oBASheet = oSoap.Sheets().getByName("Batch Amounts")
oSSSheet = oSoap.Sheets().getByName("Sugar Scrub Soaps")

oCBACell = oBASheet.getCellRangeByName("VO17:WH32")
oCSSCell = oSSSheet.getCellRangeByName("VO16:WM31")

oBACursor = oBASheet.createCursor
oBACursor.GotoEndOfUsedArea(False)
nBARow = oBACursor.getRangeAddress().endRow
oBACell = oBASheet.getcellbyposition(0,nBARow+1)
oCBARange = oCBACell.getRangeAddress
oBARange = oBACell.getCellAddress
oBASheet.copyRange(oBARange,oCBARange)

oSSCursor = oSSSheet.createCursor
oSSCursor.GotoEndOfUsedArea(False)
nSSRow = oSSCursor.getRangeAddress().endRow
oSSCell = oSSSheet.getcellbyposition(0,nSSRow+1)
oCSSRange = oCSSCell.getRangeAddress
oSSRange = oSSCell.getCellAddress
oSSSheet.copyRange(oSSRange,oCSSRange)

Dim oSSFCell1 as Object
Dim oSSFCell2 as Object
Dim oSSFCell3 as Object
Dim oSSFCell4 as Object
Dim oConditionalForm as Object
Dim oConditionalForm2 as Object

oSSFCursor = oSSSheet.createCursor
oSSFCursor.GotoEndOfUsedArea(False)
nSSFRow = oSSFCursor.getRangeAddress().endRow
oSSFCell1 = oSSSheet.getcellbyposition(12,nSSFRow-4)
oSSFCell2 = oSSSheet.getcellbyposition(12,nSSFRow-11)
oSSFCell3 = oSSSheet.getcellbyposition(18,nSSFRow-7)
oSSFCell4 = oSSSheet.getcellbyposition(12,nSSFRow-8)
oSSFZ1 = oSSSheet.getcellbyposition(18,nSSFRow-9)
oSSFZ2 = oSSSheet.getcellbyposition(18,nSSFRow-4)
oSSFZiel1 = oSSFZ1.getCellAddress
oSSFZiel2 = oSSFZ2.getCellAddress

oConditionalForm = oSSFCell1.ConditionalFormat
oConditionalForm = oSSFCell2.ConditionalFormat
oConditionalForm = oSSFCell3.ConditionalFormat
Dim oCondition1(3) as New com.sun.star.beans.PropertyValue
oCondition1(0).Name = "Operator"
oCondition1(0).Value = com.sun.star.sheet.ConditionOperator.LESS
oCondition1(1).Name = "Formula1"
oCondition1(1).Value = oSSFZiel1
oCondition1(2).Name = "StyleName"
oCondition1(2).Value = "Negativ - Rot"
oConditionalForm.addNew( oCondition1() )
oCondition1(0).Value = com.sun.star.sheet.ConditionOperator.EQUAL
oCondition1(1).Name = "Formula1"
oCondition1(1).Value = oSSFZiel1
oCondition1(2).Name = "StyleName"
oCondition1(2).Value = "Mittelwert - Gelb"
oConditionalForm.addNew( oCondition1() )
oCondition1(0).Value = com.sun.star.sheet.ConditionOperator.GREATER
oCondition1(1).Name = "Formula1"
oCondition1(1).Value = oSSFZiel1
oCondition1(2).Name = "StyleName"
oCondition1(2).Value = "Positiv - Grün"
oConditionalForm.addNew( oCondition1() )
oSSFCell1.ConditionalFormat = oConditionalForm
oSSFCell2.ConditionalFormat = oConditionalForm
oSSFCell3.ConditionalFormat = oConditionalForm

oConditionalForm2 = oSSFCell4.ConditionalFormat
Dim oCondition2(3) as New com.sun.star.beans.PropertyValue
oCondition2(0).Name = "Operator"
oCondition2(0).Value = com.sun.star.sheet.ConditionOperator.LESS
oCondition2(1).Name = "Formula1"
oCondition2(1).Value = "oSSFZiel2*2"
oCondition2(2).Name = "StyleName"
oCondition2(2).Value = "Negativ - Rot"
oConditionalForm2.addNew( oCondition2() )
oCondition2(0).Value = com.sun.star.sheet.ConditionOperator.BETWEEN
oCondition2(1).Name = "Formula1"
oCondition2(1).Value = "oSSFZiel2*2"
oCondition2(1).Name = "Formula2"
oCondition2(1).Value = oSSFZiel1
oCondition2(2).Name = "StyleName"
oCondition2(2).Value = "Mittelwert - Gelb"
oConditionalForm2.addNew( oCondition2() )
oCondition2(0).Value = com.sun.star.sheet.ConditionOperator.GREATER
oCondition2(1).Name = "Formula1"
oCondition2(1).Value = oSSFZiel1
oCondition2(2).Name = "StyleName"
oCondition2(2).Value = "Positiv - Grün"
oConditionalForm2.addNew( oCondition2() )
oSSFCell4.ConditionalFormat = oConditionalForm2

End Sub
Eine kurze Erklärung:
Durch drücken auf die Schaltfläche "Add Row" im sheet "Batch Amounts" wird eine neue Tabelle in beiden Sheets hinzugefügt.
Nun sollen 4 der neu erstellten Zellen eine Bedingte Formatierung bekommen.

Und wenn es Vorschläge dazu gibt wie ich eventuell das Makro übersichtlicher machen kann nehme ich diese auch gerne :) Ist mein erstes Makro. ^^

Danke und LG Benny

Edit:
Gibt es auch die Möglichkeit in die Letzte Aktive Zelle in einer bestimmten Spalte zu springen? :)
Dateianhänge
test.ods
(26.18 KiB) 79-mal heruntergeladen
F3K Total
********
Beiträge: 3704
Registriert: Mo, 28.02.2011 17:49

Re: Bedingte Formatierung - Zelle als Zielwert wählen

Beitrag von F3K Total »

Moin,
wenn du aus den bedingten Formatierungen des ersten Blocks die absolute Zeilenaddressierung entfernst, also die $-Zeichen vor den Zeilennummern, geht dieses kürzere Makro:

Code: Alles auswählen

REM  *****  BASIC  *****

Sub Add_Row

oSoap = thisComponent
oBASheet = oSoap.Sheets().getByName("Batch Amounts")
oSSSheet = oSoap.Sheets().getByName("Sugar Scrub Soaps")

oCBACell = oBASheet.getCellRangeByName("VO17:WH32")
oCSSCell = oSSSheet.getCellRangeByName("VO16:WM31")

oBACursor = oBASheet.createCursor
oBACursor.GotoEndOfUsedArea(False)
nBARow = oBACursor.getRangeAddress().endRow
oBACell = oBASheet.getcellbyposition(0,nBARow+1)
oCBARange = oCBACell.getRangeAddress
oBARange = oBACell.getCellAddress
oBASheet.copyRange(oBARange,oCBARange)

oSSCursor = oSSSheet.createCursor
oSSCursor.GotoEndOfUsedArea(False)
nSSRow = oSSCursor.getRangeAddress().endRow
oSSCell = oSSSheet.getcellbyposition(0,nSSRow+1)
oCSSRange = oCSSCell.getRangeAddress
oSSRange = oSSCell.getCellAddress
oSSSheet.copyRange(oSSRange,oCSSRange)

oSSFCursor = oSSSheet.createCursor
oSSFCursor.GotoEndOfUsedArea(False)
nSSFRow = oSSFCursor.getRangeAddress().endRow
'SourceCells
oSSFSourceCell1 = oSSSheet.getcellbyposition(12,16)'M17
oSSFSourceCell2 = oSSSheet.getcellbyposition(12,19)'M20
oSSFSourceCell3 = oSSSheet.getcellbyposition(18,20)'S21
oSSFSourceCell4 = oSSSheet.getcellbyposition(12,23)'M24
'TargetCells
oSSFTargetCell1 = oSSSheet.getcellbyposition(12,nSSFRow-4)
oSSFTargetCell2 = oSSSheet.getcellbyposition(12,nSSFRow-11)
oSSFTargetCell3 = oSSSheet.getcellbyposition(18,nSSFRow-7)
oSSFTargetCell4 = oSSSheet.getcellbyposition(12,nSSFRow-8)

oSSFTargetCell1.ConditionalFormat = oSSFSourceCell1.ConditionalFormat
oSSFTargetCell2.ConditionalFormat = oSSFSourceCell2.ConditionalFormat
oSSFTargetCell3.ConditionalFormat = oSSFSourceCell3.ConditionalFormat
oSSFTargetCell4.ConditionalFormat = oSSFSourceCell4.ConditionalFormat

End Sub
So kann das bestehende Format einfach übertragen werden.

Gruß R
Dateianhänge
test.ods
(24.89 KiB) 87-mal heruntergeladen
Benny88
Beiträge: 6
Registriert: So, 03.03.2019 05:11

Re: Bedingte Formatierung - Zelle als Zielwert wählen

Beitrag von Benny88 »

Wow, danke F3K Total für die schnelle Hilfe :)
Klappt super!

Weiss vielleicht noch jemand ob es die möglichkeit gibt in die letzte aktive Zelle einer bestimmten Spalte zu springen?
z.B A5 ist die letzte aktive in der Spalte A und es wird ignoriert das in der Spalte B die letzte aktive Zelle B9 ist. Sodass das Makro dann in Spalte A5 springt (und nicht wie bei mir in Spalte A9) ^^
Ich hoffe das ist einigermaßen verständlich erklärt :)

Danke & LG Benny
Stephan
********
Beiträge: 12369
Registriert: Mi, 30.06.2004 19:36
Wohnort: nahe Berlin

Re: Bedingte Formatierung - Zelle als Zielwert wählen

Beitrag von Stephan »

Weiss vielleicht noch jemand ob es die möglichkeit gibt in die letzte aktive Zelle einer bestimmten Spalte zu springen?
z.B.:

Code: Alles auswählen

Sub letzte_zelle()
	odoc=Thiscomponent.Sheets(0)
	ozeile=odoc.Columns(2)
	oleer=ozeile.queryemptycells
	oletzter=oleer(oleer.count-1)
	msgbox "letzte nicht-leere Zeile in Spalte C: " & _
		oletzter.rangeaddress.startrow-1
End Sub

Gruß
Stephan
Benny88
Beiträge: 6
Registriert: So, 03.03.2019 05:11

Re: Bedingte Formatierung - Zelle als Zielwert wählen

Beitrag von Benny88 »

Danke Stephan.
Es ist aber leider nicht genau was ich suche.
Ich hätte mich besser erklären sollen. Das hole ich jetzt nach ^^

Die Tabelle befindet sich im Anhang :)
Wenn ich im Sheet "Batch Amounts" auf die Schaltfläche "Add Body Soap" klicke wird eine neue Tabelle in der Spalte "Body Soaps" nach der letzten aktiven Zelle und eine neue Tabelle im Sheet "Body Soaps" erstellt.
Das Problem ist jedoch, das ich eine unterschiedliche Anzahl an Tabellen habe und beim klicken auf eine der anderen Schaltflächen die neuen Tabellen im Sheet "Batch Amounts" zu weit unten erstellt werden.

Mir ist bewusst das es durch meine Verwendung von .GotoEndOfUsedArea(False) dazu kommt, habe aber leider keine Ahnung wie ich das sinnvoll ersetzen soll.
Momentan wird der zu Kopierende Bereich und der Zielbereich so ausfindig gemacht:

Code: Alles auswählen

oSoap = thisComponent
oBASheet = oSoap.Sheets().getByName("Batch Amounts")
oBASheet.unprotect("")

oCBACell = oBASheet.getCellRangeByName("VO17:VT32")

oBACursor = oBASheet.createCursor
oBACursor.GotoEndOfUsedArea(False)
nBARow = oBACursor.getRangeAddress().endRow
oBACell = oBASheet.getcellbyposition(0,nBARow+1)
oCBARange = oCBACell.getRangeAddress
oBARange = oBACell.getCellAddress
oBASheet.copyRange(oBARange,oCBARange)
Ich hoffe ich habe es nun verständlich erklärt :)

LG Benny
Dateianhänge
test2.0.ods
(41.13 KiB) 90-mal heruntergeladen
F3K Total
********
Beiträge: 3704
Registriert: Mo, 28.02.2011 17:49

Re: Bedingte Formatierung - Zelle als Zielwert wählen

Beitrag von F3K Total »

Moin,
ist zwar eigentlich ein neues Thema, für das du einen neuen Thread eröffnen solltest, hier eine Idee:

Code: Alles auswählen

Sub Add_RowBS

oSoap = thisComponent
oBASheet = oSoap.Sheets().getByName("Batch Amounts")
oBSSheet = oSoap.Sheets().getByName("Body Soaps")
oBASheet.unprotect("")
oBSSheet.unprotect("")

oCBACell = oBASheet.getCellRangeByName("VU17:WA32")
oCBSCell = oBSSheet.getCellRangeByName("VO16:WM31")

'oBACursor = oBASheet.createCursor
'oBACursor.GotoEndOfUsedArea(False)
'nBARow = oBACursor.getRangeAddress().endRow
nBARow = First_Empty_Row(oBASheet,"H")'hier wird mit der function unten, die erste leere Zeile der Spalte "H" auf oBASheet ermittelt
oBACell = oBASheet.getcellbyposition(6,nBARow+1)
oCBARange = oCBACell.getRangeAddress
oBARange = oBACell.getCellAddress
oBASheet.copyRange(oBARange,oCBARange)


oBSCursor = oBSSheet.createCursor
oBSCursor.GotoEndOfUsedArea(False)
nBSRow = oBSCursor.getRangeAddress().endRow
oBSCell = oBSSheet.getcellbyposition(0,nBSRow+1)
oCBSRange = oCBSCell.getRangeAddress
oBSRange = oBSCell.getCellAddress
oBSSheet.copyRange(oBSRange,oCBSRange)

oBSFCursor = oBSSheet.createCursor
oBSFCursor.GotoEndOfUsedArea(False)
nBSFRow = oBSFCursor.getRangeAddress().endRow
'SourceCells
oBSFSourceCell1 = oBSSheet.getcellbyposition(12,16)'M17
oBSFSourceCell2 = oBSSheet.getcellbyposition(12,19)'M20
oBSFSourceCell3 = oBSSheet.getcellbyposition(18,20)'S21
oBSFSourceCell4 = oBSSheet.getcellbyposition(12,23)'M24
'TargetCells
oBSFTargetCell1 = oBSSheet.getcellbyposition(12,nBSFRow-11)'M17
oBSFTargetCell3 = oBSSheet.getcellbyposition(18,nBSFRow-7)'M20
oBSFTargetCell2 = oBSSheet.getcellbyposition(12,nBSFRow-8)'S21
oBSFTargetCell4 = oBSSheet.getcellbyposition(12,nBSFRow-4)'M24

oBSFTargetCell1.ConditionalFormat = oBSFSourceCell1.ConditionalFormat
oBSFTargetCell2.ConditionalFormat = oBSFSourceCell2.ConditionalFormat
oBSFTargetCell3.ConditionalFormat = oBSFSourceCell3.ConditionalFormat
oBSFTargetCell4.ConditionalFormat = oBSFSourceCell4.ConditionalFormat

oBASheet.protect("")
oBSSheet.protect("")

End Sub

Function First_Empty_Row(oSheet, sColumnName)
    oColumn = oSheet.Columns.getbyName(sColumnName)
    oEC = oColumn.queryEmptyCells
    oERange = oSheet.getcellrangebyName(oEC.ElementNames(uBound(oEC.ElementNames)))
    First_Empty_Row = oERange.RangeAddress.StartRow
end function
Gruß R
Benny88
Beiträge: 6
Registriert: So, 03.03.2019 05:11

Re: Bedingte Formatierung - Zelle als Zielwert wählen

Beitrag von Benny88 »

Moin,
ist zwar eigentlich ein neues Thema, für das du einen neuen Thread eröffnen solltest, hier eine Idee:
Du hast recht, hätte ich als neuen Thread fragen sollen.

Und danke nochmal F3K Total, das hilft mir auf jeden Fall weiter :)
Antworten