checkbox multiples et aperçu avant impression

arvin

XLDnaute Occasionnel
bonjour à tous, j'ai un souci

Grâce à ce super forum, je suis arrivé à lancer une macro mais je bute à la dernière marche :

Un userform mermet de demander si on souhaite cocher des checkbox, si oui , en fonction de la checkbox qui est cochée, alors on a un aperçu avant impression qui va masquer des cellules : et ça c'est génial

le problème est que cela marche pour une checkbox , mais si on en coche plusieurs (donc 1 checkbox correspond à des cellules à masquer avant impression) cela ne marche pas !

bon je ne sais pas si je suis clair alors je lance l'exemple (pour 2 checkbox cocher)

merci à tous !!!!!



If CheckBox1.Value = True Then
FrmDates.Hide 'on cache l'userform car sinon cela plante
Dim temp(1000)
ligne = 1
Set champ = Range("A34:E36") 'cellules à masquer
For Each c In champ
temp(ligne) = c.NumberFormat
ligne = ligne + 1
c.NumberFormat = ";;;"
Next c
ActiveSheet.PrintPreview 'on a l'aperçu youpi !!!!!
ligne = 1
For Each c In champ
c.NumberFormat = temp(ligne)
ligne = ligne + 1
Next c
End If


mais comment faire pour le 2e check box si on le coche , et le 3e ......
If CheckBox2.Value = True Then
FrmDates.Hide
' Dim temp(1000)
ligne = 1
Set champ = Range("A41:E41") 'nouvelles cellules à masquer en fonction si la checkbox est cocher

For Each c In champ
temp(ligne) = c.NumberFormat
ligne = ligne + 1
c.NumberFormat = ";;;"
Next c
ActiveSheet.PrintPreview
ligne = 1
For Each c In champ
c.NumberFormat = temp(ligne)
ligne = ligne + 1
Next c
End If


FrmDates.Hide
 

PMO2

XLDnaute Accro
Re : checkbox multiples et aperçu avant impression

Bonjour,

Inspirez vous de l'exemple de code suivant qui a été fait sans un environnement concret et qui, par conséquent,
a de grands risques de ne pas fonctionner directement dans votre application.

Code:
Sub maMacro()
Dim temp()
Dim i&
FrmDates.Hide 'on cache l'userform car sinon cela plante
ligne = 0
'--- 1ère CheckBox ---
If CheckBox1.Value = True Then
  Set champ = Range("A34:E36") 'cellules à masquer
  For Each c In champ
    ligne = ligne + 1
    ReDim Preserve temp(1 To 2, 1 To ligne)
    temp(1, ligne) = c.Address
    temp(2, ligne) = c.NumberFormat
    c.NumberFormat = ";;;"
  Next c
End If
'--- 2ème CheckBox ---
If CheckBox2.Value = True Then
  Set champ = Range("A41:E41")
  For Each c In champ
    ligne = ligne + 1
    ReDim Preserve temp(1 To 2, 1 To ligne)
    temp(1, ligne) = c.Address
    temp(2, ligne) = c.NumberFormat
    c.NumberFormat = ";;;"
  Next c
End If
'--- etc (d'autres CheckBox) ---

'--- Aperçu à l'impression ---
ActiveSheet.PrintPreview
'--- Restaure les formats ---
For i& = 1 To UBound(temp, 2)
  Set c = Range(temp(1, i&))
  c.NumberFormat = temp(2, i&)
Next i&
End Sub

Cordialement.

PMO
Patrick Morange
 

jp14

XLDnaute Barbatruc
Re : checkbox multiples et aperçu avant impression

Bonsoir

La macro ne marche pas à cause de ces lignes :
For Each c In champ
c.NumberFormat = temp(ligne)
ligne = ligne + 1
Next c
Qui rétablissent l'état antérieur.
Il faut donc supprimer des lignes
Code:
FrmDates.Hide 'on cache l'userform car sinon cela plante
Dim temp(1000)
ligne = 1

If CheckBox1.Value = True Then
Set champ = Range("A34:E36") 'cellules à masquer
For Each c In champ
temp(ligne) = c.NumberFormat
ligne = ligne + 1
c.NumberFormat = ";;;"
Next c
End If

If CheckBox2.Value = True Then
Set champ = Range("A41:E41") 'nouvelles cellules à masquer en fonction si la checkbox est cocher
For Each c In champ
temp(ligne) = c.NumberFormat
ligne = ligne + 1
c.NumberFormat = ";;;"
Next c
End If

ActiveSheet.PrintPreview
' pour rétablir les données
ligne = 1
For Each c In champ
c.NumberFormat = temp(ligne)
ligne = ligne + 1
Next c

A tester

JP
 

Discussions similaires

Réponses
2
Affichages
154

Statistiques des forums

Discussions
312 322
Messages
2 087 288
Membres
103 508
dernier inscrit
max5554