Récupérer ligne en fonction d'une variable

jeanphi

XLDnaute Occasionnel
Bonjour

Dans le fichier joint, je souhaite si la valeur de la cellule de la colonne F de la feuille" SuiviCommande" est vide copier dans la feuille "Commande" les infos contenues dans les colonnes B et D de la feuille "SuiviCommande" pour la ligne concernée
J'ai developpé le code suivant mais il bugge et je ne comprends pas pourquoi :(

Sub CDE()
Dim cell As Range
For Each cell In Sheets("SuiviCommande").Range("F6:F" & Sheets("SuiviCommande").Range("F65536").End(xlUp).Row)
If cell.Value = "" Then
cell.Offset(0, -2).Copy Sheets("Commande").Range("B6" & Sheets("Commande").Range("B65536").End(xlUp).Row + 1)
End If
Next
End Sub


Quelqu'un peut t'il m'aider?
Merci
 

Pièces jointes

  • FEB2.xls
    19.5 KB · Affichages: 67
C

Compte Supprimé 979

Guest
Re : Récupérer ligne en fonction d'une variable

Bonjour Jeanphi,

Le problème vient de ta cellule de destination pour le collage
Tu as laisser le numéro de ligne
Range("B6" & Sheets("Commande").Range("B65536").End(xlUp).Row + 1)

Sinon avec un code un peu plus facile à lire, en tout cas pour moi :D
Code:
Sub CDE()
Dim Cell As Range, DerLig As Long, Derlig1 As Long
With Sheets("SuiviCommande")
  DerLig = .Range("F" & Rows.Count).End(xlUp).Row
  For Each Cell In .Range("F6:F" & DerLig)
    If Cell.Value = "" Then
      With Sheets("Commande")
        Derlig1 = .Range("B" & Rows.Count).End(xlUp).Row
        Cell.Offset(0, -2).Copy Destination:=.Range("B" & Derlig1 + 1)
      End With
    End If
  Next
End With
End Sub

A+
 

si81ze

XLDnaute Nouveau
Re : Récupérer ligne en fonction d'une variable

Bonjour à toutes et à tous,
je souhaite supprimer une, deux ou trois colonnes sur différentes feuilles selon la valeur d'une cellule bien précise.
Est ce que vous pouvez m'aider?

Merci d'avance.

Simo
 

jeanphi

XLDnaute Occasionnel
Re : Récupérer ligne en fonction d'une variable

Bonjour Bruno et merci pour ton aide!!


J'ai malgré tout encore un petit souci
La macro ne récupère que la première ligne répondant au conditions et pas les autres!
Je te joint mon code et celui que tu m'as proposé
Peux tu m'aider?
Encore merci :)


Sub CDE()
Dim Cell As Range
For Each Cell In Sheets("SuiviCommande").Range("F6:F" & Sheets("SuiviCommande").Range("F65536").End(xlUp).Row)
If Cell.Value = "" Then
Cell.Offset(0, -4).Copy Sheets("Commande").Range("B" & Sheets("Commande").Range("B65536").End(xlUp).Row + 1)
Cell.Offset(0, -2).Copy Sheets("Commande").Range("D" & Sheets("Commande").Range("D65536").End(xlUp).Row + 1)
End If
Next
End Sub

Sub CDE2()
Dim Cell As Range, DerLig As Long, Derlig1 As Long
With Sheets("SuiviCommande")
DerLig = .Range("F" & Rows.Count).End(xlUp).Row
For Each Cell In .Range("F6:F" & DerLig)
If Cell.Value = "" Then
With Sheets("Commande")
Derlig1 = .Range("B" & Rows.Count).End(xlUp).Row
Cell.Offset(0, -4).Copy Destination:=.Range("B" & Derlig1 + 1)
End With
End If
Next
End With
End Sub
 

jeanphi

XLDnaute Occasionnel
Bonjour

J'ai un petit souci avec la macro ci-dessous
La macro ne récupère que la première ligne répondant au conditions et pas les autres!
Si je veux récupérer toutes les lignes répondant aux critères, il faut que je lance autant de fois la macro
Quelles sont les modif à apporter à cette macro pour récupérer les variables en une seule fois?
Merci ;)


Sub CDE()
Dim Cell As Range
For Each Cell In Sheets("SuiviCommande").Range("F6:F" & Sheets("SuiviCommande").Range("F65536").End(xlUp).Row)
If Cell.Value = "" Then
Cell.Offset(0, -4).Copy Sheets("Commande").Range("B" & Sheets("Commande").Range("B65536").End(xlUp).Row + 1)
Cell.Offset(0, -2).Copy Sheets("Commande").Range("D" & Sheets("Commande").Range("D65536").End(xlUp).Row + 1)
End If
Next
End Sub

Sub CDE2()
Dim Cell As Range, DerLig As Long, Derlig1 As Long
With Sheets("SuiviCommande")
DerLig = .Range("F" & Rows.Count).End(xlUp).Row
For Each Cell In .Range("F6:F" & DerLig)
If Cell.Value = "" Then
With Sheets("Commande")
Derlig1 = .Range("B" & Rows.Count).End(xlUp).Row
Cell.Offset(0, -4).Copy Destination:=.Range("B" & Derlig1 + 1)
End With
End If
Next
End With
End Sub[/QUOTE]
 

skoobi

XLDnaute Barbatruc
Re : Récupérer ligne en fonction d'une variable

Bonjour,

en reprenant le code de Bruno, "DerLig" doit être cherché en colonne B:

Code:
Sub CDE2()
  Dim Cell As Range, DerLig As Long, Derlig1 As Long
  With Sheets("SuiviCommande")
    DerLig = .Range("[COLOR=Blue][B]B[/B][/COLOR]" & Rows.Count).End(xlUp).Row
    For Each Cell In .Range("F6:F" & DerLig).SpecialCells(xlCellTypeBlanks)
      With Sheets("Commande")
        Derlig1 = .Range("B" & Rows.Count).End(xlUp).Row
        Cell.Offset(0, -4).Copy Destination:=.Range("B" & Derlig1 + 1)
      End With
    Next
  End With
End Sub
 

Discussions similaires

Réponses
1
Affichages
205
Réponses
21
Affichages
430

Statistiques des forums

Discussions
312 608
Messages
2 090 189
Membres
104 446
dernier inscrit
Phil A