Microsoft 365 VBA : Commentaire en B, C, D après clic droit dans n'importe quelle cellule de cette plage ?

  • Initiateur de la discussion Initiateur de la discussion anthoYS
  • Date de début Date de début

Boostez vos compétences Excel avec notre communauté !

Rejoignez Excel Downloads, le rendez-vous des passionnés où l'entraide fait la force. Apprenez, échangez, progressez – et tout ça gratuitement ! 👉 Inscrivez-vous maintenant !

anthoYS

XLDnaute Barbatruc
bonjour


l'objectif est de pouvoir dimensionner la case du commentaire prêt à être saisi. et d'afficher une case vierge pour commentaire...
et qu'il n'apparaisse qu'au survol.
pourtant ici, il apparaît le dernier toujours ouvert, or ce n'est pas l'objectif...


Comment faire pour que le fond du commentaire soit blanc par défaut et non jaunâtre ?


voici le code:

VB:
Private Sub Worksheet_BeforeRightClick(ByVal Target As Range, Cancel As Boolean)
    ' Applique aux colonnes B, C, D (2, 3, 4)
    If Target.Column >= 2 And Target.Column <= 4 Then
        Cancel = True ' Annule le menu contextuel
       
        ' Configuration de l'affichage des commentaires
        Application.DisplayCommentIndicator = xlCommentIndicatorOnly
       
        ' Crée un nouveau commentaire si nécessaire
        If Target.Comment Is Nothing Then
            Target.AddComment Text:="" ' Crée un commentaire vide
        End If
       
        ' Rend le commentaire visible et éditable
        Target.Comment.Visible = True
        Target.Comment.Shape.Select ' Active l'édition immédiate
    End If
    If Target.Comment Is Nothing Then
    Target.AddComment Text:=""
    ' Ne pas mettre Target.Comment.Visible = True
End If
' Ne pas forcer l'affichage du commentaire
End Sub


Merci par avance
 

Pièces jointes

