Bonjour a tous,
J'ai un problème concernant mon code sous VBA Access, je souhaiterais qu'il reconnaisse que si il y a une case vide en (5,1) lors d'un export en automation alors en case (6,1) je peux mettre une phrase.
J'utilise :
If xlSheet.Cells(5, 1) = "" Then
xlSheet.Cells(6, 1) = "Youpi"
End If
Et ca ne marche pas, alors que si la valeur est 48 est que je remplace "" par "48" là ca marche ???? Que dois je utiliser comme expression pour la case vide ?
Pour info voici mon code complet qui peut servir d'exemple d'automation :
Private Sub LIA_01_Click()
Dim xlApp As Excel.Application
Dim xlSheet As Excel.Worksheet
Dim xlBook As Excel.Workbook
Dim I As Long, J As Long
Dim t0 As Long, t1 As Long
t0 = Timer
Dim enr As Recordset
Set rec = CurrentDb.OpenRecordset("* ma requête", dbOpenSnapshot)
'Initialisations
Set xlApp = CreateObject("Excel.Application" )
Set xlBook = xlApp.Workbooks.Add
'Ajouter une feuille de calcul
Set xlSheet = xlBook.Worksheets.Add
xlSheet.Name = "TT"
xlSheet.PageSetup.CenterHeader = ">T"
xlSheet.PageSetup.HeaderMargin = "1"
xlSheet.PageSetup.CenterFooter = "&G&P"
xlSheet.PageSetup.FooterMargin = "1"
xlSheet.PageSetup.Orientation = xlLandscape
xlSheet.PageSetup.Zoom = False
xlSheet.PageSetup.FitToPagesWide = 1
xlSheet.PageSetup.FitToPagesTall = False
xlSheet.PageSetup.LeftMargin = "0"
xlSheet.PageSetup.RightMargin = "75"
xlSheet.Cells(2, 1) = "Requête effectuée le " & Format(Date, "dd/mm/yyyy " ) & "à " & Format(Time, "hh:mm:ss" )
xlSheet.Cells(2, 1).Font.Bold = True
' les entetes
' .Fields(Index).Name renvoie le nom du champ
For J = 0 To rec.Fields.Count - 1
xlSheet.Cells(4, J + 1) = rec.Fields(J).Name
' Nous appliquons des enrichissements de format aux cellules
With xlSheet.Cells(4, J + 1)
.Interior.Pattern = xlSolid
.Interior.Color = RGB(192, 192, 192)
.Borders(xlEdgeBottom).LineStyle = xlContinuous
.Borders(xlEdgeBottom).Weight = xlThin
.Borders(xlEdgeBottom).ColorIndex = xlAutomatic
.Borders(xlEdgeTop).LineStyle = xlContinuous
.Borders(xlEdgeTop).Weight = xlThin
.Borders(xlEdgeTop).ColorIndex = xlAutomatic
.Borders(xlEdgeRight).LineStyle = xlContinuous
.Borders(xlEdgeRight).Weight = xlThin
.Borders(xlEdgeRight).ColorIndex = xlAutomatic
.Borders(xlEdgeLeft).LineStyle = xlContinuous
.Borders(xlEdgeLeft).Weight = xlThin
.Borders(xlEdgeLeft).ColorIndex = xlAutomatic
.HorizontalAlignment = xlLeft
End With
Next J
' recopie des données à partir de la ligne 5
I = 5
Do While Not rec.EOF
For J = 0 To rec.Fields.Count - 1
xlSheet.Cells(I, J + 1) = rec.Fields(J)
If xlSheet.Cells(5, 1) = "" Then
xlSheet.Cells(6, 1) = "Youpi"
End If
' .Fields(Index).Type renvoie le type du champ
' si c'est une date (dbDate) nous insérons en format date pour
' qu'il soit reconnu par Excel comme une date française
If rec.Fields(J).Type = dbDate Then
xlSheet.Cells(I, J + 1).NumberFormat = "dd/mm/yyyy"
xlSheet.Cells(I, J + 1) = rec.Fields(J)
With xlSheet.Cells(I, J + 1)
.Columns.EntireColumn.AutoFit
.Interior.Pattern = xlSolid
.Borders(xlEdgeBottom).LineStyle = xlContinuous
.Borders(xlEdgeBottom).Weight = xlThin
.Borders(xlEdgeBottom).ColorIndex = xlAutomatic
.Borders(xlEdgeTop).LineStyle = xlContinuous
.Borders(xlEdgeTop).Weight = xlThin
.Borders(xlEdgeTop).ColorIndex = xlAutomatic
.Borders(xlEdgeRight).LineStyle = xlContinuous
.Borders(xlEdgeRight).Weight = xlThin
.Borders(xlEdgeRight).ColorIndex = xlAutomatic
.Borders(xlEdgeLeft).LineStyle = xlContinuous
.Borders(xlEdgeLeft).Weight = xlThin
.Borders(xlEdgeLeft).ColorIndex = xlAutomatic
.HorizontalAlignment = xlLeft
End With
Else
xlSheet.Cells(I, J + 1) = rec.Fields(J)
With xlSheet.Cells(I, J + 1)
.Columns("B:ZZ" ).EntireColumn.AutoFit
.Interior.Pattern = xlSolid
.Borders(xlEdgeBottom).LineStyle = xlContinuous
.Borders(xlEdgeBottom).Weight = xlThin
.Borders(xlEdgeBottom).ColorIndex = xlAutomatic
.Borders(xlEdgeTop).LineStyle = xlContinuous
.Borders(xlEdgeTop).Weight = xlThin
.Borders(xlEdgeTop).ColorIndex = xlAutomatic
.Borders(xlEdgeRight).LineStyle = xlContinuous
.Borders(xlEdgeRight).Weight = xlThin
.Borders(xlEdgeRight).ColorIndex = xlAutomatic
.Borders(xlEdgeLeft).LineStyle = xlContinuous
.Borders(xlEdgeLeft).Weight = xlThin
.Borders(xlEdgeLeft).ColorIndex = xlAutomatic
.HorizontalAlignment = xlLeft
End With
End If
Next J
I = I + 1
rec.MoveNext
Loop
Set xlSheet = xlBook.Worksheets("Feuil1" )
xlApp.ScreenUpdating = False ' pour empêcher le message d'excel
xlSheet.Delete
xlApp.ScreenUpdating = True
Set xlSheet = xlBook.Worksheets("Feuil2" )
xlApp.ScreenUpdating = False ' pour empêcher le message d'excel
xlSheet.Delete
xlApp.ScreenUpdating = True
Set xlSheet = xlBook.Worksheets("Feuil3" )
xlApp.ScreenUpdating = False ' pour empêcher le message d'excel
xlSheet.Delete
xlApp.ScreenUpdating = True
' code de fermeture et libération des objets
xlBook.SaveAs "TT.xls"
xlApp.Quit
MsgBox ("Requête de M enregistrée dans -Mes Documents-" )
rec.Close
Set rec = Nothing
Set xlSheet = Nothing
Set xlBook = Nothing
Set xlApp = Nothing
t1 = Timer
End Sub
En tout cas merci pour votre aide.
Cordialement