Ho scritto questa Macro e diciamo che in linea di massima funziona. Viene attivata al cambiamento nel foglio tramite la chiamata alla procedura Ordini (ultima procedura macro)
Codice: Seleziona tutto
' definisco le variabili pubbliche
'foglio attuale,variabili, preventivo
'riga,colonna,peso, minimo, pezzi/gomma,
'Tutti valori preventivo,-,Nome foglio attivo
'Chiamata funzioni Calc
Public SH,VAR,PRE,RG,CL,PESO,MINI,PZGM,VALS,PCOL,ACTIVE,SVC
Public SheetName,CellRange,Item,ret
function _SelSh(Value)
shrange="b2:d9900"
free10range="m2:n9900"
free20range="p2:q9900"
free30range="s2:t9900"
free40range="v2:w9900"
cas5060range="j2:k9900"
' ... here is the actual code of the function
Select Case Value
Case 1 to 19999
SheetName="Minuteria"
CellRange=shrange
Case 20000 to 29999
SheetName="Accessori"
CellRange=shrange
Case 40000 to 49999
SheetName="Chiusure"
CellRange=shrange
Case 50000 to 59999
SheetName="Orecchini"
CellRange=shrange
Case 80000 to 89999
SheetName="Sacro"
CellRange=shrange
Case 90000 to 99999
SheetName="Pins e Tiretti"
CellRange=shrange
Case 100000 to 199999
SheetName="Riferiti"
CellRange=free10range
Case 200000 to 299999
SheetName="Riferiti"
CellRange=free20range
Case 300000 to 399999
SheetName="Riferiti"
CellRange=free30range
Case 400000 to 499999
SheetName="Riferiti"
CellRange=free40range
Case 50600000 to 50609999
SheetName="Riferiti"
CellRange=cas5060range
Case Else
MSGBOX "Errore "
ret=1
exit function
End Select
ret=0
'msgbox "Sheet Name: "+SheetName
End function
Sub CercaOrdini(Target)
if CL <> 3 then exit sub
item = Target.String
value= Target.Value ' il valore Target.Value è quello che incide sul risultato
if item="" or value="" then exit sub
sostituo=True
Item=UCase(item)
result = left(item,3)
shrange="b2:g9900"
diveCENrange="g2:h9900"
diveORErange="d2:e9900"
diveVARIrange="a2:b9900"
menrange="y2:z9900"
If IsNumeric(Item) Then
'Val(Item)
msgbox "Numerico "+value
Call _SelSh(value)
Else
'msgbox "is cliente"
Select Case result
Case "AMC","amc"
SheetName="Amc"
CellRange=shrange
Case "CF.","CI.","CO.","CU.","OR.","VI.","DI.","LO.","PA.","TE."
SheetName="Riferiti"
CellRange=menrange
Case "K-","10900","MOM"
SheetName="Vari"
CellRange=shrange
Case "CEN" ' SOLO PER DIVE
SheetName="Riferiti"
CellRange=diveCENrange
Case "ORE","ORC" ' SOLO PER DIVE
SheetName="Riferiti"
CellRange=diveORErange
Case "PEN","WP.","VE.","ANE","CV." ' SOLO PER DIVE
SheetName="Riferiti"
CellRange=diveVARIrange
Case "ELG" ' SOLO PER ELLEGI
SheetName="Ellegi"
CellRange=shrange
Case Else
SheetName="Vari"
CellRange=shrange
End Select
End If
'------ Eseguo la ricerca impostata sui valori del select
' msgbox ret
if ret=1 then
exit sub
endif
InRange = ThisComponent.Sheets.getByName(SheetName).getCellRangeByName(CellRange) 'devo inserire il rage degli articoli e tuti i dati seguenti
' la funzione Match viene gestita dopo la corretta ricerca del foglio
'---------
' se l'articolo è della free o della dive sostituisco e inserisco descrizione
if SheetName = "Vari" then
'salvo articolo
newitem=UCase(SVC.callFunction("VLOOKUP", Array(Item, InRange,6 , True)))
splititem=split(newitem,".")
_ReadWriteArt
SH.getCellByposition(CL,RG).String = splititem(0)+"."+splititem(1)
SH.getCellByposition(CL+9,RG).String = Item 'imposto le note
exit sub
end If
' se il valore rientra nel campo dei Riferiti (qindi dive, free, menoni) trasformo il riferimento nel codice Cas, quindi vado a recuperarlo
if SheetName = "Riferiti" then
OldItem=Item 'salvo articolo
Item=UCase(SVC.callFunction("VLOOKUP", Array(OldItem, InRange,2 , True))) 'recupero il codice corretto
'msgbox "Old "+OldItem
'ricreo il range
if Item = "0" then
msgbox "Controvalore non definito"
exit sub
endif
value=int(Item)
errore= _SelSh(value)
if errore = 0 then
SH.getCellByposition(CL,RG).String = Item
SH.getCellByposition(CL+9,RG).String = OldItem 'imposto le note
else
msgbox "devo poi estrarre la descrizione"
end if
end if
_ReadWriteArt
End Sub
' procedura globale richiamata da diverse sub-procedure
Sub _Variabili(Target)
SVC = createUnoService("com.sun.star.sheet.FunctionAccess") 'creo la chiamata alle funzioni generali di Calc
VAR = ThisComponent.Sheets.getByName("Variabili")
PRE = ThisComponent.Sheets.getByName("Preventivo")
ACTIVE= ThisComponent.getCurrentController().getActiveSheet().getRangeAddress().Sheet
SH = Target.getSpreadsheet()
RG = Target.celladdress.Row
CL = Target.celladdress.Column
Select Case SH.name()
Case "Minuteria","Accessori","Chiusure","Orecchini","Sacro","Pins e Tiretti","Amc"
PZGM=SH.getCellByPosition(5,RG).Value
PESO=SH.getCellByPosition(3,RG).Value
MINI=SH.getCellByPosition(4,RG).Value
Case "Preventivo"
PZGM=PRE.getCellRangeByName("P5").value
PESO=PRE.getCellRangeByName("P4").value
MINI=PRE.getCellRangeByName("P6").value
CDSH=PRE.getCellRangeByName("R4").value
CDRW=PRE.getCellRangeByName("R3").value
Case Else
End Select
End Sub
Sub Ordini(Target)
If NOT Target.supportsService("com.sun.star.sheet.SheetCell") then exit sub
Call _Variabili(Target)
Call CercaOrdini(Target)
End Sub
Ma appunto, qual'è il problema: il problema nasce dal fatto che ogni tanto la macro di colpo smette di funzionare. E non riesco a capire perché.
E' presente in un file che viene aperto su sistema Mac e su Linux. Sembra che quando lo apro su Linux, subito dopo si riscontra il blocco.
Oggi ad esempio non riesco a farla tornare a funzionare.
Sembra che il problema sia legato al Target ovvero il parametro passato alla variabile in fase di chiamata. O meglio alla identificazione dell valore del parametro:
Infatti il Target.String restituisce il valore corretto (ma come stringa)
il Target.Value restituisco 0.
Cosa ho sbagliato a scrivere?