Solution
Et un bonus si tu souhaites changer la couleur du texte du com (j'utilise actuellement sur mon calendrier)
je n'ai pas activé mais si tu souhaites

Code:
Option Explicit
Private lastCmt As Comment

Private Sub Worksheet_BeforeRightClick(ByVal Target As Range, Cancel As Boolean)
    Const FIRST_COL As Long = 2
    Const LAST_COL  As Long = 4
    
    If Target.Column < FIRST_COL Or Target.Column > LAST_COL Then Exit Sub
    Cancel = True
    Application.DisplayCommentIndicator = xlCommentIndicatorOnly

    If Not lastCmt Is Nothing Then
        If Not lastCmt Is Target.Comment Then lastCmt.Visible = False
        Set lastCmt = Nothing
    End If

    Dim cmt As Comment                     'ou As Object si AddComment2
    If Target.Comment...
Bonjour,

Une idée si j'ai bien compris,

VB:
Option Explicit
Private lastCmt As Comment

Private Sub Worksheet_BeforeRightClick(ByVal Target As Range, Cancel As Boolean)
    If Target.Column < 2 Or Target.Column > 4 Then Exit Sub
   
    Cancel = True
    Application.DisplayCommentIndicator = xlCommentIndicatorOnly
   
    If Not lastCmt Is Nothing Then
        lastCmt.Visible = False
        Set lastCmt = Nothing
    End If

    If Target.Comment Is Nothing Then
        Target.AddComment "" 'sinon AddComment2 pour Office 365
        With Target.Comment.Shape
            .Width = 180
            .Height = 60
            .Fill.ForeColor.RGB = vbWhite
           ' .Line.Visible = msoFalse
        End With
    End If

    With Target.Comment
        .Visible = True
        .Shape.Select
    End With
   
    Set lastCmt = Target.Comment
End Sub

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If Not lastCmt Is Nothing Then
        lastCmt.Visible = False
        Set lastCmt = Nothing
    End If
End Sub
 

Pièces jointes

Bonjour
comment transforme t on les commentaire en postit auto hidden
comme ceci:
VB:
Private Sub Worksheet_BeforeRightClick(ByVal Target As Range, Cancel As Boolean)
    ' Applique aux colonnes B, C, D (2, 3, 4)
    If Target.Column >= 2 And Target.Column <= 4 And Target.Count = 1 Then
        Cancel = True ' Annule le menu contextuel
        
        ' Configuration de l'affichage des commentaires
        Application.DisplayCommentIndicator = False 'il ne s'affiche pas au survol
        
        ' Crée un nouveau commentaire si nécessaire
        If Target.Comment Is Nothing Then
            Target.AddComment Text:="" ' Crée un commentaire vide
        End If
        
        ' Rend le commentaire visible et éditable
        On Error Resume Next
        With Target.Comment
            .Visible = True 'on le met visible d'office (pas besoins du survol)
            With .Shape
                .Width = 200 'on dimensionne le width
                .Height = 150 'on dimensionne le height
                .Select ' Active l'édition immédiate
            End With
        End With
        DoEvents 'ON LAISSE LES EXECUSSIONs AUTRES SE FAIRE UN PEU
        Application.SendKeys " +{Backspace}" 'on tape un espace et un retour en arriere pour que le caret cligonant d'edition soit au debut
        Application.SendKeys "" ' réactive le pavé numérique si besoin
    End If
    ' Ne pas forcer l'affichage du commentaire
End Sub


Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    For Each c In ActiveSheet.Comments
        c.Visible = False
    Next
End Sub
au clickdroit le commentaire est créé (si il n'existe pas) et affiché de force
il est dimensionné
et placer en mode edition immédiatement le curseur clignote
la sélection dans une autre cellule masque les commentaires affichés donc on peut considérer que ca fait office de retractation du commentaire
important le triangle non visible !!! ca bloque l'affichage au survol
terminé
 
Bonjour,

Une idée si j'ai bien compris,

VB:
Option Explicit
Private lastCmt As Comment

Private Sub Worksheet_BeforeRightClick(ByVal Target As Range, Cancel As Boolean)
    If Target.Column < 2 Or Target.Column > 4 Then Exit Sub
  
    Cancel = True
    Application.DisplayCommentIndicator = xlCommentIndicatorOnly
  
    If Not lastCmt Is Nothing Then
        lastCmt.Visible = False
        Set lastCmt = Nothing
    End If

    If Target.Comment Is Nothing Then
        Target.AddComment "" 'sinon AddComment2 pour Office 365
        With Target.Comment.Shape
            .Width = 180
            .Height = 60
            .Fill.ForeColor.RGB = vbWhite
           ' .Line.Visible = msoFalse
        End With
    End If

    With Target.Comment
        .Visible = True
        .Shape.Select
    End With
  
    Set lastCmt = Target.Comment
End Sub

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If Not lastCmt Is Nothing Then
        lastCmt.Visible = False
        Set lastCmt = Nothing
    End If
End Sub
C'est exactement ça, merci beaucoup !
 
Bonjour
comment transforme t on les commentaire en postit auto hidden
comme ceci:
VB:
Private Sub Worksheet_BeforeRightClick(ByVal Target As Range, Cancel As Boolean)
    ' Applique aux colonnes B, C, D (2, 3, 4)
    If Target.Column >= 2 And Target.Column <= 4 And Target.Count = 1 Then
        Cancel = True ' Annule le menu contextuel
       
        ' Configuration de l'affichage des commentaires
        Application.DisplayCommentIndicator = False 'il ne s'affiche pas au survol
       
        ' Crée un nouveau commentaire si nécessaire
        If Target.Comment Is Nothing Then
            Target.AddComment Text:="" ' Crée un commentaire vide
        End If
       
        ' Rend le commentaire visible et éditable
        On Error Resume Next
        With Target.Comment
            .Visible = True 'on le met visible d'office (pas besoins du survol)
            With .Shape
                .Width = 200 'on dimensionne le width
                .Height = 150 'on dimensionne le height
                .Select ' Active l'édition immédiate
            End With
        End With
        DoEvents 'ON LAISSE LES EXECUSSIONs AUTRES SE FAIRE UN PEU
        Application.SendKeys " +{Backspace}" 'on tape un espace et un retour en arriere pour que le caret cligonant d'edition soit au debut
        Application.SendKeys "" ' réactive le pavé numérique si besoin
    End If
    ' Ne pas forcer l'affichage du commentaire
End Sub


Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    For Each c In ActiveSheet.Comments
        c.Visible = False
    Next
End Sub
au clickdroit le commentaire est créé (si il n'existe pas) et affiché de force
il est dimensionné
et placer en mode edition immédiatement le curseur clignote
la sélection dans une autre cellule masque les commentaires affichés donc on peut considérer que ca fait office de retractation du commentaire
important le triangle non visible !!! ca bloque l'affichage au survol
terminé
Merci je voudrais un mix des deux svp.

Le problème, c'est que là, il disparaît après quand je change de cellule, le code @Nicolas JACQUIN me convient davantage...

Le souci, c'est qu'il y a débogage, s'il y a clic droit à nouveau, sur la cellule, or je voudrais qu'il permette d'éditer le commentaire existant...



Merci
 
Bonjour à tous

Voici la version corrigée en espérant que ce soit bon et bien expliqué.

VB:
Option Explicit
Private lastCmt As Comment

Private Sub Worksheet_BeforeRightClick(ByVal Target As Range, Cancel As Boolean)
    Const FIRST_COL As Long = 2
    Const LAST_COL  As Long = 4
    
    If Target.Column < FIRST_COL Or Target.Column > LAST_COL Then Exit Sub
    Cancel = True
    Application.DisplayCommentIndicator = xlCommentIndicatorOnly

    If Not lastCmt Is Nothing Then
        If Not lastCmt Is Target.Comment Then lastCmt.Visible = False
        Set lastCmt = Nothing
    End If

    Dim cmt As Comment                     'ou As Object si AddComment2
    If Target.Comment Is Nothing Then
        Set cmt = Target.AddComment("")    'AddComment2 pour Office 365 « Comments »
        With cmt.Shape
            .Width = 180   'change la taille ici en largeur
            .Height = 60   'change la taille ici en hauteur
            .Fill.ForeColor.RGB = vbWhite  'fond blanc
            '.Line.Visible = msoFalse       'pas de bordure
        End With
    Else
        Set cmt = Target.Comment
    End If

    On Error Resume Next           'empêche un débordement si déjà sélectionné
    cmt.Visible = True
    cmt.Shape.Select               'place le curseur dans la note
    On Error GoTo 0
    
    Set lastCmt = cmt              'nouvelle « note courante »
End Sub

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If Not lastCmt Is Nothing Then
        lastCmt.Visible = False
        Set lastCmt = Nothing
    End If
End Sub
 

Pièces jointes

Et un bonus si tu souhaites changer la couleur du texte du com (j'utilise actuellement sur mon calendrier)
je n'ai pas activé mais si tu souhaites

Code:
Option Explicit
Private lastCmt As Comment

Private Sub Worksheet_BeforeRightClick(ByVal Target As Range, Cancel As Boolean)
    Const FIRST_COL As Long = 2
    Const LAST_COL  As Long = 4
    
    If Target.Column < FIRST_COL Or Target.Column > LAST_COL Then Exit Sub
    Cancel = True
    Application.DisplayCommentIndicator = xlCommentIndicatorOnly

    If Not lastCmt Is Nothing Then
        If Not lastCmt Is Target.Comment Then lastCmt.Visible = False
        Set lastCmt = Nothing
    End If

    Dim cmt As Comment                     'ou As Object si AddComment2
    If Target.Comment Is Nothing Then
        Set cmt = Target.AddComment("")    'AddComment2 pour Office 365 « Comments »
        With cmt.Shape
        
        ''''''''BONUS''''''''
        
        '    With .TextFrame
        '        .AutoSize = False
        '        .Characters.Font.Color = RGB(0, 0, 139) ' Bleu foncé (texte)
        '    End With
        
            .Width = 180   'change la taille ici en largeur
            .Height = 60   'change la taille ici en hauteur
            .Fill.ForeColor.RGB = vbWhite  'fond blanc
            '.Line.Visible = msoFalse       'pas de bordure
        End With
    Else
        Set cmt = Target.Comment
    End If

    On Error Resume Next           'empêche un débordement si déjà sélectionné
    cmt.Visible = True
    cmt.Shape.Select               'place le curseur dans la note
    On Error GoTo 0
    
    Set lastCmt = cmt              'nouvelle « note courante »
End Sub

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If Not lastCmt Is Nothing Then
        lastCmt.Visible = False
        Set lastCmt = Nothing
    End If
End Sub
 

Pièces jointes

le select selectionne la shape du ,commentaire c'est tout
faire les 3 sendkeys comme je fait dans mon exemple pour être sur d'avoir le carrêt (tu l'apelle le curseur)clignotant dans le comm si on veut qu'il soit en mode edition
sinon tu est obligé de cliquer dessus pour pourvoir écrire (taper dedans)
 
- Navigue sans publicité
- Accède à Cléa, notre assistante IA experte Excel... et pas que...
- Profite de fonctionnalités exclusives
Ton soutien permet à Excel Downloads de rester 100% gratuit et de continuer à rassembler les passionnés d'Excel.
Je deviens Supporter XLD

Discussions similaires

Retour