Insertion d'une image externePictures.Insert(fichierImage) insère le fichier spécifié à la position du curseur. Sub essai() ou Sub essai2() Pour que la hauteur de l'image soit égale à la hauteur de la cellule répertoirePhoto = "c:\mesdoc\" ' Adapter Pour que l'image occupe la cellule en hauteur et largeur répertoirePhoto = "c:\mesdoc\" ' Adapter Si la cellule est fusionnée répertoirePhoto = "c:\mesdoc\" ' Adapter
Fonction qui positionne une image sur la cellule qui contient son nom Function positionImage(cel As Range) Import avec GetOpenFilename GetOpenFileName Sub ImportImage() Gif Web Set img=ActiveSheet.Pictures.Insert ("http://www.lemonde.fr/medias/www/1.2.167/img/lgo/lemonde_fr_grd.gif") Import images WebImporte les images associées aux hyperliens Sub ImportImagesLiens() Image Web dans un formulairePrivate Sub UserForm_Initialize() Choix d'une photo dans un contrôle image
Private Sub Worksheet_Change(ByVal Target As Range) Positionnement d'un shapeLes propriétés Left et Top définissent la position d'un shape. ActiveSheet.Shapes(1).Top =100 Positionnement d'un shape sur une celluleSub Positionnement() Centrage d'une image dans un champSub CentrageChamp() Adresse de la cellule d'un shapeTopLeftCell.Address
|
Un shape est placé
entre 2 shapes invisibles |
Ce que l'on voit |
|
|
For y = 1 To 170
ActiveSheet.Shapes("monshape").Left = 220
ActiveSheet.Shapes("monshape").Top = y
w = 0.04
temp = Timer
Do While Timer < temp + w
DoEvents
Loop
Next y
Function CompteImages(champ As Range)
Application.Volatile
For Each s In ActiveSheet.Shapes
If Not Intersect(Range(s.TopLeftCell.Address),
champ) Is Nothing Then
n = n + 1
End If
Next
CompteImages = n
End Function
Function CompteImages2(champ As Range, nomImage)
Application.Volatile
For Each s In ActiveSheet.Shapes
If Not Intersect(Range(s.TopLeftCell.Address),
champ) Is Nothing Then
If s.Name = nomImage Then n
= n + 1
End If
Next
CompteImages2 = n
End Function
Sub CreeShapes()
i = 1
For Each c In ActiveSheet.Comments
With ActiveSheet.Shapes.AddShape(Type:=msoShapeCross,
_
Left:=c.Parent.Left + c.Parent.Width
- 9, Top:=c.Parent.Top, Width:=9, Height:=9)
.Fill.ForeColor.RGB = RGB(255, 255,
255)
.Line.ForeColor.RGB = RGB(255, 0,
0)
.Name = "commentaire" &
i
i = i + 1
End With
Next
End Sub
Sub SupShapes()
For Each s In ActiveSheet.Shapes
If Left(s.Name, 11) = "commentaire"
Then s.Delete
Next s
End Sub
Pour faire apparaître les commentaires avec un triangle vert.
Sub CreeShapes()
i = 1
For Each c In ActiveSheet.Comments
With ActiveSheet.Shapes.AddShape(Type:=msoShapeRightTriangle,
_
Left:=c.Parent.Left +
c.Parent.Width - 4, Top:=c.Parent.Top + 1, Width:=4, Height:=4)
.Fill.ForeColor.RGB = RGB(0,
255, 0)
.Line.ForeColor.RGB = RGB(0,
255, 0)
.IncrementRotation 180
.Name = "commentaire"
& i
i = i + 1
End With
Next
End Sub
Autres formes
msoShapeDownArrow
msoShapeSmileyFace
msoShapeCloudCallout
msoShapeRightArrow
msoShapeOvalCallout
msoShapeIsoscelesTriangle
msoShapeHeart
msoShapecross
Function NomShapeCellule(adr)
For Each s In ActiveSheet.Shapes
If s.TopLeftCell.Address = adr.Address Then
NomShapeCellule = s.Name
End If
Next s
End Function
Function TailleImg(nom)
TailleImg = ActiveSheet.Shapes(nom).Width & "x"
& ActiveSheet.Shapes(nom).Height
End Function
Encadre la cellule active ou la sélection en rouge. On peut aussi choisir une forme ovale.
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
On Error Resume Next
ActiveSheet.Shapes("Curseur").Visible = True
If Err <> 0 Then
ActiveSheet.Shapes.AddShape(msoShapeRectangle,
6, 6, 8, 6).Name = "curseur"
ActiveSheet.Shapes("Curseur").Fill.Visible
= msoFalse
ActiveSheet.Shapes("curseur").Line.Visible
= True
ActiveSheet.Shapes("curseur").Line.ForeColor.SchemeColor
= 10
ActiveSheet.Shapes("curseur").Line.Weight
= 3
End If
ActiveSheet.Shapes("curseur").Left = Target.Left
ActiveSheet.Shapes("curseur").Top = Target.Top
ActiveSheet.Shapes("curseur").Height = Selection.Height
ActiveSheet.Shapes("curseur").Width = Selection.Width
End Sub
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Set champ = [B3:E21]
If Not Intersect(champ, Target) Is Nothing Then
On Error Resume Next
Shapes("curseurH").Visible = True
If Err <> 0 Then ActiveSheet.Shapes.AddTextbox(msoTextOrientationHorizontal,
1, 1, 1000, 1).Name = "curseurH"
ActiveSheet.Shapes("curseurH").Line.ForeColor.RGB
= RGB(255, 0, 0)
Shapes("curseurH").Top = ActiveCell.Top
+ ActiveCell.Height
Shapes("curseurH").Height = 1
Shapes("curseurH").Width = champ.Width
Shapes("curseurH").Left = champ.Left
ActiveSheet.Shapes("curseurv").Line.ForeColor.RGB
= RGB(255, 0, 0)
Else
On Error Resume Next
Shapes("curseurH").Visible = False
End If
End Sub
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Set champ = [b4:BK7]
If Not Intersect(champ, Target) Is Nothing Then
On Error Resume Next
Shapes("curseur").Visible = True
If Err <> 0 Then ActiveSheet.Shapes.AddTextbox(msoTextOrientationVertical,
1, 1, 1, 1).Name = "curseur"
Shapes("curseur").Fill.Solid
Shapes("curseur").Fill.ForeColor.SchemeColor
= 10
Shapes("curseur").Line.ForeColor.RGB
= RGB(255, 0, 0)
Shapes("curseur").Top = champ.Top
Shapes("curseur").Left = ActiveCell.Left
- 3
Shapes("curseur").Height = champ.Height
Shapes("curseur").Width = 2
Else
On Error Resume Next
Shapes("curseur").Visible = False
End If
End Sub
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
On Error Resume Next
ActiveSheet.Shapes("Curseur").Visible = True
If Err <> 0 Then
ActiveSheet.Shapes.AddShape(7, 6, 6, 8, 6).Name
= "Curseur"
ActiveSheet.Shapes("Curseur").Fill.ForeColor.SchemeColor
= 2
ActiveSheet.Shapes("curseur").IncrementRotation
90
ActiveSheet.Shapes("curseur").Line.Visible
= msoFalse
End If
ActiveSheet.Shapes("curseur").Left = Target.Left
+ 4
ActiveSheet.Shapes("curseur").Top = Target.Top +
2
ActiveSheet.Shapes("curseur").Height = 6
ActiveSheet.Shapes("curseur").Width = 8
End Sub
Sub CommentImages()
repertoire = ThisWorkbook.Path & "\"
For Each c In Range("A2", [A65000].End(xlUp))
c.ClearComments
c.AddComment
c.Comment.Text Text:=CStr(c)
fichier = CStr(c.Value) & ".jpg"
If Dir(repertoire & fichier) <> ""
Then
c.Comment.Shape.Fill.UserPicture
repertoire & fichier
taille = TaillePixelsImage(repertoire,
fichier)
c.Comment.Shape.Height = Val(Split(taille,
"x")(1))
c.Comment.Shape.Width = Val(Split(taille,
"x")(0))
c.Comment.Shape.ScaleHeight
0.5, msoFalse, msoScaleFromTopLeft
c.Comment.Shape.ScaleWidth 0.5,
msoFalse, msoScaleFromTopLeft
End If
Next
End Sub
Function TaillePixelsImage(repertoire, fichier)
Set myShell = CreateObject("Shell.Application")
Set myFolder = myShell.Namespace(repertoire)
Set myFile = myFolder.Items.Item(fichier)
TaillePixelsImage = myFolder.GetDetailsOf(myFile, 26)
End Function
Sub essai()
CreeShapes Range("B2"), "texte1", "texte2"
CreeShapes Range("E3"), "texte3", "texte4"
End Sub
Sub CreeShapes(c, texte1, texte2)
On Error Resume Next
ActiveSheet.Shapes(c.Address & "1").Delete
ActiveSheet.Shapes(c.Address & "2").Delete
With ActiveSheet.Shapes.AddShape(Type:=msoShapeRightTriangle,
_
Left:=c.Left, Top:=c.Top + 1,
Width:=c.Width, Height:=c.Height)
.OLEFormat.Object.Characters.Text
= texte1
.OLEFormat.Object.Characters.Font.Size
= 7
.Fill.ForeColor.RGB = RGB(255,
0, 0)
.Line.ForeColor.RGB = RGB(255,
0, 0)
.IncrementRotation 180
.Name = c.Address &
"1"
End With
With ActiveSheet.Shapes.AddShape(Type:=msoShapeRightTriangle,
_
Left:=c.Left, Top:=c.Top + 1, Width:=c.Width,
Height:=c.Height)
.OLEFormat.Object.Characters.Text
= texte2
.OLEFormat.Object.Characters.Font.Size
= 7
.Fill.ForeColor.RGB = RGB(0, 255,
0)
.Line.ForeColor.RGB = RGB(0, 255,
0)
.Name = c.Address & "2"
End With
End Sub
Sub supshapes()
Sheets(1).DrawingObjects.Delete
End Sub
-Définir un modèle de fiche en R2:S4
-Créer un shape MonShape avec l'appareil photo.
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Not Intersect([B2:G8], Target) Is Nothing And Target.Count
= 1 Then
If Target <> "" Then
Set p = Application.Index([Base],
, 1).Find(what:=Target, lookat:=xlWhole)
If Not p Is Nothing Then
ActiveSheet.Shapes("monShape").Visible
= True
[s2] = Target
Shapes("monshape").Left
= Target.Offset(, 1).Left + 5
Shapes("monshape").Top
= Target.Top
End If
Else
ActiveSheet.Shapes("monShape").Visible
= False
End If
Else
ActiveSheet.Shapes("monShape").Visible
= False
End If
End Sub
Autre version avec Shapes (ne fonctionne pas sur Excel 2003)
Différentes façons de représenter des informations sous forme d'arborescence.
OrganigrammeH
OrganigrammeH Liens Sup
OrganigrammeHClic
OrganigrammeH Survol
OrganigrammeV
OrganigrammeMaterielVClic
Organigramme Généalogie
Organigramme
Généalogie branche choisie
Classe ArbreTableau
ArbreTableau
Classe
ArbreDictionary
Dim colonne, débutOrg, f, forga, inth, intv, Tbl(), n
Sub DessineOrga()
Set forga = Sheets("orgaShapes")
Set f = Sheets("bd")
Tbl = f.Range("A2:C" & f.[A65000].End(xlUp).Row).Value
n = UBound(Tbl)
For Each s In forga.Shapes
If s.Type = 17 Or s.Type = 1 Then s.Delete
Next
inth = 70
intv = 60
colonne = 0
Set débutOrg = forga.Range("c4")
créeShape Tbl(1, 1), 1, Tbl(1, 3), f.Cells(2, 1).Interior.Color
End Sub
Sub créeShape(parent, niv, Attribut, coul) ' procédure récursive
hauteurshape = 48
largeurshape = 85
colonne = colonne + 1
forga.Shapes.AddShape(msoShapeFlowchartAlternateProcess, 10,
10, largeurshape, hauteurshape).Name = parent
forga.Shapes(parent).Line.ForeColor.SchemeColor = 1
txt = parent & vbLf & Attribut
With forga.Shapes(parent)
.TextFrame.Characters.Text = txt
.TextFrame.Characters(Start:=1, Length:=1000).Font.Size
= 8
.TextFrame.Characters(Start:=1, Length:=1000).Font.ColorIndex
= 0
.TextFrame.Characters(Start:=1, Length:=Len(parent)).Font.Bold
= True
.TextFrame.Characters(Start:=1, Length:=Len(parent)).Font.ColorIndex
= 3
.Fill.ForeColor.RGB = coul
End With
forga.Shapes(parent).Left = débutOrg.Left + inth *
colonne
forga.Shapes(parent).Top = débutOrg.Top + intv * (niv
- 1)
For i = 1 To n
If Tbl(i, 1) = parent And niv > 1 Then
shapePère = Tbl(i, 2)
forga.Shapes.AddConnector(msoConnectorElbow,
100, 100, 100, 100).Name = parent & "c"
forga.Shapes(parent & "c").Line.ForeColor.SchemeColor
= 22
forga.Shapes(parent & "c").ConnectorFormat.BeginConnect
forga.Shapes(shapePère), 3
forga.Shapes(parent & "c").ConnectorFormat.EndConnect
forga.Shapes(parent), 1
End If
If Tbl(i, 2) = parent Then créeShape Tbl(i, 1),
niv + 1, Tbl(i, 3), f.Cells(i + 1, 1).Interior.Color
Next i
End Sub
Avec photo
Organigramme
Photo
Organigramme
PhotoArrièrePlan
Autre version avec regroupements de noms au 3e niveau
Avec la seconde version, on visualise la branche choisie de l'arbre généalogique.
Organigramme
Généalogie
Organigramme
Généalogie branche choisie
Arbre Généalogique
(pedigree) branche choisie
Ci dessous, on obtient l'arbre généalogique des ascendants pour la ligne choisie dans la base de données.
Arbre généalogique (pedigree) branche choisie
Positionne un curseur en fonction de la région choisie en col A.
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
xfrance = ActiveSheet.Shapes("france").Left
yfrance = ActiveSheet.Shapes("france").Top
If Not Intersect([A3:A100], Target) Is Nothing And Target.Count
= 1 Then
ActiveSheet.Shapes("pointeur").Top =
Target.Offset(, 2) + yfrance
ActiveSheet.Shapes("pointeur").Left
= Target.Offset(, 1) + xfrance
End If
End Sub
-placer le pointeur rouge sur la région
-placer le curseur dans la colonne B
-puis clic
Sub recupXY()
ActiveCell = ActiveSheet.Shapes("pointeur").Left - ActiveSheet.Shapes("france").Left
ActiveCell.Offset(, 1) = ActiveSheet.Shapes("pointeur").Top
- ActiveSheet.Shapes("france").Top
End Sub
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
xfrance = ActiveSheet.Shapes("france").Left
yfrance = ActiveSheet.Shapes("france").Top
If Not Intersect([A2:A100], Target) Is Nothing And Target.Count
= 1 Then
ActiveSheet.Shapes("commentaire").Top
= Target.Offset(, 2) + yfrance
ActiveSheet.Shapes("commentaire").Left
= Target.Offset(, 1) + xfrance
ActiveSheet.Shapes("commentaire").TextFrame.Characters.Text
= Target.Offset(, 3)
End If
End Sub
SurvolRegionForm
SurvolRegionTableur
Dim Lbl(1 To 23) As New ClasseLabel
Private Sub UserForm_Initialize()
i = 0
For Each c In Me.Controls
temp = c.Name
If TypeName(c) = "Label" And temp <>
"Commentaire" Then
i = i + 1
Set Lbl(i).GrLabels = Me(temp)
End If
Next c
End Sub
Module de classe ClasseLabel
Public WithEvents GrLabels As Msforms.Label
Private Sub GrLabels_MouseMove(ByVal Button As Integer, ByVal Shift As
Integer, ByVal X As Single, ByVal Y As Single)
Set result = [a:a].Find(what:=Replace(GrLabels.Name, "_",
" "))
If Not result Is Nothing Then
UserForm1.Commentaire.Caption = " Région
" & Cells(result.Row, 1) & Chr(10) & _
" " & Cells(result.Row, 2)
UserForm1.Commentaire.Left = GrLabels.Left
UserForm1.Commentaire.Top = GrLabels.Top
End If
End Sub
Sub BullesCA()
Set f = Sheets("BullesCa")
ech = 30000 / Application.Max([B:B])
For Each c In f.Range([A2], f.[A65000].End(xlUp))
temp = (c.Offset(, 1) / 1000 * ech * ech) ^ 0.5
f.Shapes(c).Height = temp
f.Shapes(c).Width = temp * 1.1
f.Shapes(c).TextFrame.Characters.Text = c.Offset(,
1)
f.Shapes(c).OLEFormat.Object.Font.Size = 6
Next c
End Sub
Sub région()
régionChoisie = Application.Caller
[ListeRégions].Find(what:=régionChoisie).Resize(, 2).Select
End Subs
Declare Function SetTimer Lib "user32" (ByVal
hWnd As Long, ByVal nIDEvent As Long, ByVal uElapse As Long, ByVal lpTimerFunc
As Long) As Long
Declare Function KillTimer Lib "user32" (ByVal hWnd As Long,
ByVal nIDEvent As Long) As Long
Declare Function GetCursorPos Lib "user32" (lpPoint As POINT)
As LongDim TimerOn As Boolean
Dim TimerOn As Boolean
Dim TimerId As Long
Type POINT
X As Long
Y As Long
End Type
Public Function TimerProc(ByVal hWnd As Long, ByVal uMsg As Long, ByVal
wParam As Long, ByVal lParam As Long) As Long
Dim p As POINTAPI
On Error Resume Next
GetCursorPos p
Set champ = ActiveWindow.RangeFromPoint(p.X, p.Y)
If champ.Row <= 10 And champ.Column <= 10 Then
Cells(champ.Row, champ.Column).Select
ActiveSheet.Shapes("zone").TextFrame.Characters.Text
= Cells(champ.Row, champ.Column)
ActiveSheet.Shapes("zone").Left = ActiveCell.Left
+ ActiveCell.Width + 5
Cells(1, 1) = champ.Row & "*" &
champ.Column
End If
End Function
Sub DébutTimer()
If Not TimerOn Then
TimerId = SetTimer(0, 0, 5, AddressOf
TimerProc)
TimerOn = True
End If
End Sub
Sub FinTimer()
If TimerOn Then
KillTimer 0, TimerId
TimerOn = False
End If
End Sub
Sub auto_close()
FinTimer
End Sub
Public Declare Function GetCursorPos Lib "user32"
(lpPoint As POINT) As Long
Public Type POINT
x As Long
y As Long
End Type
Public p As POINT
Public boucle
Sub essai()
boucle = True
Do While boucle
GetCursorPos p
[C1] = p.x
[D1] = p.y
DoEvents
Loop
End Sub
Sub fin()
boucle = False
End Sub
Les images ont la même taille. On clique sur le nom
de l'image en colonne A. L'image apparaît dans un formulaire.
On suppose que les images externes sont dans le même répertoire
que le classeur Excel.
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Column = 1 And Target.Count = 1 Then
If Target <> "" Then
répertoireImage = ThisWorkbook.Path
' à adapter
NomImage = Target
If Dir(répertoireImage &
"\" & NomImage & ".jpg") <> ""
Then
UserForm1.Image1.PictureSizeMode
= fmPictureSizeModeStretch
UserForm1.Image1.Picture
= LoadPicture(répertoireImage & "\" & NomImage
& ".jpg")
UserForm1.Show
End If
End If
End If
End Sub
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Column = 1 And Target.Count = 1 Then
If Target <> "" Then
répertoireImage = ThisWorkbook.Path
' à adapter
NomImage = Target
If Dir(répertoireImage &
"\" & NomImage & ".jpg") <> ""
Then
taille = TaillePixelsImage(répertoireImage,
NomImage & ".jpg")
rap = Val(Split(taille,
"x")(0)) / Val(Split(taille, "x")(1))
UserForm1.Image1.Height
= 200 ' on fixe la hauteur
UserForm1.Image1.Width
= UserForm1.Image1.Height * rap
UserForm1.Height = UserForm1.Image1.Height
+ 20
UserForm1.Width = UserForm1.Image1.Width
UserForm1.Image1.Picture
= LoadPicture(répertoireImage & "\" & NomImage
& ".jpg")
UserForm1.Show
End If
End If
End If
End Sub
Function TaillePixelsImage(repertoire, fichier)
Set myShell = CreateObject("Shell.Application")
Set myFolder = myShell.Namespace(repertoire)
Set myFile = myFolder.Items.Item(fichier)
TaillePixelsImage = myFolder.GetDetailsOf(myFile, 26)
End Function
L'opérateur clique sur une image miniature interne au classeur pour afficher dans un formulaire l'image externe correspondante. Le nom de la photo externe=nom de l'image miniature interne.
Sub Affichephoto()
répertoire = ThisWorkbook.Path
NomImage = Application.Caller
If Dir(répertoire & "\" & NomImage
& ".jpg") <> "" Then
taille = TaillePixelsImage(répertoire,
NomImage & ".jpg")
rap = Val(Split(taille, "x")(0)) / Val(Split(taille,
"x")(1))
UserForm1.Image1.Height = 120
UserForm1.Image1.Width = UserForm1.Image1.Height
* rap
UserForm1.Height = UserForm1.Image1.Height + 20
UserForm1.Width = UserForm1.Image1.Width
UserForm1.Image1.Picture = LoadPicture(répertoire
& "\" & NomImage & ".jpg")
UserForm1.Show
End If
End Sub
Function TaillePixelsImage(repertoire, fichier)
Set myShell = CreateObject("Shell.Application")
Set myFolder = myShell.Namespace(repertoire)
Set myFile = myFolder.Items.Item(fichier)
TaillePixelsImage = myFolder.GetDetailsOf(myFile, 26)
End Function
Les mots clés sont définis dans la zone commentaires des images (propriétés)
Dim repertoireImages
Private Sub UserForm_Initialize()
repertoireImages = ThisWorkbook.Path & "\"
' adapter
Set mondico = CreateObject("Scripting.Dictionary")
Set myShell = CreateObject("Shell.Application")
Set myFolder = myShell.Namespace(repertoireImages)
nf = Dir(repertoireImages & "*.jpg")
Do While nf <> ""
Set myFile = myFolder.items.Item(nf)
cmt = myFolder.GetDetailsOf(myFile, 14)
'DateCréation = myFolder.GetDetailsOf(myFile,3)
'auteur = myFolder.GetDetailsOf(myFile,9)
'titre = myFolder.GetDetailsOf(myFile,10)
tmp = LCase(Left(nf, Len(nf) - 4))
mondico.Item(tmp) = tmp
If cmt <> "" Then
b = Split(cmt, ",")
For Each k In b
tmp = LCase(k)
mondico.Item(tmp) = tmp
Next k
End If
nf = Dir
Loop
temp = mondico.items
Call tri(temp, LBound(temp), UBound(temp))
Me.ComboBox1.List = temp
End Sub
Private Sub ComboBox1_Change()
[A2:B100].ClearContents
For Each s In ActiveSheet.Shapes
If Not Intersect(s.TopLeftCell, Range("c:c"))
Is Nothing Then s.Delete
Next s
ligne = 2
Set myShell = CreateObject("Shell.Application")
Set myFolder = myShell.Namespace(repertoireImages)
nf = Dir(repertoireImages & "*.jpg")
Do While nf <> ""
Set myFile = myFolder.items.Item(nf)
cmt = myFolder.GetDetailsOf(myFile, 14)
If InStr(LCase(cmt), Trim(Me.ComboBox1)) >
0 Or LCase(Left(nf, Len(nf) - 4)) = Me.ComboBox1 Then
Cells(ligne, 1) = nf
Cells(ligne, 2) = cmt
Set s = ActiveSheet.Pictures.Insert(repertoireImages
& nf)
H = s.Height
L = s.Width
s.Name = nf
s.Left = Cells(ligne, 3).Left + 2
s.Top = Cells(ligne, 3).Top + 2
s.Height = Cells(ligne, 1).EntireRow.RowHeight
- 3
ech = s.Height / H
s.Width = L * ech - 3
ligne = ligne + 1
End If
nf = Dir
Loop
End Sub
Sub tri(a, gauc, droi) ' Quick sort
ref = a((gauc + droi) \ 2)
g = gauc: d = droi
Do
Do While a(g) < ref: g = g + 1: Loop
Do While ref < a(d): d = d - 1: Loop
If g <= d Then
temp = a(g): a(g) = a(d): a(d) = temp
g = g + 1: d = d - 1
End If
Loop While g <= d
If g < droi Then Call tri(a, g, droi)
If gauc < d Then Call tri(a, gauc, d)
End Sub
On affiche 'une image au survol d'une image.
-Avec la BO Boite à outils contrôle,
créer une image Image1
-Dans les propriétes, choisir l'image dans Picture
-Inserer une image Image2
Private Sub Image1_MouseMove(ByVal Button As Integer, ByVal
Shift As Integer, ByVal X As Single, ByVal Y As Single)
If X < 10 Or X > Image1.Width - 10 Or Y < 10 Or Y
> Image1.Height - 10 Then
ActiveSheet.Shapes("Image2").Visible
= False
Else
ActiveSheet.Shapes("Image2").Visible = True
End If
End Sub
Private Sub Image1_MouseMove(ByVal Button As Integer, ByVal
Shift As Integer, ByVal X As Single, ByVal Y As Single)
Image1.Picture = _
IIf((X < 10 Or X > Image1.Width - 10 Or Y < 10 Or
Y > Image1.Height - 10), Sheets("photos").Image1.Picture,
Sheets("photos").Image2.Picture)
End Sub
FormImageInterneSurvol
FormImageExterneSurvol
ImageExterneSurvol
Les photos sont encapsulées dans des objets Contrôle Image (BO contrôles).
DiaporamaTableurPhotosInternes
Dim p, temps, f
Sub auto_open()
Set f = Sheets("photos")
p = 1
majHeure
End Sub
Sub majHeure()
temp = "Photo" & p
Sheets("accueil").Image1.Picture = f.OLEObjects(temp).Object.Picture
p = p + 1
If p = 4 Then p = 1
temps = Now + TimeValue("00:00:3")
Application.OnTime temps, "majHeure"
End Sub
Sub auto_close()
On Error Resume Next
Application.OnTime temps, Procedure:="majHeure",
Schedule:=False
End Sub
DiaporamaTableurPhotosInternes
DiaporamaTableurPhotosInternes2
DiaporamaTableurPhotosExternes
DiaporamaFormulaire
-Créer 3 photos des champs A1:E7 des feuilles 2008,2009,2010
avec Edition/Copier puis Maj+Edition/coller l'image
avec liaison
-Les nommer 2008,2009,2010
Dim temps, p
Sub auto_open()
majHeure
p = 0
End Sub
Sub majHeure()
Application.ScreenUpdating = False
a = Array(2008, 2009, 2010)
For Each c In a
Sheets(1).Shapes(CStr(c)).Visible = False
Next c
Sheets(1).Shapes(CStr(a(p))).Left = 20
Sheets(1).Shapes(CStr(a(p))).Top = 10
Sheets(1).Shapes(CStr(a(p))).Visible = True
p = p + 1
If p > UBound(a) Then p = 0
temps = Now + TimeValue("00:00:5")
Application.OnTime temps, "majHeure"
End Sub
Sub auto_close()
On Error Resume Next
Application.OnTime temps, Procedure:="majHeure",
Schedule:=False
End Sub
En mode direct
Pour copier une image interne dans un contrôle image d'un formulaire, Edition/Copier puis dans la propriété Picture du contrôle Edition/Coller.
En VBA
Dim f
Private Sub UserForm_Initialize()
Set f = Sheets("photos")
For Each s In f.Shapes
Me.ComboBox1.AddItem s.Name
Next
End Sub
Private Sub ComboBox1_Change()
Set s = f.Shapes(CStr(Me.ComboBox1))
s.CopyPicture xlScreen, xlBitmap
With s.Parent.ChartObjects.Add(0, 0, s.Width, s.Height).Chart
While .Shapes.Count = 0
DoEvents
.Paste
Wend
.Export "monimage.jpg", "Jpg"
.Parent.Delete
End With
Me.Image1.PictureSizeMode = fmPictureSizeModeZoom
Me.Image1.Picture = LoadPicture("monimage.jpg")
Kill "monimage.jpg"
End Sub
Les images sont encapsulées dans des contrôles Image
Dim f
Private Sub UserForm_Initialize()
Set f = Sheets("photos")
For Each s In f.Shapes
Me.ComboBox1.AddItem s.Name
Next
End Sub
Private Sub ComboBox1_Change()
temp = Me.ComboBox1
Me.Image1.Picture = f.OLEObjects(temp).Object.Picture
End Sub
On affiche une image interne dans une une autre image (cadre)
PhotoInterneCadre
PhotoInterneCadre2
Dim p
Private Sub CommandButton1_Click()
Set f = Sheets("photos")
p = p + 1: If p > 3 Then p = 1
Set s = f.Shapes("photo" & p)
s.CopyPicture xlScreen, xlBitmap
With s.Parent.ChartObjects.Add(0, 0, s.Width, s.Height).Chart
While .Shapes.Count = 0
DoEvents
.Paste
Wend
.Export "monimage.jpg", "Jpg"
.Parent.Delete
End With
ActiveSheet.Shapes("Cadre").Fill.UserPicture "monimage.jpg"
Kill "monimage.jpg"
End Sub
Cette fonction affichePhoto(photo As String, nom As String) affiche une image interne de la personne en B2. Les images sont encapsulées dans des contrôles Image (Boite à outils contrôles)
Function affichePhoto(Photo As String, nom As String)
Application.Volatile
Set f = Sheets(Application.Caller.Parent.Name)
f.OLEObjects(Photo).Object.Picture = Nothing
f.OLEObjects(Photo).Object.Picture = Sheets("images").OLEObjects(nom).Object.Picture
affichePhoto = ""
End Function
Cette fonction affichePhoto(photo As String, nom As String) affiche une image interne de la personne qui a réalisé le meilleur CA du mois.
En B7:=affichePhoto(B1;INDEX($A$2:$A$6;EQUIV(MAX(B2:B6);B2:B6;0)))
Function affichePhoto(photo As String, nom As String)
Application.Volatile
Set f = Sheets(Application.Caller.Parent.Name)
f.OLEObjects(Photo).Object.Picture = Nothing
f.OLEObjects(photo).Object.Picture = Sheets("images").OLEObjects(nom).Object.Picture
affichePhoto = nom
End Function
L'image du bouton à bascule est modifiée lorsque le bouton est enfoncé.
Private Sub ToggleButton1_Click()
Me.ToggleButton1.Picture = IIf(Me.ToggleButton1, Me.Image1.Picture,
Me.Image2.Picture)
End Sub
-Nommer le champ B2:B10 (champ)
-Avec la boîte à outils Contrôles,
créer un label Label1 transparent avec A
-Le positionner sur le champ.
AffichePhotoExterneSurvolCellule
Dim Xc, Yc
Private Sub Label1_MouseMove(ByVal Button As Integer, ByVal Shift As Integer,
ByVal X As Single, ByVal Y As Single)
d = 3
If X < d Or X > Label1.Width - d Or Y < d Or Y >
Label1.Height - d Then
Range("champ").Interior.ColorIndex
= xlNone
UserForm1.Hide
Else
Hcel = Range("champ").Cells(1,
1).Height
Lcel = Range("champ").Cells(1,
1).Width
Yc = Int(Y / Hcel)
Xc = Int(X / Lcel)
Range("champ").Interior.ColorIndex
= xlNone
Range("champ").Cells(1, 1).Offset(Yc,
Xc).Interior.ColorIndex = 3
'Range("champ").Cells(1, 1).Offset(Yc,
Xc).Select ' optionel
répertoireImage
= "c:\mesdoc" ' à adapter
NomImage = Range("champ").Cells(1,
1).Offset(Yc, Xc)
If Dir(répertoireImage & "\"
& NomImage & ".jpg") <> "" Then
UserForm1.Image1.Picture = LoadPicture(répertoireImage
& "\" & NomImage & ".jpg")
UserForm1.Show
End If
End If
End Sub
Private Sub Label1_Click()
Range("champ").Interior.ColorIndex = xlNone
Range("champ").Cells(1, 1).Offset(Yc, Xc).Select
Range("champ").Cells(1, 1).Offset(Yc, Xc).Interior.ColorIndex
= 4
ActiveSheet.Label1.Visible = False
Range("champ").Cells(1, 1).Offset(Yc, Xc).Select
AppActivate "Microsoft Excel"
ActiveSheet.Label1.Width = Range("champ").Width
ActiveSheet.Label1.Height = Range("champ").Height
ActiveSheet.Shapes("label1").Top = Range("champ").Top
+ 1
ActiveSheet.Shapes("label1").Left = Range("champ").Left
+ 1
End Sub
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Intersect([champ], Target) Is Nothing Then
Unload UserForm1
Range("champ").Interior.ColorIndex =
xlNone
ActiveSheet.Label1.Visible = True
Else
Range("champ").Interior.ColorIndex =
xlNone
ActiveSheet.Label1.Visible = True
End If
End Sub
Pour obtenir un tableau avec arrière-plan:
1-Créer un rectangle avec les formes automatiques
2-Format/Forme automatique
3-Transparence 65%
3-Couleurs et traits/Couleur/Motifs et textures/Image
4-Choisir une image
5- Déplacer le rectangle sur le tableau
Pour automatiser le procédé ci dessus, créer une fonction ArrierePlan(NomImage,ChampArrierePlan,RépertoireImage,transparence)
En H1 par ex: =ArrierePlan("clown";B1:F10;"c:\mesdoc\";70%)
Tableau
Arrière Plan
Tableau Arrière
PlanURL
1-Edition/copier du champ B2:F6
2-Maj+Edition/coller image avec liaison
3-Agrandir la copie
4- Affecter une image d'arrière plan:
-Format/image
-Couleurs et traits/couleur
-Motifs et textures/image
La fonction Jauge(taux, champJauge As Range,Largeur,Transparence) crée sur le champ spécifié un shape proportionnel au taux de réalisation (0%->100%)
FonctionJaugeVerticale
FonctionJaugeVerticale2
FonctionJaugeHorizontale
FonctionJaugeHorizontale2
Pour obtenir une jauge sur 40% de la largeur de colonne et non transparente.
En B12:=jauge(B11/B2;B4:B9;40%;0)
Pour obtenir une jauge sur la largeur de la colonne et transparente à 70%.
En B12:=jauge(B11/B2;B4:B9;100%;70%)
Function Jauge(taux, champJauge As Range, largeur, transparence)
Application.Volatile
Set f = Sheets(Application.Caller.Parent.Name)
NomShape = "S" & Application.Caller.Address
NomShape2 = NomShape & "-x"
For Each s In f.Shapes
If s.Type = 1 Then
If Not Intersect(s.TopLeftCell,
champJauge) Is Nothing Then
If Right(s.Name,
2) = "-x" Then temp = Left(s.Name, Len(s.Name) - 2) Else temp
= s.Name
If UCase(temp) <>
UCase(NomShape) Then s.Delete
End If
End If
Next s
For Each s In f.Shapes
If UCase(s.Name) = UCase(NomShape) Then
ok = True
Next s
If Not ok Then
f.Shapes.AddShape(msoShapeRectangle, 120#, 258.75,
52.5, 34.5).Name = NomShape
f.Shapes(NomShape).Fill.ForeColor.SchemeColor
= 9
NomShape2 = NomShape & "-x"
f.Shapes.AddShape(msoShapeRectangle, 120#, 258.75,
52.5, 34.5).Name = NomShape2
End If
If taux > 1 Then taux = 1
f.Shapes(NomShape2).Fill.ForeColor.SchemeColor = IIf(taux
< 0.5, 2, 3)
f.Shapes(NomShape).Fill.Transparency = transparence
f.Shapes(NomShape).Width = champJauge.Width * largeur
f.Shapes(NomShape).Height = champJauge.Height * (1 - taux)
f.Shapes(NomShape).Top = champJauge.Top
f.Shapes(NomShape).Left = champJauge.Left
f.Shapes(NomShape2).Fill.Transparency = transparence
f.Shapes(NomShape2).Width = champJauge.Width * largeur
f.Shapes(NomShape2).Height = champJauge.Height * taux
f.Shapes(NomShape2).Top = champJauge.Top + champJauge.Height
* (1 - taux)
f.Shapes(NomShape2).Left = champJauge.Left
Jauge = ""
End Function
Avec =jauge(B10/B2;B10;40%;0), on obtient
Si le champ qui recoit la jauge contient des cellules fusionnées remplacer champ.Height par champJauge.MergeArea.Height.
En B11: =jaugeTriangle(B10/B2;B4:B9)
=jaugeThermo(taux;champ)
=Thermo(taux;champ;largeur%)
Fonction Jauge(taux, champJauge As Range, hauteur)
Ci dessous, le shape nommé Dupont apparaît
plus ou moins foncé en fonction du % total/objectif
en B12.
Ceci est obtenu en modifiant la transparence (40% -->100%)
Private Sub Worksheet_Calculate()
ActiveSheet.Shapes("dupont").Fill.Transparency =
1 - (100 * IIf([B12] < 1, [B12], 1)) / 170
End Sub
La fonction Transparent(nomShape, cellule, taux)
modifie la transparence de shapes en fonction de la valeur de la cellule
par rapport au maximun des cellules du champ B11:F11.
Si le shape n'existe pas, il est crée et positionné par
la fonction.
Function Transparent(NomShape, cel, taux)
Set f = Sheets(Application.Caller.Parent.Name)
For Each s In f.Shapes
If s.Type = 17 Then
If s.TopLeftCell.Address = cel.Address
Then
If UCase(s.Name) <>
UCase(NomShape) Then s.Delete
End If
End If
Next s
For Each s In f.Shapes
If UCase(s.Name) = UCase(NomShape) Then ok = True
Next s
If Not ok Then
f.Shapes.AddTextbox(msoTextOrientationHorizontal,
10, 10, 100, 50).Name = NomShape
f.Shapes(NomShape).Fill.ForeColor.SchemeColor
= 2 '3=vert 4:bleu 13:jaune 2:rouge
End If
f.Shapes(NomShape).Fill.Transparency = 1 - (100 * taux) /
170
f.Shapes(NomShape).Width = cel.Width
f.Shapes(NomShape).Height = cel.Height
f.Shapes(NomShape).Top = cel.Top
f.Shapes(NomShape).Left = cel.Left
Transparent = ""
End Function
En B13:
=SI(B12<100%;Transparent(B1;B12;B12;2);Transparent(B1;B12;100%;3))
-Pour les % positifs, un shape FlècheHaut
vert est affiché.
-Pour les % négatifs, un shape FlècheBas
vert est affiché.
-La transparence est fonction de la valeur du pourcentage.
En B13: =SI(B12>0;Transparent(B1;B12;B12;3;100%);Transparent(B1;B12;B12;2;100%))
Pour afficher les flèches sur le champ B4:B9 sur
la moitié de la colonne:
En B13: =SI(B12>0;Transparent(B1;B4:B9;B12;3;50%);Transparent(B1;B4:B9;B12;2;50%))
Cette fonction Affiche(NomShape;couleur;message) crée un shape dans la cellule où est elle est écrite et afiiche un message.
En B11:
=SI(B10>300;Affiche(B1;2;"Excellent!");SI(B10>200;Affiche(B1;3;"Super!");SI(B10>100;Affiche(B1;5;"Bravo!");Affiche(B1;9;""))))
Cette fonction =ArrierePlan(NomPhoto;ChampArrièrePlan;RépertoirePhoto) crée un shape et le positionne sur le champ spécifié.
-Pour déplacer les shapes, cliquer dessus
-F9 pour les replacer sur le champ
Fonction
Arrière-Plan
Arrière-PlanVariable
En B12:
=ArrierePlan(B1;B4:B9;"c:\mesdoc\")
Arrière-PlanVariable
Constructeur d'Arrière-Plan
Cette fonction Feu(NomFeu, Pourc, champAffFeu) construit des feux tricolores en fonction d'un % et les affiche à l'endroit spécifié.
-Rouge si %<0
-Orange si %=0
-Vert si %>0
Variantes de cette fonction
Function FeuTranche(NomFeu, Pourc, pRouge, pOrange, champAffFeu)
Function FeuNo(NomFeu, no, champAffFeu)
Function Feu(NomFeu, Pourc, champAffFeu)
Application.Volatile
Set f = Sheets(Application.Caller.Parent.Name)
If Pourc < 0 Then no = 1 Else If Pourc = 0 Then no = 2
Else no = 3
For Each s In f.Shapes
If s.Type = 1 Then
If Not Intersect(s.TopLeftCell, champAffFeu.Resize(2))
Is Nothing Then
If Mid(s.Name, Len(NomFeu)
+ 1, 1) = "C" Then
temp
= Left(s.Name, Len(s.Name) - 2)
Else
temp
= s.Name
End If
If UCase(temp) <>
UCase(NomFeu) Then s.Delete
End If
End If
Next s
For Each s In f.Shapes
If UCase(s.Name) = UCase(NomFeu) Then ok
= True
Next s
If Not ok Then
f.Shapes.AddShape(msoShapeRectangle, 15, 15, 16,
48).Name = NomFeu
f.Shapes(NomFeu).Fill.ForeColor.SchemeColor =
0
For c = 1 To 3
f.Shapes.AddShape(msoShapeOval, 15
+ 3 * (c - 1) * 15, 15 + 3, 10, 10).Name = NomFeu & "C"
& c
Next
End If
f.Shapes(NomFeu).Top = champAffFeu.Top + 2
f.Shapes(NomFeu).Left = champAffFeu.Left + 5
For c = 1 To 3
f.Shapes(NomFeu & "C" & c).Top
= champAffFeu.Top + (c - 1) * 15 + 6
f.Shapes(NomFeu & "C" & c).Left
= champAffFeu.Left + 8
f.Shapes(NomFeu & "C" & c).Fill.ForeColor.SchemeColor
= 0
f.Shapes(NomFeu & "C" & c).Line.Visible
= True
f.Shapes(NomFeu & "C" & c).Line.ForeColor.SchemeColor
= 1
Next c
f.Shapes(NomFeu & "C" & no).Fill.ForeColor.SchemeColor
= Array(2, 52, 3)(no - 1)
Feu = ""
End Function
Crée des shapes avec les noms de champ de la feuille.
Sub NomsChamps()
For Each s In ActiveSheet.Shapes
If Left(s.Name, 2) = "x_" Then
s.Delete
Next s
For Each n In ActiveWorkbook.Names
p = InStr(n, ActiveSheet.Name)
If p > 0 Then
p1 = InStr(n, "!")
P2 = InStr(n, ":")
c = Mid(n, p1 + 1, P2
- p1 - 1)
nom = "x_" &
n.Name
ActiveSheet.Shapes.AddTextbox(msoTextOrientationHorizontal,
1, 1, Len(n.Name) * 7, 10).Name = nom
ActiveSheet.Shapes(nom).TextFrame.Characters.Font.Name
= "Verdana"
ActiveSheet.Shapes(nom).TextFrame.Characters.Font.Size
= 8
ActiveSheet.Shapes(nom).Fill.ForeColor.SchemeColor
= 13
ActiveSheet.Shapes(nom).Left
= Range(c).Left
t = IIf(Range(c).Row >
1, Range(c).Offset(-1, 0).Top, Range(c).Top)
ActiveSheet.Shapes(nom).Top
= t
ActiveSheet.Shapes(nom).TextFrame.Characters.Text
= n.Name
End If
Next n
End Sub
-Créer 3 photos des champs A1:E7
des feuilles 2008,2009,2010 avec Edition/Copier
puis Maj+Edition/coller l'image avec liaison
-Les nommer 2008,2009,2010
ChoixChamp
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Set champ = [B2:B4]
If Not Intersect(champ, Target) Is Nothing And Target.Count
= 1 Then
For Each c In champ
ActiveSheet.Shapes(CStr(c)).Visible
= False
Next c
If Target <> "" Then
ActiveSheet.Shapes(CStr(Target)).Left
= 180
ActiveSheet.Shapes(CStr(Target)).Top
= 10
ActiveSheet.Shapes(CStr(Target)).Visible
= True
End If
End If
End Sub
La photo du champ A1:F7 de feuil1 suit le curseur lorsque celui ci est déplacé
Pour créer une photo du champ A1:E7
de feuil1:
-Edition/Copier du champ
-MaJ+Edition coller l'image avec liaison dans Feuil2
-Nommer le shape Image 1
Pour que la photo Image 1 du champ suive le déplacement du curseur
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Shapes("image 1").Top = Target.Top
Shapes("image 1").Left = Target.Offset(, 2).Left
End Sub
Pour un affichage en haut à gauche de l'écran.
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Shapes("image 1").Top = ActiveWindow.VisibleRange.Rows(1).Top
Shapes("image 1").Left = ActiveWindow.VisibleRange.Columns(1).Left
End Sub
Sub ExtraitPhotoCmtJPG()
répertoirePhotos = "c:\photos\" ' Adapter
If Dir(répertoirePhotos, vbDirectory) = ""
Then MkDir répertoirePhotos
Set f = Sheets("liste")
For Each c In f.Range("a2:a" & f.[a65000].End(xlUp).Row)
c.Comment.Visible = True
H = c.Comment.Shape.Height
L = c.Comment.Shape.Width
c.Comment.Shape.CopyPicture
c.Comment.Visible = False
f.ChartObjects.Add(0, 0, L, H).Chart.Paste
f.ChartObjects(1).Border.LineStyle = 0
f.ChartObjects(1).Chart.Export Filename:=
_
répertoirePhotos &
c & ".jpg", FilterName:="jpg"
f.ChartObjects(1).Delete
Next c
End Sub
Sub ExtraitPhotosCmt()
Set f = Sheets("extrait")
For Each c In f.Range("a2:a" & f.[a65000].End(xlUp).Row)
c.Comment.Visible = True
c.Comment.Shape.CopyPicture
c.Comment.Visible = False
f.Paste
Selection.Name = c.Value
Next c
End Sub
Sub ConvShapesJPG()
répertoirePhotos = "c:\photos\"
Set f = Sheets("images")
For Each s In f.Shapes
If s.Type = 13 Then
s.CopyPicture
f.ChartObjects.Add(0, 0, s.Width, s.Height).Chart.Paste
f.ChartObjects(1).Select
Selection.Border.LineStyle = 0
f.ChartObjects(1).Chart.Export Filename:=répertoirePhotos
& s.Name & ".jpg"
f.ChartObjects(1).Delete
End If
Next s
End Sub
-Les photos internes sont dans la feuille
Liste
-Elles sont d'abord exportées dans un répertoire c:\photos
par auto_open()
Dim répertoirePhotos
Sub auto_open()
répertoirePhotos = "c:\photos\" '
Adapter
If Dir(répertoirePhotos, vbDirectory) = ""
Then MkDir répertoirePhotos
Set f = Sheets("liste")
For Each c In f.Range("liste")
lig = [liste].Find(c, LookAt:=xlWhole).Row
col = [liste].Column + 1
For Each s In f.Shapes
If s.TopLeftCell.Address = Cells(lig,
col).Address Then
H = s.Height
L = s.Width
s.Copy
f.ChartObjects.Add(0,
0, L, H).Chart.Paste
f.ChartObjects(1).Border.LineStyle
= 0
f.ChartObjects(1).Chart.Export
Filename:= _
répertoirePhotos
& c & ".jpg", FilterName:="jpg"
f.ChartObjects(1).Delete
End If
Next s
Next c
UserForm1.Show
End Sub
'Pour récupérer le formulaire: clic-droit
sur Userform1/exporter
Dim début, n, répertoirePhotos
Private Sub UserForm_Initialize()
début = 1
n = 3
Me.ScrollBar1.Min = 1
Me.ScrollBar1.Max = [liste].Count - n + 1
affiche
End Sub
Sub affiche()
répertoirePhotos = "c:\photos\" '
Adapter
For i = 1 To n
Me("Image" & i).Picture = LoadPicture(répertoirePhotos
& Range("liste").Cells(i + début - 1, 1) & ".jpg")
Me("Image" & i).ControlTipText =
Range("liste").Cells(i + début - 1, 1)
Me("Image" & i).BorderStyle = 0
Me("Label" & i).Caption = Range("liste").Cells(i
+ début - 1, 1)
Next i
Me.Repaint
End Sub
Private Sub ScrollBar1_Change()
début = ScrollBar1
affiche
End Sub
Sub ChoixClick(p, nom)
For i = 1 To n
Me("Image" & i).BorderStyle = 0
Next i
Me("Image" & p).BorderStyle = 1
If ActiveCell.Column = 3 Then
ActiveCell.Offset(, -1) = nom
For Each s In ActiveSheet.Shapes
If Not Intersect(s.TopLeftCell, ActiveCell)
Is Nothing Then s.Delete
Next s
Set Img = ActiveSheet.Pictures.Insert(répertoirePhotos
& nom & ".jpg")
Img.Left = ActiveCell.Left
Img.Top = ActiveCell.Top
End If
End Sub
Private Sub Image1_Click()
ChoixClick 1, Me.Image1.ControlTipText
End Sub
Private Sub Image2_Click()
ChoixClick 2, Me.Image2.ControlTipText
End Sub
Private Sub Image3_Click()
ChoixClick 3, Me.Image3.ControlTipText
End Sub
Les photos des commentaires sont exportées sous forme de JPG dans le répertoire c:\photos\
Sub auto_open()
répertoirePhotos = "c:\photos\" ' Adapter
If Dir(répertoirePhotos, vbDirectory) = ""
Then MkDir répertoirePhotos
Set f = Sheets("liste")
For Each c In f.Range("a2:a" & f.[a65000].End(xlUp).Row)
c.Comment.Visible = True
H = c.Comment.Shape.Height
L = c.Comment.Shape.Width
c.Comment.Shape.CopyPicture
c.Comment.Visible = False
f.ChartObjects.Add(0, 0, L, H).Chart.Paste
f.ChartObjects(1).Border.LineStyle = 0
f.ChartObjects(1).Chart.Export Filename:= _
répertoirePhotos & c & ".jpg",
FilterName:="jpg"
f.ChartObjects(1).Delete
Next c
UserForm1.Show
End Sub
Code du formulaire
'Pour récupérer le formulaire: clic-droit
sur Userform1/exporter
Dim début, n, répertoirePhoto
Private Sub UserForm_Initialize()
début = 1
n = 3
Me.ScrollBar1.Min = 1
Me.ScrollBar1.Max = [Liste].Count - n + 1
affiche
End Sub
Sub affiche()
répertoirePhotos = "c:\photos\" ' Adapter
For i = 1 To n
Me("Image" & i).Picture = LoadPicture(répertoirePhotos
& Range("liste").Cells(i + début - 1, 1) & ".jpg")
Me("Image" & i).ControlTipText =
Range("liste").Cells(i + début - 1, 1)
Me("Image" & i).BorderStyle = 0
Me("Label" & i).Caption = Range("liste").Cells(i
+ début - 1, 1)
Next i
Me.Repaint
End Sub
Private Sub ScrollBar1_Change()
début = ScrollBar1
affiche
End Sub
Sub ChoixClick(p, nom)
For i = 1 To n
Me("Image" & i).BorderStyle = 0
Next i
Me("Image" & p).BorderStyle = 1
ActiveCell = nom
[Liste].Find(nom, LookAt:=xlWhole).Copy
ActiveCell.PasteSpecial Paste:=xlPasteFormats
ActiveCell.PasteSpecial Paste:=xlPasteComments
End Sub
Private Sub Image1_Click()
ChoixClick 1, Me.Image1.ControlTipText
End Sub
Private Sub Image2_Click()
ChoixClick 2, Me.Image2.ControlTipText
End Sub
Private Sub Image3_Click()
ChoixClick 3, Me.Image3.ControlTipText
End Sub
Private Sub Label1_Click()
ChoixClick 1, Me.Label1.Caption
End Sub
Private Sub Label2_Click()
ChoixClick 2, Me.Label2.Caption
End Sub
Private Sub Label3_Click()
ChoixClick 3, Me.Label3.Caption
End Sub
ChoixPhotoExterneListBox
ChoixPhotoExterneListBoxCmt
'Pour récupérer le formulaire: clic-droit
sur Userform1/exporter
Dim début, n, répertoirePhoto
Private Sub UserForm_Initialize()
début = 1
n = 3
Me.ScrollBar1.Min = 1
Me.ScrollBar1.Max = [Liste].Count - n + 1
affiche
End Sub
Sub affiche()
répertoirePhoto = "c:\mesdoc\"
' Adapter
For i = 1 To n
Me("Image" & i).Picture = LoadPicture(répertoirePhoto
& Range("liste").Cells(i + début - 1, 1) & ".jpg")
Me("Image" & i).ControlTipText =
Range("liste").Cells(i + début - 1, 1)
Me("Image" & i).BorderStyle = 0
Next i
Me.Repaint
End Sub
Private Sub ScrollBar1_Change()
début = ScrollBar1
affiche
End Sub
Private Sub Image1_Click()
ChoixClick 1
End Sub
Private Sub Image2_Click()
ChoixClick 2
End Sub
Private Sub Image3_Click()
ChoixClick 3
End Sub
Sub ChoixClick(p)
For i = 1 To n
Me("Image" & i).BorderStyle = 0
Next i
Me("Image" & p).BorderStyle = 1
If ActiveCell.Column = 3 Then
nom = Me("image" & p).ControlTipText
ActiveCell.Offset(, -1) = nom
Set Img = ActiveSheet.Pictures.Insert(répertoirePhoto
& nom & ".jpg")
Img.Left = ActiveCell.Left
Img.Top = ActiveCell.Top
End If
End Sub
Private Sub Workbook_SheetActivate(ByVal Sh As Object)
Set f = Sheets("Dossier images")
If UCase(Sh.Name) <> UCase(f.Name) Then
nom = Sheets(Sh.Name).[B1]
For Each s In Sheets(Sh.Name).Shapes
If UCase(s.Name) = UCase(nom)
Then existe = True
Next s
If Not existe Then
Set adr = f.[2:2].Find(nom, LookAt:=xlWhole)
If Not adr Is Nothing Then
col = adr.Column
For Each s In f.Shapes
If s.TopLeftCell.Address
= f.Cells(1, col).Address Then
Sheets(Sh.Name).[A1].Select
s.Copy
Sheets(Sh.Name).Paste
Selection.Name
= nom
Selection.Left
= Sheets(Sh.Name).[A1].Left + 5
Selection.Top
= Sheets(Sh.Name).[A1].Top + 5
End If
Next s
End If
End If
End If
End Sub
Function affiche(champ As Range)
Application.Volatile
Set f = Sheets(Application.Caller.Parent.Name)
For Each s In f.Shapes
If UCase(Left(s.Name, 3)) = "IMG" Then
s.Visible = False
Next
For Each c In champ
On Error Resume Next
If Not c.EntireRow.Hidden Then f.Shapes("img"
& c.Value).Visible = True
Next c
affiche = ""
End Function
Sub CréeFlèches()
ActiveSheet.Shapes.AddShape(7, 6, 6, 6, 6).Name = "Flèche"
ActiveSheet.Shapes.AddShape(msoShapeRectangle, 10, 10, 11,
10).Name = "Rect"
ActiveSheet.Shapes("Flèche").Fill.ForeColor.SchemeColor
= 0
ActiveSheet.Shapes("Rect").Fill.ForeColor.SchemeColor
= 22
ActiveSheet.Shapes("Flèche").IncrementRotation
180
ActiveSheet.Shapes("Flèche").Line.Visible
= msoFalse
ActiveSheet.Shapes("Rect").Line.Visible = msoFalse
For Each c In Cells.SpecialCells(xlCellTypeAllValidation)
ActiveSheet.Shapes("Rect").Copy
c.Offset(, 1).Select
ActiveSheet.Paste
Selection.Name = c.Address & "x"
Selection.Left = c.Offset(, 1).Left + 2
Selection.Top = c.Offset(, 1).Top + 1
Selection.Height = c.Offset(, 1).Height - 1
Selection.OnAction = "clicFlèche"
ActiveSheet.Shapes("flèche").Copy
c.Offset(, 1).Select
ActiveSheet.Paste
Selection.Name = c.Address
Selection.Left = c.Offset(, 1).Left + 4
Selection.Top = c.Offset(, 1).Top + 3
Selection.Height = c.Offset(, 1).Height - 7
Selection.OnAction = "clicFlèche"
Next c
ActiveSheet.Shapes("flèche").Delete
ActiveSheet.Shapes("rect").Delete
End Sub
Sub ClicFlèche()
Range(ActiveSheet.Shapes(Application.Caller).TopLeftCell.Address).Offset(,
-1).Select
SendKeys "%{down}"
End Sub
Dans un onglet Classement, on récupère les informations de la feuille BD dans l'ordre décroissant des points avec les formules.
En D2:=SI(LIGNES($1:1)<=NBVAL(Noms);GRANDE.VALEUR(points;LIGNES($1:1));0)
En B2:=SI(LIGNES($1:1)<=NBVAL(Noms);
INDEX(Noms;PETITE.VALEUR(SI(points=D2;LIGNE(INDIRECT("1:"&LIGNES(Noms))));NB.SI(D$2:D2;D2)));"")
Pour récupérer les images de la feuille BD.
Private Sub Worksheet_Activate()
Application.ScreenUpdating = flase
For Each s In ActiveSheet.Shapes
If s.Type = 13 Then s.Delete
Next s
For Each c In [B2:B20]
If c <> "" Then
lig = [noms].Find(c, LookAt:=xlWhole).Row
col = [noms].Column + 1
For Each s In Sheets("bd").Shapes
If s.TopLeftCell.Address
= Cells(lig, col).Address Then s.Copy
Next s
ActiveSheet.Paste
Selection.ShapeRange.Left = c.Offset(,
1).Left + 7
Selection.ShapeRange.Top = c.Top +
5
End If
Next c
End Sub
Sub appel()
NomShape = Application.Caller
ActiveSheet.Shapes(NomShape).TextFrame.Characters(Start:=1,
Length:=15).Font.Bold = _
Not ActiveSheet.Shapes(NomShape).TextFrame.Characters(Start:=1,
Length:=1).Font.Bold
End Sub
Met en gras et en couleur le bouton appelant
Sub appelBoutons2()
For Each c In ActiveSheet.Shapes
If c.Type = 8 And Left(c.Name, 4) <>
"Drop" Then
c.TextFrame.Characters(Start:=1, Length:=1).Font.Bold
= False
c.TextFrame.Characters(Start:=1, Length:=1).Font.ColorIndex
= 0
End If
Next c
nomshape = Application.Caller
'[A1] = ActiveSheet.Shapes(nomshape).TextFrame.Characters.Text
ActiveSheet.Shapes(nomshape).TextFrame.Characters(Start:=1,
Length:=1).Font.Bold = True
ActiveSheet.Shapes(nomshape).TextFrame.Characters(Start:=1,
Length:=1).Font.ColorIndex = 3
'--- Filtre
lettre = ActiveSheet.Shapes(nomshape).TextFrame.Characters.Text
critère = "=" & lettre & "*"
Range("B4").Select
Selection.AutoFilter Field:=1, Criteria1:=critère
End Sub
Sub essai()
MsgBox NomImgCel(Range("C3"), "feuil1")
MsgBox NomImgCel(Range("A1"), "feuil1")
End Sub
Function NomImgCel(ByVal c As Range, f)
NomImgCel = ""
For Each s In Sheets(f).Shapes
If s.TopLeftCell.Address = c.Address Then NomImgCel
= s.Name
Next s
End Function
Donne la couleur d'une image.
Function couleurImage(s)
Application.Volatile
Set f = Sheets(Application.Caller.Parent.Name)
couleurImage = f.Shapes(s).Fill.ForeColor.RGB
End Function
En fonction du total en B8, on colorie l'image ZT1
-Au dessus de 500 -> Vert
-Entre 250 et 500 -> Orange
-Moins de 250 -> Rouge
=ColorieImage("ZT1";SI(B8>500;65025;SI(B8>250;4626167;255)))
Function ColorieImage(s, couleur)
Application.Volatile
Set f = Sheets(Application.Caller.Parent.Name)
f.Shapes(s).Fill.ForeColor.RGB = couleur
End Function
Colorier
Image
Colorier Image 3
Colorier Image 3 B
Colorier
Image 4
Indicateur
Indicateur Image autre feuille
Colorier Indicateurs
Colorier Indicateurs2
Colorier Croix
=modifietexte("monimage";B15)
Function modifieTexte(nomImage, libellé)
Application.Volatile
Set f = Sheets(Application.Caller.Parent.Name)
f.Shapes(nomImage).TextFrame.Characters.Text = libellé
modifieTexte = ""
End Function
Colorier Image Couleur Cellule
Function ColorieImage(s, couleur)
Application.Volatile
Set f = Sheets(Application.Caller.Parent.Name)
f.Shapes(s).Fill.ForeColor.RGB = couleur
End Function
Function CouleurCellule(c As Range)
Application.Volatile
CouleurCellule = c.Interior.Color
End Function
Sur l'exemple ci dessous, on colorie des shapes avec la couleur d'un autre shape.
=colorieimage("MonTriangle";couleurimage("monshape"))
Function ColorieImage(s, couleur)
Application.Volatile
Set f = Sheets(Application.Caller.Parent.Name)
f.Shapes(s).Fill.ForeColor.RGB = couleur
End Function
Function couleurImage(s)
Application.Volatile
Set f = Sheets(Application.Caller.Parent.Name)
couleurImage = f.Shapes(s).Fill.ForeColor.RGB
End Function
Colorier Image Couleur Cellule2
Sur cette version, le coloriage des états se fait en fonction du CA.
Pour colorier les départements en fonction du CA:
Colorier
Carte France
Colorier Carte
France Nom
Colorier Carte France
CA
Colorier Carte France
Nom Responsables
Colorier Carte France
Clic
Colorier Carte France
CA Fonction
Colorier Carte des Régions
Sub coloriage()
For Each c In [départ]
If c <> "" Then
ca = c.Offset(, 1)
p = Application.Match(ca, [légende],
1)
couleur = Range("légende").Cells(p,
1).Interior.Color
ActiveSheet.Shapes("fr-"
& c).Fill.ForeColor.RGB = couleur
End If
Next c
End Sub
Pour écrire les numéros de département:
Sub EcritNoDepart()
For Each c In [départ]
If c <> "" Then ecritShape "fr-"
& c, c
Next c
c = "54": ecritShape "fr-" & c, c,
"Bas"
c = "192": ecritShape "fr-" & c, Right(c,
2), , "Gauche"
For Each c In Array("175", "193", "194")
ecritShape "fr-" & c, Right(c, 2)
Next c
End Sub
Pour obtenir des infos-bulles au survol des départements:
Sub bulles()
For Each s In ActiveSheet.Shapes
If s.Type <> 8 Then
ActiveSheet.Hyperlinks.Add Anchor:=s, Address:="",
SubAddress:=""
tmp = Mid(s.Name, 4)
bulle = Application.VLookup(tmp, [departca],
2, False)
If Not IsError(bulle) Then
libdep = Application.VLookup(tmp,
[departca], 3, False)
s.Hyperlink.ScreenTip = libdep
& " Ca:" & Format(bulle, "# ##0") & Chr(10)
Else
s.Hyperlink.ScreenTip = "...."
End If
End If
Next s
End Sub
Pour déclencher une macro sur le clic d'un shape:
Sub GenereOnAction()
For Each c In [départ]
If c <> "" Then ActiveSheet.Shapes("fr-"
& c).OnAction = "ClicDepart"
Next c
End Sub
Sub clicDepart()
dep = Mid(Application.Caller, 4)
[i2] = Application.VLookup(dep, [départca], 3, False)
[j2] = Format(Application.VLookup(dep, [départca],
2, False), "##,0")
End Sub
Pour grouper/dégrouper les shapes
Sub grouperShapes()
Dim a(), i
For Each s In ActiveSheet.Shapes
If s.Type = 5 Then
i = i + 1: ReDim Preserve
a(1 To i): a(i) = s.Name
End If
Next
ActiveSheet.Shapes.Range(a).Group.Name = "CarteFrance"
End Sub
Sub degrouperShapes()
ActiveSheet.Shapes("CarteFrance").Ungroup
End Sub
Colorier Carte
Europe
Colorier Carte Europe 2
Colorier Carte Europe 3
Colorier Carte Belgique
Le pays cherché est choisi dans un menu déroulant.
Le pays choisi est colorié en rouge.
Des Info-Bulles (nom du pays) apparaissent au survol de chaque pays.
Sub Info_bulles()
For Each c In [Pays]
ActiveSheet.Hyperlinks.Add Anchor:=ActiveSheet.Shapes(c),
Address:="", SubAddress:=""
ActiveSheet.Shapes(c).Hyperlink.ScreenTip = c
Next c
End Sub
-Avec cette version, on demande de situer un pays choisi
au hasard en cliquant sur le pays.
-Si on clique sur un pays, il est affiché en rouge.
Colorier Carte Monde Interrogation
Carte Monde Densité population
Sub coloriageDensité()
For Each c In [pays]
If Not IsError(c.Offset(, 9)) Then
popul = CDbl(c.Offset(, 9))
p = Application.Match(popul, [légende],
1)
couleur = Range("légende").Cells(p,
1).Interior.Color
ActiveSheet.Shapes(c).Fill.ForeColor.RGB
= couleur
End If
Next c
End Sub
Fonction affichage libellé sur un shape
=EcritShape(B3;N3)
Function ecritShape(nomShape, Libellé)
Application.Volatile
With ActiveSheet.Shapes(nomShape).TextFrame2.TextRange
.Characters.Text = Libellé
.Characters.Font.Size = 8
.Parent.VerticalAnchor = msoAnchorMiddle
.Parent.HorizontalAnchor = msoAnchorCenter
End With
ecritShape = ""
End Function
Autre exemple
On colorie 2 flèches en fonction des valeurs en E2 et E3
=colorieimage("Axe_site1";couleur(DECALER($A$2;EQUIV(Taux_site1;légende;1)-1;)))
Cette fonction crée un Shape superposé à la photo et écrit un texte dans ce shape
AfficheTexte(groupe As Range, NomShape, Libelle)
=afficheTexte($B$3:$B$8;B3;B3)
Affichage
texte photo
Affichage texte forme
libre
Function TailleImage(s, largeur, hauteur)
Application.Volatile
Set f = Sheets(Application.Caller.Parent.Name)
f.Shapes(s).Width = largeur
f.Shapes(s).Height = hauteur
End Function
End Function
1- avec la boite à outils contrôles,
créer un contrôle image
2- dans la propriété Picture, choisir l'image
externe
3- créer une zone de texte et la nommer cmt
Private Sub Image1_MouseMove(ByVal Button As Integer, ByVal
Shift As Integer, ByVal X As Single, ByVal Y As Single)
If X < 10 Or X > Image1.Width - 10 Or Y < 10 Or Y
> Image1.Height - 10 Then
ActiveSheet.Shapes("cmt").Visible =
False
Else
ActiveSheet.Shapes("cmt").Visible =
True
End If
End Sub
Private Sub Image1_MouseMove(ByVal Button As Integer, ByVal
Shift As Integer, ByVal X As Single, ByVal Y As Single)
If X < 10 Or X > Image1.Width - 10 Or Y < 10 Or Y
> Image1.Height - 10 Then
ActiveSheet.Shapes("Bretagne").Fill.ForeColor.RGB
= RGB(255, 255, 255)
Else
ActiveSheet.Shapes("Bretagne").Fill.ForeColor.RGB
= RGB(0, 255, 0)
End If
End Sub
Private Sub Image2_MouseMove(ByVal Button As Integer, ByVal
Shift As Integer, ByVal X As Single, ByVal Y As Single)
If X < 10 Or X > Image2.Width - 10 Or Y < 10 Or Y
> Image2.Height - 10 Then
ActiveSheet.Shapes("Basse-normandie").Fill.ForeColor.RGB
= RGB(255, 255, 255)
Else
ActiveSheet.Shapes("Basse-normandie").Fill.ForeColor.RGB
= RGB(0, 255, 0)
End If
End Sub
Private Sub Image3_MouseMove(ByVal Button As Integer, ByVal
Shift As Integer, ByVal X As Single, ByVal Y As Single)
If X < 10 Or X > Image3.Width - 10 Or Y < 10 Or Y
> Image3.Height - 10 Then
ActiveSheet.Shapes("pays-de-loire").Fill.ForeColor.RGB
= RGB(255, 255, 255)
Else
ActiveSheet.Shapes("pays-de-loire").Fill.ForeColor.RGB
= RGB(0, 255, 0)
End If
End Sub
Private Sub Image1_Click()
razShapes
Sheets("bretagne").Select
End Sub
Private Sub Image2_Click()
razShapes
Sheets("Basse-normandie").Select
End Sub
Private Sub Image3_Click()
razShapes
Sheets("pays-de-loire").Select
End Sub
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
razShapes
End Sub
Sub razShapes()
For Each s In ActiveSheet.Shapes
s.Fill.ForeColor.RGB = RGB(255, 255, 255)
sNext s
End Sub
Permet de dessiner dans une cellule ou un champ des drapeaux
Français ou Italien.
Si du texte est déjà présent dans la cellule ou le
champ, il apparaît par transparence.
=DrapeauFR(champDrapeau; transparence)
=DrapeauIT(champDrapeau; transparence)
Sur l'exemple:
En C2: =drapeauFR(A2;50%)
En C9: =drapeauIT(A9;50%)
ComboBox
images
ListBox images