Ceci est une page optimisée pour les mobiles. Cliquez sur ce texte pour afficher la vraie page.

XL 2010 MACRO - Gestion des erreurs

AntoineM

XLDnaute Junior
Bonjour le (nouveau?) forum,

Je poste un petit message d'appel à l'aide aujourd'hui quant à la gestion des erreurs. Voici mon code :
VB:
Private Sub CommandButton1_Click()
  Dim Ctrl As Control
  Dim k As Integer
  Dim j As Integer
 
  For Each Ctrl In Frame1.Controls
  If Ctrl.Object.Value = True Then
  Exit For
  End If
  Next Ctrl
 
k = Application.Match(ComboBox1.Value, Feuil1.Range("A1:A100"), 0)
j = Application.Match(Ctrl.Object.Caption, Feuil1.Range("A1:H1"), 0)
Sheets("Suivi").Cells(k, j).Value = Format(DTPicker1.Value, "dd/mm/yyyy")
Sheets("Suivi").Cells(k, j).Font.Bold = True
Unload Me
End Sub

J'ai un frame contenant 4 optionbutton. Quand je ne sélectionner aucun d'entre eux, ma macro plante forcément à la ligne :
VB:
j = Application.Match(Ctrl.Object.Caption, Feuil1.Range("A1:H1"), 0)

J'aimerais gérer l'erreur de manière à :
- Si aucun Option Button n'est sélectionner :
1°) Afficher un MsgBox ("Il faut sélectionner...")
2°) Revenir à l'userform pour sélectionner l'OptionButton
3°) Resume la procédure

Merci d'avance,

AntoineM
 

Pièces jointes

  • Capture.JPG
    50.7 KB · Affichages: 37

AntoineM

XLDnaute Junior
Re-bonjour le forum,

Désolé pour la question, je n'avais pas assez fouillez dans la gestion des erreurs de silkyroad.

Réponse :
VB:
Private Sub CommandButton1_Click()
 
 
  Dim Ctrl As Control
  Dim k As Integer
  Dim j As Integer
 
For Each Ctrl In Frame1.Controls
 
   If TypeOf Ctrl Is msforms.OptionButton Then
 
       If Ctrl.Value = True Then
             F1D = True
             Exit For
             End If
       End If
   Next Ctrl
 
If F1D = False Then
MsgBox "Attention! Il faut sélectionner un statut de classification"
Exit Sub
 
Else
 
'Déclanchement procédure
k = Application.Match(ComboBox1.Value, Feuil1.Range("A1:A100"), 0)
j = Application.Match(Ctrl.Object.Caption, Feuil1.Range("A1:H1"), 0)
 
Feuil1.Cells(k, j).Value = Format(DTPicker1.Value, "mm/dd/yyyy")
With Feuil1.Cells(k, j).Font
    .Bold = True
    .Italic = False
    .Color = 3
    .Size = 11
    .Name = "Calibri"
End With
 
End If
              
Unload Me
 
End Sub
 

Robert

XLDnaute Barbatruc
Repose en paix
Bonjour Antoine, bonjour le forum,

Peut-être comme ça :

VB:
Private Sub CommandButton1_Click()
Dim Ctrl As Control
Dim k As Integer
Dim j As Integer
Dim test As Boolean
For Each Ctrl In Frame1.Controls
If Ctrl.Value = True Then
    test = True
    Exit For
End If
If test = False Then
    MsgBox "blablabla"
    Me.Frame1.OptionButton1.SetFocus
    Exit Sub
Next Ctrl
k = Application.Match(ComboBox1.Value, Feuil1.Range("A1:A100"), 0)
j = Application.Match(Ctrl.Object.Caption, Feuil1.Range("A1:H1"), 0)
Sheets("Suivi").Cells(k, j).Value = Format(DTPicker1.Value, "dd/mm/yyyy")
Sheets("Suivi").Cells(k, j).Font.Bold = True
Unload Me
End Sub
 

AntoineM

XLDnaute Junior
Bonjour Robert, le forum,

Effectivement ta solution est la bonne, je l'ai posté un tout petit peu avant toi après avoir trainé sur certain sites anglais de VBA.

Merci de ta réponse, elle fonctionne
 
Les cookies sont requis pour utiliser ce site. Vous devez les accepter pour continuer à utiliser le site. En savoir plus…