Actuellement j’utilise ce code pour un fichier Excel.
Je voudrais le compléter pour insérer :
1) Un Entête et un pied de page
2) Un saut de page après x lignes d’afficher sur la page.
'Existance d'un fichier modèle
If Arg_Path & "" = "" Then
'Pas de fichier model
Set ExcelApp =
CreateObject("Excel.application").Workbooks.Add
Set ExcelSheet = ExcelApp.Worksheets(1)
Else
'Fichier modèle
Set ExcelApp = GetObject(Arg_Path)
Set ExcelSheet = ExcelApp.Worksheets(1)
End If
ExcelApp.Windows(1).Visible = True
'Existance des données
If Not (Arg_Rs.BOF = True And Arg_Rs.EOF = True) Then
'Il y a des données à exporter
Arg_Rs.MoveLast
Arg_Rs.MoveFirst
NbrChamps = Arg_Rs.Fields.Count
'Titre de colonne
For I = 0 To NbrChamps - 1
ExcelSheet.Cells(Arg_Ligne, I + Arg_Colonne) =
Arg_Rs(I).Name
Next
'Copie des infos
ExcelSheet.Cells(Arg_Ligne + 1, Arg_Colonne).CopyFromRecordset
Arg_Rs
'Mise en forme si arg_cadre = true
If Arg_MEF = True Then
'datage
With ExcelSheet.Range(ExcelSheet.Cells(Arg_Ligne
- 2, Arg_Colonne), ExcelSheet.Cells(Arg_Ligne - 2, Arg_Colonne +
NbrChamps - 1))
.Font.Italic = True
.Font.Bold = True
.Font.Color = 255
End With
'Cadre + couleur des titres
'With = la zone tableau
With
ExcelSheet.Range(ExcelSheet.Cells(Arg_Ligne, Arg_Colonne),
ExcelSheet.Cells(Arg_Ligne + Arg_Rs.RecordCount, Arg_Colonne +
NbrChamps - 1))
.Borders(xlInsideVertical).Weight = xlThin
.Borders(xlInsideHorizontal).Weight =
xlThin
.Borders(xlEdgeLeft).Weight = xlMedium
.Borders(xlEdgeTop).Weight = xlMedium
.Borders(xlEdgeBottom).Weight = xlMedium
.Borders(xlEdgeRight).Weight = xlMedium
End With
With ExcelSheet.Range(ExcelSheet.Cells(Arg_Ligne,
Arg_Colonne), ExcelSheet.Cells(Arg_Ligne, Arg_Colonne + NbrChamps -
1))
.Interior.ColorIndex = 37
.Borders(xlEdgeBottom).Weight = xlMedium
End With
End If
End If
Merci pour votre aide
Salutations
Sub MiseEnForme()
Dim x As Integer
Dim i As Integer
Dim c As Integer
Dim d As Integer
Application.ScreenUpdating = False
With ActiveSheet.PageSetup
.LeftHeader = "Partie gauche En-tête"
.CenterHeader = "Partie centrale En-tête"
.RightHeader = "Partie droite En-tête"
.LeftFooter = "Partie gauche Pied de page" & Chr(10) & "&F &A"
.CenterFooter = "Partie centrale Pied de page" & Chr(10) & "&P/
&N"
.RightFooter = "Partie droite Pied de page" & Chr(10) & "&D
&T"
End With
ActiveSheet.PageSetup.PrintArea = ActiveSheet.UsedRange.Address
x = 10
c = ActiveSheet.UsedRange.Row
d = Cells.SpecialCells(xlLastCell).Row
For i = 1 To Round((d - c) / x, 0)
ActiveWindow.SelectedSheets.HPageBreaks.Add
Before:=Cells(c + i * x, 1)
Next
Application.ScreenUpdating = True
End Sub
--------------------------------------
2/ Avec d'autres paramètres d'impression pré-définis:
Sub MiseEnForme()
Dim x As Integer
Dim i As Integer
Dim c As Integer
Dim d As Integer
Application.ScreenUpdating = False
With ActiveSheet.PageSetup
.PrintTitleRows = ""
.PrintTitleColumns = ""
End With
With ActiveSheet.PageSetup
.LeftHeader = "Partie gauche En-tête"
.CenterHeader = "Partie centrale En-tête"
.RightHeader = "Partie droite En-tête"
.LeftFooter = "Partie gauche Pied de page" & Chr(10) & "&F &A"
.CenterFooter = "Partie centrale Pied de page" & Chr(10) & "&P/
&N"
.RightFooter = "Partie droite Pied de page" & Chr(10) & "&D
&T"
.LeftMargin = Application.InchesToPoints(1)
.RightMargin = Application.InchesToPoints(1)
.TopMargin = Application.InchesToPoints(0.9)
.BottomMargin = Application.InchesToPoints(0.9)
.HeaderMargin = Application.InchesToPoints(0.5)
.FooterMargin = Application.InchesToPoints(0.5)
.FitToPagesWide = 1
.PrintHeadings = False
.PrintGridlines = False
.PrintComments = xlPrintNoComments
.CenterHorizontally = False
.CenterVertically = False
.Orientation = xlPortrait
.Draft = False
.PaperSize = xlPaperA4
.FirstPageNumber = xlAutomatic
.Order = xlDownThenOver
.BlackAndWhite = False
.Zoom = 100
.PrintErrors = xlPrintErrorsDisplayed
End With
ActiveSheet.PageSetup.PrintArea = ActiveSheet.UsedRange.Address
x = 10
c = ActiveSheet.UsedRange.Row
d = Cells.SpecialCells(xlLastCell).Row
For i = 1 To Round((d - c) / x, 0)
ActiveWindow.SelectedSheets.HPageBreaks.Add Before:=Cells(c +
i * x, 1)
Next
Application.ScreenUpdating = True
End Sub
x = 10
With ExcelSheet
With .PageSetup
.CenterHeader = "Tête"
.CenterFooter = "Pied"
End With
.HPageBreaks.Add Before:=Rows(x)
End With
isabelle
Le 2010-12-06 07:49, jacques a écrit :
> Bonjour,
>
> Actuellement j’utilise ce code pour un fichier Excel.
>
> Je voudrais le compléter pour insérer :
> 1) Un Entête et un pied de page
> 2) Un saut de page après x lignes d’afficher sur la page.
>
> 'Existance d'un fichier modèle
> If Arg_Path& "" = "" Then
Bonsoir Isabelle, Bonsoir Joe H,
Merci pour vos réponses.
En premier j'essaye la solution d'Isabelle.
Lorsque je lance le code j'ai une erreur (sub ou fonction non définie)
sur Rows.
A+
Salutations
il doit y avoir un point devant Rows,
.HPageBreaks.Add Before:=.Rows(x)
isabelle
Le 2010-12-08 11:57, jacques a écrit :
> On 7 déc, 00:24, isabelle<i...@v.org> wrote:
>> bonjour Jacques,
>>
>> x = 10
>> With ExcelSheet
>> With .PageSetup
>> .CenterHeader = "T te"
>> .CenterFooter = "Pied"
>> End With
>> .HPageBreaks.Add Before:=Rows(x)
>> End With
>>
>> isabelle
>>
>> Le 2010-12-06 07:49, jacques a crit :
> Bonsoir Isabelle, Bonsoir Joe H,
Bonsoir Isabelle,
Ca marche, c'était bien le point qui manquait.
Maintenant je voudrais inserer des lignes après chaque saut de page.
Peux-tu m'aider ?
A+
Salutations
par exemple pour insérer 2 lignes à chaque saut de page
For Each hpb In ActiveSheet.HPageBreaks
x = hpb.Location.Row
Rows(x & ":" & x + 1).Insert Shift:=xlDown
Next
isabelle
.
Re Bonsoir Isabelle,
La le code bloque sur xlDown, et je ne comprend pas pourquoi.
Salutations
Bonjour Isabelle,
Super le fichier exemple.
Peux-tu le compléter en ajoutant pour chaque page et sur deux lignes
un Entête de page et un pied de page?.
De plus est-il possible d'avoir sur l'entête de page les noms (Ex NOM,
PRENOM, ADRESSE etc...;)
des colonnes formatées (Ex Largeur police centrer etc...;)?.
Je te remercie par avance.
A+
Salutations
pour l'entête et pied de page sur 2 lignes,
With ActiveSheet.PageSetup
.CenterHeader = "tete" & Chr(10) & "deuxième ligne"
.CenterFooter = "pied" & Chr(10) & "deuxième ligne"
End With
pour l'autre question je ne comprend pas ce que tu veut dire.
isabelle
Bonjour Isabelle,
http://cjoint.com/?0mlu7UQmBDo
Avec la fonction Access décrite dans mon premier message je crée un
classeur Excel brut présenté dans la photo (paragraphe 1), puis juste
avant la sauvegarde du classeur je voudrais faires différentes
manipulations, pour avoir la présentation suivant la photo du
(paragraphe 2.)
Il me reste à faire le formatage de la ligne bleu des noms des
colonnes (Hauteur, retour à la ligne, centrage vertical) et de
reproduire cette ligne sur chaque page.
A+
Et bon dimanche.
Salutations.
je ne suis pas sur de bien comprendre le "reproduire cette ligne sur chaque page"
alors c'est peut être ca :
For Each f In Worksheets
f.Select
With Range(Cells(1, 1), Cells(1, Range("IV1").End(xlToLeft).Column))
.Interior.ColorIndex = 34
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
.WrapText = True
.Rows.AutoFit
End With
Next
End Sub
ou bien :
Sheets("Feuil1").Select
With Range(Cells(1, 1), Cells(1, Range("IV1").End(xlToLeft).Column))
.Interior.ColorIndex = 34
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
.WrapText = True
.Rows.AutoFit
End With
Rows(1).Copy
For Each f In Worksheets
f.Select
Range("A1").Select
ActiveSheet.Paste
Next
Application.CutCopyMode = False
ou bien
que la première ligne de l'onglet apparaissent sur chacune de ses pages à l'aperçu ou à l'impression
ActiveSheet.PageSetup.PrintTitleRows = "$1:$1"
@+
et bon dimanche à toi également
isabelle
Bonjour Isabelle,
C'est cette ligne qui a répondu à mon problème:
ActiveSheet.PageSetup.PrintTitleRows = "$1:$1"
Par contre si j'édite les différentes lignes de code est-ce que tu
pourras
aléger son écriture.
Merci par avance.
A+
Salutations
Bonsoir Isabelle,
Par fichier tu entends Feuille Excel avec le code.
Si oui dans mon cas le code se trouve dans un formulaires Access, et
si besoin je pourrais
faire et mettre une base allégée sur cjoint.
Je joins tout de même mes lignes de codes, je précise que les lignes
entourées posent problème.
Salutations
Private Sub Test_BtnExportExcel_Click()
On Error GoTo Test_Err
Dim I As Integer
Chemin = ""
Set rs = CurrentDb.OpenRecordset(strsql, dbOpenDynaset)
Set Excl = fExportExcel(Chemin, rs, True, 1, 1)
Path = CurrentProject.Path
With Excl.Sheets(1)
'Renomme les champs
.Cells(1, 3) = "Nom"
.Cells(1, 9) = "Date de naissance"
.Cells(1, 17) = "Profes."
.Cells(1, 18) = "Activ."
.Cells(1, 19) = "Asso."
'Supprime les colonnes
.Columns("T:AE").Delete
.Columns("A:B").Delete
.Columns("C:E").Delete
'Hauteur de la ligne
.Rows(1).RowHeight = 47.25
'Largeur des colonnes, Centrage vertical et horizontal
.Columns("A:A").ColumnWidth = 13
.Cells(1, 1).HorizontalAlignment = xlCenter
.Columns("A:A").VerticalAlignment = xlCenter
.Columns("B:B").ColumnWidth = 5.4
.Cells(1, 2).HorizontalAlignment = xlCenter
.Columns("B:B").VerticalAlignment = xlCenter
.Columns("B:B").WrapText = True
.Columns("C:C").ColumnWidth = 10.6
.Cells(1, 3).HorizontalAlignment = xlCenter
.Columns("C:C").VerticalAlignment = xlCenter
.Columns("D:D").ColumnWidth = 8.6
.Columns("D:D").HorizontalAlignment = xlCenter
.Columns("D:D").VerticalAlignment = xlCenter
.Columns("D:D").WrapText = True
.Columns("E:E").ColumnWidth = 3.3
.Columns("E:E").HorizontalAlignment = xlCenter
.Columns("E:E").VerticalAlignment = xlCenter
.Columns("E:E").WrapText = True
.Columns("F:F").ColumnWidth = 10.6
.Cells(1, 6).HorizontalAlignment = xlCenter
.Columns("F:F").VerticalAlignment = xlCenter
.Columns("F:F").WrapText = True
.Columns("G:G").ColumnWidth = 22.6
.Cells(1, 7).HorizontalAlignment = xlCenter
.Columns("G:G").VerticalAlignment = xlCenter
.Columns("G:G").WrapText = True
.Columns("H:H").ColumnWidth = 6.4
.Columns("H:H").HorizontalAlignment = xlCenter
.Columns("H:H").VerticalAlignment = xlCenter
.Columns("I:I").ColumnWidth = 10.6
.Cells(1, 9).HorizontalAlignment = xlCenter
.Columns("I:I").VerticalAlignment = xlCenter
.Columns("J:J").ColumnWidth = 3.3
.Columns("J:J").HorizontalAlignment = xlCenter
.Columns("J:J").VerticalAlignment = xlCenter
.Columns("K:K").ColumnWidth = 25
.Cells(1, 11).HorizontalAlignment = xlCenter
.Columns("K:K").VerticalAlignment = xlCenter
.Columns("L:L").ColumnWidth = 4
.Columns("L:L").HorizontalAlignment = xlCenter
.Columns("L:L").VerticalAlignment = xlCenter
.Columns("M:M").ColumnWidth = 3.7
.Columns("M:M").HorizontalAlignment = xlCenter
.Columns("M:M").VerticalAlignment = xlCenter
.Columns("N:N").ColumnWidth = 5
.Columns("N:N").HorizontalAlignment = xlCenter
.Columns("N:N").VerticalAlignment = xlCenter
With .PageSetup
'en-tête de page
.CenterHeader = "&G&18&KFF0000&""Comic Sans Ms""Liste des
bénéficiaires" & "&B" & Chr(10) & " " & Year(CDate(DébutSaison)) & "
- " & Year(CDate(FinSaison)) '<-- texte (style gras + taille 12 +
style police)
'en-tête de page
.Orientation = xlLandscape
'Affichage Portrait ou Paysage
.LeftFooter = "&I&D / &T" '<-- date / heure (style italique)
.RightFooter = "&8&P/&N" '<-- numéro de page / nombre de
pages (taille 8)
'Affichage des titres sur chaque pages
.PrintTitleRows = "$1:$1"
'*******************************************************************
'*
*
'* 'Saut de
page *
'* For I = 29 To 90 Step
29 *
'* .HPageBreaks.Add .Range("A" & I) *
'*
Next
*
'*
*
'* en débogage le code ne boucle pas sur For I *
'*
*
'*******************************************************************
'*******************************************************************
'* 'Marges de la
page *
'* .LeftMargin = Application.InchesToPoints(0.196) *
'* .RightMargin = Aplication.InchesToPoints(0.196) *
'* .TopMargin = Aplication.InchesToPoints(1.063) *
'*
*
'* Je n'arrive pas à faire cette partie, elle me crée une *
'* Erreur sur
InchesToPoints *
'*
*
'*******************************************************************
End With
End With
'Sauvegarde d'Excel
Excl.SaveAs Path & "\" & "Dossier Excel\Assurance" & " " &
Year(CDate(DébutSaison)) & " - " & Year(CDate(FinSaison)) & ".xlsx"
Excl.Application.Quit
MsgBox "Tableau Excel Terminé"
Set Excl = Nothing
Exit Sub
Test_Err:
If err.Number <> 91 Then
MsgBox "Une erreur inattendue est apparue . L'erreur N° " &
err.Number & " ( " & err.Description & " )! Contactez
l'administrateur.", vbOKOnly + vbCritical, "Erreur inattendue !"
End If
Set Excl = Nothing
Set rs = Nothing
End Sub
la méthode InchesToPoints s'applique à l'objet "application" mais cette objet n'est pas défini dans ton code,
tu pourrais par contre utilisé les propriétées Margin en convertissant la valeur pouce en point
With .PageSetup
.LeftMargin = 70 ' de 70 à 72 = 2.5 cm sur mon écran
End With
il faut adapter à chaque écran.
Points Pixels Centimètres Pouces
18 24 0.635 0.25
36 48 1.27 0.5
72 96 2.54 1
108 144 3.81 1.5
144 192 5.08 2
180 240 6.35 2.5
isabelle
ton code n'
> .LeftFooter = "&I&D /&T" '<-- date / heure (style italique)
> Excl.SaveAs Path& "\"& "Dossier Excel\Assurance"& " "&
> Year(CDate(DébutSaison))& " - "& Year(CDate(FinSaison))& ".xlsx"
> Excl.Application.Quit
>
> MsgBox "Tableau Excel Terminé"
>
> Set Excl = Nothing
> Exit Sub
> Test_Err:
>
> If err.Number<> 91 Then
> MsgBox "Une erreur inattendue est apparue . L'erreur N° "&
> err.Number& " ( "& err.Description& " )! Contactez
Bonsoir Isabelle,
En faisant la conversion en pouces les marges fonctionnent très bien.
Par contre je n'arrive pas à regler ce problème:
'***************************************************************
'*
*
'* 'Saut de
page
'* For I = 29 To 90 Step
29
'* .HPageBreaks.Add.Range("A" & I)
'* Next
*
'* en débogage le code ne boucle pas sur For I
'*
*
'**************************************************************
Merci pour ta patience.
Salutations
il faut mettre un End With à With .PageSetup pour que .HPageBreaks s'applique à Excl.Sheets(1)
et enlever un End With juste avant 'Sauvegarde d'Excel
End With
For I = 29 To 90 Step 29
.HPageBreaks.Add.Range("A" & I)
Next
je te souhaite de bonne fête,
isabelle
Bonjour Isabelle,
Super tout fonctionne.
et bonne fête de fin d'année à toi également
Jacques