Problème condition IF
Bonjour, j'ai un petit problème ma condition ne fonctionne pas et je passe directement dans mon "Else" sans comprendre pourquoi, quelqu'un pourrait t'il m'éclairer ?
'------------------------------------------------------------------------------
Sub Creer_Recapitulatif()
Dim wbRecap As Workbook 'fichier recap
Dim wsRecap As Worksheet 'feuille où on écrit les données
Dim wbSource As Workbook 'fichier à ouvrir
Dim wsSource As Worksheet 'feuille où on cherche les données
Dim DernLign As Integer 'ligne où on écrit les données
Dim vFichiers As Variant 'noms des fichiers
Dim i As Integer, k As Integer, tot As Integer, NbCells As Integer ' variables numériques
Dim rgRecap As Range 'plage où on copie les données
Dim dercCol As Integer 'ajout JEP
Dim rng As Range 'ajout JEP
Dim vlg As Variant 'ajout JEP
Dim toto As Integer
Set wbRecap = ThisWorkbook 'Fichier récapitulatif
Set wsRecap = wbRecap.Sheets(1) 'on écrit dans la feuille 1 du fichier récapitulatif
Set rng = wsRecap.Range("A3:A30")
vlg = Columns(1).ColumnWidth
' --- Ouvrir boite de dialogue pour sélectionner les fichiers à ouvrir
vFichiers = Selectionner_Fichiers("Sélectionner les fichiers à compiler") 'Appel de Fonction pour ouvrir fichiers
' --- Initialisation des variables numériques (incrementeur de colonnes)
NbCells = 4
i = 0
tot = 0
toto = 4
' --- Vérifier qu'au moins un fichier à été sélectionné
If Not IsArray(vFichiers) Then
Debug.Print "Aucun fichier sélectionné."
MsgBox "Erreur! Aucun/Mauvais fichier sélectionné."
Exit Sub
End If
On Error Resume Next
Application.ScreenUpdating = False
' --- Boucle à travers les fichiers
For k = 1 To UBound(vFichiers)
Application.StatusBar = ">> Lecture du fichier #" & k & "/" & UBound(vFichiers)
' ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
' C'est ici qu'on écrit les instructions
Set wbSource = Workbooks.Open(vFichiers(k)) 'on ouvre le fichier
Set wsSource = wbSource.Sheets(1) 'On copie les données de la feuille 1
' - On copie les données vers le fichier Recapitulatif; à adapter
derCol = wsRecap.Cells(4, Columns.Count).End(xlToLeft).Column + 1 'ajout JEP
rng.Copy 'ajout JEP
wsRecap.Cells(3, derCol).PasteSpecial Paste:=xlPasteFormats 'ajout JEP
wsRecap.Columns(derCol).ColumnWidth = vlg 'ajout JEP
If Range("S5") = "Définitif" Then
With wsSource
wsRecap.Cells(3, derCol) = .Range("P1")
wsRecap.Cells(4, derCol) = .Range("N5") 'ajout JEP
wsRecap.Cells(6, derCol) = .Range("Q9")
wsRecap.Cells(8, derCol) = .Range("T8")
wsRecap.Cells(10, derCol) = .Range("P11")
wsRecap.Cells(11, derCol) = .Range("P12")
wsRecap.Cells(12, derCol) = .Range("P13")
wsRecap.Cells(13, derCol) = .Range("P14")
wsRecap.Cells(14, derCol) = .Range("T16")
wsRecap.Cells(15, derCol) = .Range("T18")
wsRecap.Cells(17, derCol) = .Range("T22")
wsRecap.Cells(21, derCol) = .Range("T38")
wsRecap.Cells(30, derCol) = .Range("T41")
End With
tot = tot + wsRecap.Cells(30, derCol)
i = i + 1
wbSource.Close 'fermer fichier
Set wbSource = Nothing
Else
derCol = wsRecap.Cells(4, Columns.Count).End(xlToLeft).Column + 1 'ajout JEP
rng.Copy 'ajout JEP
wsRecap.Cells(3, derCol).PasteSpecial Paste:=xlPasteFormats 'ajout JEP
wsRecap.Columns(derCol).ColumnWidth = vlg 'ajout JEP
With wsSource
wsRecap.Cells(25, derCol) = .Range("B4")
wsRecap.Cells(26, derCol) = .Range("B5")
wsRecap.Cells(27, derCol) = .Range("B6")
wsRecap.Cells(28, derCol) = .Range("B7")
wsRecap.Cells(29, derCol) = .Range("B8")
wsRecap.Cells(30, derCol) = .Range("C34")
End With
tot = tot + wsRecap.Cells(30, derCol)
i = i + 1
wbSource.Close 'fermer fichier
Set wbSource = Nothing
End If
' ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Next k
Cells.Select
Selection.Columns.AutoFit
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Range("b33").Select
Selection.Value = tot
ActiveCell.Select
Application.ScreenUpdating = True ' JEP : pas nécessaire / à supprimer
Application.StatusBar = False
End Sub
Function Selectionner_Fichiers(sTitre As String) As Variant
Dim sFiltre As String, bMultiSelect As Boolean
sFiltre = "Fichiers XYZ (.xls)(.xlsm), *.xls*"
bMultiSelect = True 'Permet de choisir plusieurs fichiers à la fois
Selectionner_Fichiers = Application.GetOpenFilename(Filefilter:=sFiltre, Title:=sTitre, MultiSelect:=bMultiSelect)
End Function
Sub RAZ()
NbArticles = Application.Max(1, Cells(Rows.Count, "A").End(xlUp).Row - 10)
Range(Cells(3, 2), Cells(16 + NbArticles, Columns.Count - 1)).Clear
End SubBonjour,
As-tu essayé d'indiquer à quelle feuille se rapporte l'instruction ?
Exemple :
If wsSource.Range("S5") = "Définitif" Then
A+
En effet, j'aurais du y penser ,merci !^^ Par hasard, sais-tu comment prendre la dernière case non vide d'une colonne x sur le fichier wsSource ?
Pour déterminer le numéro de ligne de la dernière cellule renseignée dans la colonne "X"
DerLig = wsSource.Range("X" & Rows.Count).End(xlUp).Row
A+
Merci de ta disponibilité ! C'est censé être placer ici ? car ça ne fonctionne pas
With wsSource
wsRecap.Cells(24, derCol) = .Range("A1")
wsRecap.Cells(25, derCol) = .Range("B4")
wsRecap.Cells(26, derCol) = .Range("B5")
wsRecap.Cells(27, derCol) = .Range("B6")
wsRecap.Cells(28, derCol) = .Range("B7")
wsRecap.Cells(29, derCol) = .Range("B8")
wsRecap.Cells(30, derCol) = .Range("C" & Rows.Count).End(xlUp).Row
End WithNon, dans le cas que tu présentes, tu ne cherches pas le numéro de la dernière cellule renseignée mais sa valeur.
wsRecap.Cells(30, DerCol) = .Range("C" & Rows.Count).End(xlUp).value
A+
Mystérieusement ça ne fonctionne pas, pourtant j'ai compris le code et je ne vous pas de raison à ce que cela ne fonctionne pas...
Tu n'es pas très loquace. Quand tu dis que ça ne fonctionne pas, est-ce parce que tu n'obtiens pas le résultat attendu, parce que tu n'obtiens aucun résultat, parce que la programme retourne une erreur, etc ?
A+
En effet excuse moi ! En fait, je n'obtiens aucun résultat, la case reste vide après execution de la macro... Je joins les doc pour que tu vois par toi même ! ^^ (d'ailleurs si je mets 2 fichiers qui passent par le "else" ça ne m'en ressort q'un seul..)
macro :
exemple :
Bonjour,
Essaie avec
wsRecap.Cells(30, derCol) = .Range("C" & .Rows.Count).End(xlUp).ValueA+
ça marche, merci !
edit: problème résolu !