Je vous propose les macros suivantes que j'utilise dans calc pour la gestion des feuilles : copier, supprimer, afficher une feuille, supprimer différentes feuilles, protéger, déprotéger ....
Code : Tout sélectionner
REM ***** BASIC *****
Option Explicit
'-----------------------------------------------------------------------------------------------
'Série de fonctions sur la gestion des feuilles
'-----------------------------------------------------------------------------------------------
'Macro qui affiche la feuille passée en paramètre
'Suppr indique si on doit supprimer la feuille qui était active
'-----------------------------------------------------------------------------------------------
Sub RetourFeuille(Fretour as string, optional suppr as boolean)
Dim feuille as string, efface as boolean
If IsMissing(suppr) then
efface=false
else
efface=suppr
end if
feuille=NomActiveFeuille()
ActiverFeuille(Fretour)
if efface=true then
oDoc.Sheets.removeByName(feuille)
else
CacheFeuille(feuille,false)
end if
FinSub()
end sub
'-----------------------------------------------------------------------------------------------
'Fonction qui vérifie l'existence d'une feuille
'return true si Ok
'return un message sinon
'-----------------------------------------------------------------------------------------------
Function IsFeuille(feuille as String, Optional Doc as Object) as Boolean
Dim oSheets as Object, oDoc as Object
If IsMissing(Doc) then
oDoc=ThisComponent
else
oDoc=Doc
end if
oSheets=oDoc.Sheets
if oSheets.hasByName(feuille) then
IsFeuille=true
else
IsFeuille=false
end if
end Function
'-------------------------------------------------------------------------------------
'Fonction qui retourne l'index d'une feuille
'-------------------------------------------------------------------------------------
Function IndexFeuille(feuille as String, Optional Doc as Object) as Integer
Dim Sheet as Object
If IsMissing(doc) then
Sheet=RetourneFeuille(feuille)
else
Sheet=RetourneFeuille(feuille,Doc)
end if
IndexFeuille=Sheet.RangeAddress.Sheet
end Function
'-----------------------------------------------------------------------------------------------
'Macro qui insère une feuille après la feuille donnée (ou la dernière)
'-------------------------------------------------------------------------------------
Sub InsereFeuille(feuille as String, Optional prec as String, Optional doc as Object)
Dim Sheets as Object, index as Integer, oDoc as Object, nb as Long
If IsMissing(doc) then
oDoc=ThisComponent
else
oDoc=doc
end if
Sheets=oDoc.Sheets
'si la feuille précédente n'est pas donnée, on la met en dernier
If IsMissing(prec) then
nb=Sheets.Count
index=nb-1
else
index=IndexFeuille(prec,oDoc)
end if
Sheets.insertNewByName(feuille,index+1)
end Sub
'-----------------------------------------------------------------------------------------------
'Macro qui copie (avec nouveau nom) et insère une feuille après la feuille donnée (ou la dernière)
'-------------------------------------------------------------------------------------
Sub CopieFeuille(feuille as String, Nom as String, Optional prec as String, Optional doc as Object)
Dim Sheets as Object, index as Integer, oDoc as Object, nb as Long
If IsMissing(doc) then
oDoc=ThisComponent
else
oDoc=doc
end if
Sheets=oDoc.Sheets
'si la feuille précédente n'est pas donnée, on la met en dernier
If IsMissing(prec) then
nb=Sheets.Count
index=nb-1
else
index=IndexFeuille(prec,oDoc)
end if
Sheets.copyByName(feuille,Nom,index+1)
end Sub
'-----------------------------------------------------------------------------------------------
'Vérifie l'existence d'une feuille : retourne true ou false
'-------------------------------------------------------------------------------------
Function VerifFeuille(feuille as String, Optional doc as Object) as Boolean
Dim oDoc as Object
If IsMissing(doc) then
oDoc=ThisComponent
else
oDoc=doc
end if
if Isfeuille(feuille,oDoc) then
VerifFeuille=true
else
VerifFeuille=false
end if
end Function
'-------------------------------------------------------------------------------------
'Fonction qui retourne un objet Feuille si elle existe
'-------------------------------------------------------------------------------------
Function RetourneFeuille(feuille as string, Optional doc as Object) as Object
Dim oDoc as Object
If IsMissing(doc) then
oDoc=ThisComponent
else
oDoc=doc
end if
if IsFeuille(feuille,oDoc)=true then
RetourneFeuille=oDoc.Sheets.getByName(feuille)
end if
end Function
'-----------------------------------------------------------------------------------------------
'Supprime une feuille
'-----------------------------------------------------------------------------------------------
Sub SupprFeuille(feuille as String, Optional doc as Object)
Dim oDoc as Object
If IsMissing(doc) then
oDoc=ThisComponent
else
oDoc=doc
end if
if IsFeuille(feuille,oDoc)=true then
oDoc.Sheets.removeByName(feuille)
end if
end sub
'-------------------------------------------------------------------------------------
'Fonction qui retourne le nom feuille active
'-------------------------------------------------------------------------------------
Function NomActiveFeuille(Optional doc as Object) as String
Dim Sheet as Object, oDoc as Object
If IsMissing(doc) then
oDoc=ThisComponent
else
oDoc=doc
end if
Sheet=oDoc.CurrentController.ActiveSheet
NomActiveFeuille=Sheet.Name
end Function
'-------------------------------------------------------------------------------------
'Rend active la feuille donnée
'-------------------------------------------------------------------------------------
Sub ActiverFeuille(feuille as String, Optional doc as Object)
Dim oSheet as Object, oDoc as Object
If IsMissing(doc) then
oDoc=ThisComponent
else
oDoc=doc
end if
oSheet=RetourneFeuille(feuille,oDoc)
oDoc.CurrentController.ActiveSheet=oSheet
end Sub
'-----------------------------------------------------------------------------------------------
'macro pour protéger une feuille
'-----------------------------------------------------------------------------------------------
Sub ProtectFeuille(feuille as String, Optional doc as Object)
Dim oSheet as Object, oDoc as Object
If IsMissing(doc) then
oDoc=ThisComponent
else
oDoc=doc
end if
oSheet=RetourneFeuille(feuille,oDoc)
oSheet.protect("passwordachanger")
End Sub
'-----------------------------------------------------------------------------------------------
'macro pour déprotéger une feuille
'-----------------------------------------------------------------------------------------------
Sub UnProtectFeuille(feuille as String, Optional doc as Object)
Dim oSheet as Object, oDoc as Object
If IsMissing(doc) then
oDoc=ThisComponent
else
oDoc=doc
end if
oSheet=RetourneFeuille(feuille,oDoc)
oSheet.unprotect("passwordachanger")
End Sub
'-----------------------------------------------------------------------------------------------
Sub UnProtectFeuilleActive()
Dim feuille as string
feuille=NomActiveFeuille()
UnProtectFeuille(feuille)
MsgBox("Acompleter",64,"Information")
end sub
'-----------------------------------------------------------------------------------------------
'macro pour déprotéger toutes les feuilles
'-----------------------------------------------------------------------------------------------
Sub UnProtectAllFeuille(Optional doc as Object)
Dim oSheets as Object, oDoc as Object, nb as Integer, i as integer, oSheet as Object
If IsMissing(doc) then
oDoc=ThisComponent
else
oDoc=doc
end if
nb=oDoc.Sheets.Count
oSheets=oDoc.Sheets
for i=0 to nb-1
oSheet=RetourneFeuille(oSheets(i).Name,oDoc)
oSheet.unprotect("passwordachanger")
next i
End Sub
'-----------------------------------------------------------------------------------------------
'macro pour protéger toutes les feuilles
'-----------------------------------------------------------------------------------------------
Sub ProtectAllFeuille(Optional doc as Object)
Dim oSheets as Object, oDoc as Object, nb as Integer, i as integer, oSheet as Object
If IsMissing(doc) then
oDoc=ThisComponent
else
oDoc=doc
end if
nb=oDoc.Sheets.Count
oSheets=oDoc.Sheets
for i=0 to nb-1
oSheet=RetourneFeuille(oSheets(i).Name,oDoc)
oSheet.protect("passwordachanger")
next i
End Sub
'-----------------------------------------------------------------------------------------------
'macro pour cacher une feuille ou pas
'-----------------------------------------------------------------------------------------------
Sub CacheFeuille(feuille as String, val as boolean, Optional Doc as Object)
Dim oSheet as Object, oDoc as Object
If IsMissing(doc) then
oDoc=ThisComponent
else
oDoc=doc
end if
oSheet=RetourneFeuille(feuille,oDoc)
oSheet.IsVisible=val
end sub
'-----------------------------------------------------------------------------------------------
'macro pour cacher toutes les feuille sauf celle en paramètre et active cette feuille
'-----------------------------------------------------------------------------------------------
Sub CacheAllFeuille(feuille as string,Optional Doc as Object)
Dim oSheet as Object, oDoc as Object, nb as integer, oSheets as object, i as integer
If IsMissing(doc) then
oDoc=ThisComponent
else
oDoc=doc
end if
nb=oDoc.Sheets.Count
oSheets=oDoc.Sheets
ActiverFeuille(feuille)
CacheFeuille(feuille,true)
for i=0 to nb-1
oSheet=RetourneFeuille(oSheets(i).Name,oDoc)
if oSheets(i).Name<>feuille then oSheet.IsVisible=false
next i
end sub
'-----------------------------------------------------------------------------------------------
'macro pour afficher toutes les feuille sauf la premiere
'-----------------------------------------------------------------------------------------------
Sub AfficheAllFeuille(Optional Doc as Object)
Dim oSheet as Object, oDoc as Object, nb as integer, oSheets as object, i as integer
If IsMissing(doc) then
oDoc=ThisComponent
else
oDoc=doc
end if
nb=oDoc.Sheets.Count
oSheets=oDoc.Sheets
for i=1 to nb-1
oSheet=RetourneFeuille(oSheets(i).Name,oDoc)
oSheet.IsVisible=true
next i
end sub
'-----------------------------------------------------------------------------------------------