XL 2016 VBA ajouter commentaire

chifounou

XLDnaute Occasionnel
Bonjour,

J ai enregistré une macro consistant à ajouter un commentaire à une cellule. Voici le code pondu

Range("L11").AddComment
Range("L11").Comment.Visible = False
Range("L11").Comment.Text Text:= _
"* depends :" & Chr(10) & "- yes with old licences acquired before july 16, 2014" & Chr(10) & "- N/A with new licences acquired after july 16, 2014"
Selection.ShapeRange.ScaleWidth 0.97, msoFalse, msoScaleFromTopLeft
Selection.ShapeRange.ScaleHeight 0.85, msoFalse, msoScaleFromTopLeft

Problème, lorsque j'exécute la macro celà bug à la ligne en gras qui normalement vise à redimensionner la bulle

Autre problème, le texte dans la bulle n'est pas en gras alors qu'il est en gras par défaut au moment où j'enregistre la macro...

Votre aide sera la bienvenue
 

Lone-wolf

XLDnaute Barbatruc
Re chifounou

With Range("L11")
chaineCherchée = "Ceci"
If .Comment Is Nothing Then
.AddComment ' Création commentaire
.Comment.Text Text:="Ceci est un commentaire..."
.Comment.Shape.TextFrame.Characters(Start:=1, Length:= _
Len(chaineCherchée)).Font.ColorIndex = 3
.Comment.Shape.TextFrame.AutoSize = True
End If
End With
 

chifounou

XLDnaute Occasionnel
Merci Lonewolf ! par contre au lieu de gras (noir épais), le texte est fin et de couleur rouge :)

edit: la valeur 1 (au lieu de 3) est noir fin, pas gras
y'a-t'il une charte de référence quelque part ?
 
Dernière édition:

Lone-wolf

XLDnaute Barbatruc
Bonjour chifounou :), le Forum :)

Si j'ai bien compris

With .Comment.Shape.TextFrame.Characters(Start:=1, Length:= Len(chaineCherchée))
.Font.ColorIndex = 0 Pour la couleur noir
.Font.Bold = False Non Gras - parce-que très mauvais pour le coeur :D
End with

EDIT: un autre exemple. Attention concernant chaine2, il faut compter aussi les espaces.

VB:
Option Explicit
Sub test()
Dim chaine1$, chaine2$

    With Range("g2")
        chaine1 = "Ceci"
        chaine2 = "commentaire"
        .Comment.Delete
        If .Comment Is Nothing Then
            .AddComment
            .Comment.Text Text:="Ceci est un commentaire..."
            With .Comment.Shape.TextFrame.Characters(Start:=1, Length:=Len(chaine1))
                .Font.Color = vbBlack
                .Font.Bold = True
            End With

            With .Comment.Shape.TextFrame.Characters(Start:=13, Length:=Len(chaine2))
                .Font.Color = vbRed
                .Font.Bold = True
            End With
            .Comment.Shape.TextFrame.AutoSize = True
        End If
    End With
End Sub

Et si ça peut t'interésser, de Jacques Boigontier

VB:
Option Explicit

Sub InsèreImageCommentaireCelluleActive()
Dim nf$

    nf = ThisWorkbook.Path & "\Images\" & ActiveCell.Value & ".gif"
    If nf = False Then Exit Sub

    With ActiveCell
        .ClearComments
        .AddComment
        .Comment.Shape.Fill.UserPicture nf
        .Comment.Shape.Height = 50
        .Comment.Shape.Width = 50
        .Comment.Shape.ScaleHeight 1.2, msoFalse, msoScaleFromTopLeft
    End With
End Sub
 
Dernière édition:

DoubleZero

XLDnaute Barbatruc
Bonjour, chifounou :), Lone-wolf :), le Forum,
... bug à la ligne en gras qui normalement vise à redimensionner la bulle... le texte dans la bulle n'est pas en gras...
Comme ceci ?
VB:
Option Explicit
Sub Commentaire_insérer()
    With ActiveCell
        .ClearComments
        .AddComment
        With .Comment
            .Shape.Height = 50
            .Shape.Width = 50
            .Text Text:="* depends :" & Chr(10) & "- yes with old licences acquired before july 16, 2014" _
                & Chr(10) & "- N/A with new licences acquired after july 16, 2014"
            .Shape.TextFrame.Characters.Font.Bold = True
            .Shape.ScaleWidth 1.39, msoFalse, msoScaleFromTopLeft
            .Shape.ScaleHeight 2.5, msoFalse, msoScaleFromTopLeft
        End With
        .Select
    End With
End Sub
A bientôt :)
 

chifounou

XLDnaute Occasionnel
Merci à vous

Voici ma version finale, complètement dans les clous

Code:
With Range("L11")
            .AddComment
            With .Comment
                .Text Text:="* depends :" & Chr(10) & "- yes with old licences acquired before july 16, 2014" _
                    & Chr(10) & "- N/A with new licences acquired after july 16, 2014"
                .Shape.TextFrame.Characters.Font.Bold = True
                .Shape.TextFrame.AutoSize = True
            End With
        End With
 

Lone-wolf

XLDnaute Barbatruc
Bonjour DoubleZero :) , chifounou :) , le Forum :)

Juste pour te montrer qu'est-ce que ça donne avec la 2ème macro.

commentaire.gif


Note: dans la 2ème, il y avait une erreur d'écriture.

VB:
Option Explicit

Sub test()
Dim chaine1$, chaine2$
         
chaine1 = "Ceci"
chaine2 = "commentaire"

    With Range("g5")
         .ClearComments
        If .Comment Is Nothing Then
            .AddComment
            .Comment.Text Text:="Ceci est un commentaire..."
            .Comment.Shape.TextFrame.AutoSize = True
            With .Comment.Shape.TextFrame.Characters(Start:=1, Length:=Len(chaine1))
                .Font.Color = vbBlack
                .Font.Bold = True
            End With

            With .Comment.Shape.TextFrame.Characters(Start:=13, Length:=Len(chaine2))
                .Font.Color = vbRed
                .Font.Bold = True
            End With
        End If
    End With

End Sub
 

chifounou

XLDnaute Occasionnel
Bonjour,

Petit fail ici

J'ai un commentaire de 4 lignes (avec retours à la ligne) que j'aimerais avoir en gras pour les 2 premiers lignes et sans gras pour les 2 dernières, le tout en actionnant cette macro

Je ne sais pas où je commets l'erreur mais ça ne marche pas (tout est en non-gras). Merci pour votre aide
 
Dernière édition:

Statistiques des forums

Discussions
311 729
Messages
2 081 971
Membres
101 852
dernier inscrit
dthi16088