Ob die Lösung elegant ist, weiss ich nicht, aber so sollte es auf jeden Fall funktionieren, wenn Du die Schaltflächen-Namen immer mit einer entsprechenden Nummer enden lässt (von 1 - x) und jeder Schaltfläche das Makro "WhateverButtonPressed" zuweist.
Code: Alles auswählen
Private Const nSpaltenabstand as Integer = 12
Private Const sSourceTableName as String = "Tabelle1_2"
Sub WhateverButtonPressed(oEvt)
oDoc = thisComponent
oSheet = oDoc.CurrentController.ActiveSheet
sButtonName = oEvt.Source.Model.Name
nDigitCount = GetDigitsCountOnEndOfString(sButtonName)
If nDigitCount = 0 Then Exit Sub
nModifikator = CInt(Right(sButtonName,nDigitCount))
oSheet.getCellByPosition(1,1).FormulaLocal = GetNewFormulaString("B1",nModifikator)
oSheet.getCellByPosition(1,5).FormulaLocal = GetNewFormulaString("B4",nModifikator)
oSheet.getCellByPosition(2,5).FormulaLocal = GetNewFormulaString("C4",nModifikator)
oSheet.getCellByPosition(3,5).FormulaLocal = GetNewFormulaString("D4",nModifikator)
oSheet.getCellByPosition(4,5).FormulaLocal = GetNewFormulaString("E4",nModifikator)
oSheet.getCellByPosition(5,5).FormulaLocal = GetNewFormulaString("F4",nModifikator)
oSheet.getCellByPosition(6,5).FormulaLocal = GetNewFormulaString("G4",nModifikator)
oSheet.getCellByPosition(7,5).FormulaLocal = GetNewFormulaString("H4",nModifikator)
oSheet.getCellByPosition(8,5).FormulaLocal = GetNewFormulaString("I4",nModifikator)
oSheet.getCellByPosition(9,5).FormulaLocal = GetNewFormulaString("J4",nModifikator)
oSheet.getCellByPosition(10,5).FormulaLocal = GetNewFormulaString("K4",nModifikator)
oSheet.getCellByPosition(11,5).FormulaLocal = GetNewFormulaString("L4",nModifikator)
oSheet.getCellByPosition(12,5).FormulaLocal = GetNewFormulaString("M4",nModifikator)
End Sub
Function GetNewFormulaString(ByVal sCellName as String,ByVal nModifikator as Integer) as String
sCellNameEnd = Right(sCellName,GetDigitsCountOnEndOfString(sCellName))
sCellName = Left(sCellName,Len(sCellName)- Len(sCellNameEnd))
nColumnASCII = ASC(Right(sCellName,1)) + (nModifikator - 1)*nSpaltenabstand
If Len(sCellName) = 1 Then
If nColumnASCII > 90 Then
GetNewFormulaString = "=" & sSourceTableName & "." & "A" & Chr(64 + (nColumnASCII Mod 90)) & sCellNameEnd
Else
GetNewFormulaString = "=" & sSourceTableName & "." & Chr(nColumnASCII) & sCellNameEnd
End If
Else
If nColumnASCII > 90 Then
GetNewFormulaString = "=" & sSourceTableName & "." & Chr(ASC(Left(sCellName,1))+1) & Chr(64 + (nColumnASCII Mod 90)) & sCellNameEnd
Else
GetNewFormulaString = "=" & sSourceTableName & "." & Left(sCellName,1) & Chr(nColumnASCII) & sCellNameEnd
End If
End If
End Function
Function GetDigitsCountOnEndOfString(sString as String) as Integer
For x = Len(sString) To 1 Step -1
If NOT isNumeric(Mid(sString,x,1)) Then Exit For
Next x
GetDigitsCountOnEndOfString = Len(sString) - x
End Function