XL 2013 Variable prenant un nom de fichier

Fred59240

XLDnaute Nouveau
Bonjour,

j'essaie de faire une macro en nommant une variable "box" qui doit correspondre à un fichier.
Le but c'est d'ouvrir une userform et de choisir le fichier à imprimer.

Voici ma macro :

Private Sub CommandButton1_Click()

Dim box As String

For I = 1 To 2
If Me.Controls("CheckBox" & I).Value = True Then
box ="Checkbox" & I.Value & ".xlsm"
Workbooks.Open Filename:="C:\DLB\Catalogues\box"
Windows("box").Activate

Sheets("CAT").Select

Or il ne reconnait pas ma variable box

Pouvez-vous m'aider sur ce sujet, pour trouver la bonne synthaxe ???

Merci
 

Lone-wolf

XLDnaute Barbatruc
Re : Variable prenant un nom de fichier

Bonjour Fred

Code:
Dim box As Workbook, fichier As String, i As Long

For i = 1 To 2
If Me.Controls("CheckBox" & i) = True Then
box =Me.Controls("CheckBox" & i).Caption & ".xlsm"
fichier = "C:\DLB\Catalogues\" & box
Workbooks.Open (fichier)
Else
Exit For
End If
Next i
 
Dernière édition:

Paf

XLDnaute Barbatruc
Re : Variable prenant un nom de fichier

Bonjour Fred59240,

Que devrait on avoir dans la variable box ?

Tel qu'elle est initialisée [box ="Checkbox" & I.Value & ".xlsm"], je ne suis pas sûr de ce qu'elle contient; quelque chose comme :
Checkbox1.xlsm si toutes fois I.value est interprété comme I ???

Si l'on veut obtenir le texte affiché auprès de la checkbox, il faudrait utiliser CheckBox.Caption:

box =Me.Controls("CheckBox" & I).Caption & ".xlsm"

Ensuite, avec Workbooks.Open Filename:="C:\DLB\Catalogues\box" on va peut-être essayer d'ouvrir le classeur box et non pas le contenu de la variable box, puisqu'elle est incluse dans la chaine entre " " . Il faut donc sortir cette variable de la chaîne:

Workbooks.Open Filename:="C:\DLB\Catalogues\" & box

A+


Edit: Bonjour Lone-wolf et bonjour pierrejean
 
Dernière édition:

pierrejean

XLDnaute Barbatruc
Re : Variable prenant un nom de fichier

bonjour a tous

moi je tenterai
box ="CheckBox" & I & ".xlsm" au lieu de box =Me.Controls("CheckBox" & I).Value & ".xlsm"
rappel:Me.Controls("CheckBox" & I).Value=true !!!
A moins qu'il n y ait lieu de considerer Me.Controls("CheckBox" & I).Caption ??

edit : salut Paf
 

Fred59240

XLDnaute Nouveau
Re : Variable prenant un nom de fichier

Lone,

J'ai recopié tes formules, je COULE !!!

Ci-dessous, l'intégralité du code de ma userform.
Ca ne fonctionne pas.

Je pense que je positionne mal les End If et les End With.

Si tu peux regarder s'il te plait.

Private Sub CommandButton1_Click()

Dim box As Workbook, fichier As String, i As Long

For i = 1 To 1
If Me.Controls("CheckBox" & i) = True Then
box = Me.Controls("CheckBox" & i).Caption & ".xlsm"
fichier = "C:\DLB\Catalogues\" & box
Workbooks.Open (fichier)

Sheets("CAT").Select

With Worksheets("CAT").Activate
Dim IL As Long
IL = Sheets("CAT").Range("A" & Application.Rows.Count).End(xlUp).Row 'Recherche de la dernière ligne
Sheets("CAT").PageSetup.PrintArea = "A1:F" & IL ' Définition de la zone

Range("A1:F" & IL).Select
ActiveSheet.PageSetup.PrintArea = ("A1:F" & IL)

