afficher une image en cliquant sur une cellule

alfazoulou

XLDnaute Occasionnel
Bonjour le forum.
je voudrais pouvoir afficher une image dans un rectangle (pour qu'elle soit toujour à la mème place) en cliquant sur une cellule, jusque là je sais faire.
Mais je voudrais dans ce meme rectangle pouvoir afficher une autre image si je clique sur une autre cellule.
autrement dit, à differentes cellules correspondent differentes images qui seront affichées dans le rectangle si on clique dessus (ou double clic, ou clic droit comme dans mon exemple ci dessous.

Private Sub Worksheet_BeforeRightClick(ByVal Target As Range, Cancel As Boolean)
If Intersect(Range("A9"), Target) Is Nothing Then Exit Sub
Cancel = True
ActiveSheet.Shapes("Rectangle 26").Select
Selection.ShapeRange.Fill.Transparency = 0#
Selection.ShapeRange.Line.Weight = 0.75
Selection.ShapeRange.Line.DashStyle = msoLineSolid
Selection.ShapeRange.Line.Style = msoLineSingle
Selection.ShapeRange.Line.Transparency = 0#
Selection.ShapeRange.Line.Visible = msoTrue
Selection.ShapeRange.Line.ForeColor.SchemeColor = 64
Selection.ShapeRange.Line.BackColor.RGB = RGB(255, 255, 255)
Selection.ShapeRange.Fill.Visible = msoTrue
Selection.ShapeRange.Fill.ForeColor.RGB = RGB(255, 255, 255)
Selection.ShapeRange.Fill.BackColor.RGB = RGB(255, 255, 255)
Selection.ShapeRange.Fill.UserPicture _
"C:\Documents and Settings\toto\Bureau\porte.jpg"
Range("F22").Select

If Intersect(Range("A13"), Target) Is Nothing Then Exit Sub
Cancel = True
ActiveSheet.Shapes("Rectangle 26").Select
Selection.ShapeRange.Fill.Transparency = 0#
Selection.ShapeRange.Line.Weight = 0.75
Selection.ShapeRange.Line.DashStyle = msoLineSolid
Selection.ShapeRange.Line.Style = msoLineSingle
Selection.ShapeRange.Line.Transparency = 0#
Selection.ShapeRange.Line.Visible = msoTrue
Selection.ShapeRange.Line.ForeColor.SchemeColor = 64
Selection.ShapeRange.Line.BackColor.RGB = RGB(255, 255, 255)
Selection.ShapeRange.Fill.Visible = msoTrue
Selection.ShapeRange.Fill.ForeColor.RGB = RGB(255, 255, 255)
Selection.ShapeRange.Fill.BackColor.RGB = RGB(255, 255, 255)
Selection.ShapeRange.Fill.UserPicture _
"C:\Documents and Settings\toto\Bureau\24_portegarage.jpg"
Range("F22").Select
Exit Sub
End Sub

la première partie du code fonctionne mais pas la seconde.
pouvez vous m'aider svp ?
 

tototiti2008

XLDnaute Barbatruc
Re : afficher une image en cliquant sur une cellule

Bonjour alfazoulou,

en mettant

If Intersect(Range("A9"), Target) Is Nothing Then Exit Sub

en début de code, tu dis : si ce n'est pas en A9 que l'on fait un clic droit, arrêtes tout. il n'arrive donc jamais au test de A13...

il serait peut-être plus logique de faire :

Code:
If not Intersect(Range("A9"), Target) Is Nothing Then 
Cancel = True
ActiveSheet.Shapes("Rectangle 26").Select
Selection.ShapeRange.Fill.Transparency = 0#
Selection.ShapeRange.Line.Weight = 0.75
Selection.ShapeRange.Line.DashStyle = msoLineSolid
Selection.ShapeRange.Line.Style = msoLineSingle
Selection.ShapeRange.Line.Transparency = 0#
Selection.ShapeRange.Line.Visible = msoTrue
Selection.ShapeRange.Line.ForeColor.SchemeColor = 64
Selection.ShapeRange.Line.BackColor.RGB = RGB(255, 255, 255)
Selection.ShapeRange.Fill.Visible = msoTrue
Selection.ShapeRange.Fill.ForeColor.RGB = RGB(255, 255, 255)
Selection.ShapeRange.Fill.BackColor.RGB = RGB(255, 255, 255)
Selection.ShapeRange.Fill.UserPicture _
"C:\Documents and Settings\toto\Bureau\porte.jpg"
Range("F22").Select
Elseif not Intersect(Range("A13"), Target) Is Nothing then
Cancel = True
ActiveSheet.Shapes("Rectangle 26").Select
Selection.ShapeRange.Fill.Transparency = 0#
Selection.ShapeRange.Line.Weight = 0.75
Selection.ShapeRange.Line.DashStyle = msoLineSolid
Selection.ShapeRange.Line.Style = msoLineSingle
Selection.ShapeRange.Line.Transparency = 0#
Selection.ShapeRange.Line.Visible = msoTrue
Selection.ShapeRange.Line.ForeColor.SchemeColor = 64
Selection.ShapeRange.Line.BackColor.RGB = RGB(255, 255, 255)
Selection.ShapeRange.Fill.Visible = msoTrue
Selection.ShapeRange.Fill.ForeColor.RGB = RGB(255, 255, 255)
Selection.ShapeRange.Fill.BackColor.RGB = RGB(255, 255, 255)
Selection.ShapeRange.Fill.UserPicture _
"C:\Documents and Settings\toto\Bureau\24_portegarage.jpg"
Range("F22").Select
end if

bien que ce soit sans doute simplifiable, puisque tu as de nombreuses lignes en commun pour les 2 cas...
 

Discussions similaires

Statistiques des forums

Discussions
311 720
Messages
2 081 907
Membres
101 836
dernier inscrit
karmon