With ActiveSheet.PageSetup
.LeftHeader = ""
.CenterHeader = ""
.RightHeader = ""
.LeftFooter = " IPNS"
.CenterFooter = "&P/&N"
.RightFooter = ""
.LeftMargin = Application.InchesToPoints(0)
.RightMargin = Application.InchesToPoints(0)
.TopMargin = Application.InchesToPoints(0)
.BottomMargin = Application.InchesToPoints(0.511811023622047)
.HeaderMargin = Application.InchesToPoints(0)
.FooterMargin = Application.InchesToPoints(0.275590551181102)
.PrintHeadings = False
.PrintGridlines = False
.PrintComments = xlPrintNoComments
' .PrintQuality = 500
.CenterHorizontally = True
.CenterVertically = True
.Orientation = xlPortrait
.Draft = False
.PaperSize = xlPaperA4
.FirstPageNumber = xlAutomatic
.Order = xlDownThenOver
.BlackAndWhite = False
.Zoom = False
.FitToPagesWide = 1 'Ajuster en largeur
.FitToPagesTall = 200 'Ajuster en hauteur
.PrintErrors = xlPrintErrorsDisplayed
.OddAndEvenPagesHeaderFooter = False
.DifferentFirstPageHeaderFooter = False
.ScaleWithDocHeaderFooter = True
.AlignMarginsHeaderFooter = True
.EvenPage.LeftHeader.Text = ""
.EvenPage.CenterHeader.Text = ""
.EvenPage.RightHeader.Text = ""
.EvenPage.LeftFooter.Text = ""
.EvenPage.CenterFooter.Text = ""
.EvenPage.RightFooter.Text = ""
.FirstPage.LeftHeader.Text = ""
.FirstPage.CenterHeader.Text = ""
.FirstPage.RightHeader.Text = ""
.FirstPage.LeftFooter.Text = ""
.FirstPage.CenterFooter.Text = ""
.FirstPage.RightFooter.Text = ""
End With
End With
Application.PrintCommunication = True
' SelectedSheets.PrintPreview
ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True, _
IgnorePrintAreas:=False


Me.Controls("CheckBox" & i).Value = False
Else
Exit For
End If
Next i
Unload Me
End Sub


Merci à toi

Fred
 

Fred59240

XLDnaute Nouveau
Re : Variable prenant un nom de fichier

Bonjour Paf et Pierrejean,

Je n'avais pas vu vos réponses.
Merci pour vos explications.
Mon problème c'est que box = " nothing" à chaque fois !!!
Dans les propriétés de checkbox, j'ai bien le nom de mon fichier dans CAPTION.

merci
 

Paf

XLDnaute Barbatruc
Re : Variable prenant un nom de fichier

Re,

la valeur box = nothing !!!

ça semble bien curieux ?
si le test [If Me.Controls("CheckBox" & i) = True Then] est faux on n'exécute rien, donc si box =nothing, ça n'a pas d'importance; et si le test est vrai alors même si la propriété Caption de la CheckBox n'est pas définie, box 'vaudrait' quand même .xlsm

A+

Edit: Est-il utile de faire une boucle pour ne traiter qu'une seule valeur ?[ For i = 1 To 1]
 
Dernière édition:

Lone-wolf

XLDnaute Barbatruc
Re : Variable prenant un nom de fichier

Re Fred

EDIT: bonsoir Paf, pierre-jean :)

Test éffectué sur une feuille, ça me donne bien les noms.
Code:
Private Sub CheckBox1_Enter()
If CheckBox2 Then CheckBox2 = False
End Sub

Private Sub CheckBox2_Enter()
If CheckBox1 Then CheckBox1 = False
End Sub


Private Sub CommandButton1_Click()
With Feuil1
For i = 1 To 2
If Me.Controls("CheckBox" & i) = True Then .Range("d5") = Me.Controls("CheckBox" & i).Caption
Next i
End With
End Sub

Sheets("CAT").Select à changer par

With ActiveWorkbook
'Puisque tu as ouvert le classeur
.Sheets("CAT").Activate
With Activesheet
Le reste du code

End With
End With
 
Dernière édition:

Discussions similaires

Statistiques des forums

Discussions
312 429
Messages
2 088 352
Membres
103 824
dernier inscrit
frederic.marien@proximus.