Copier et effacer valeur a partir listbox-vba

lgvba

XLDnaute Nouveau
Bonjour

Jai ' besoin d ' aide avec le code suivant qui me permet de transferer les lignes selectionner dans la lisbox afin de les transfer dans une autre feuille, mais je voudrais rajouter le code afin d effacer la ligne selectionner apres avoir transferer les donnes

Voici le debut de Code
Code:
Private Sub complete_ar_Click() 
    Dim iListCount As Integer, iColCount As Integer 
    Dim iRow As Integer 
    Dim rStartCell As Range 
     'Set a range variable to the first cell to recieve our data
     'Using "End(xlUp).Offset(1, 0)" _
    will give us the cell below the last entry 
    Set rStartCell = Spotclassified.Range("A65536").End(xlUp).Offset(1, 0) 
     'Loop as many times (less one) as there are entries in our list.
     'We must start from zero to use this in the Selected Property.
    For iListCount = 0 To ListBox2.ListCount - 1 
        If ListBox2.Selected(iListCount) = True Then 'User has selected
             
            ListBox2.Selected(iListCount) = False 
            iRow = iRow + 1 
             'Now loop as many times as there are columns in MyRange
            For iColCount = 0 To Range("plageprogress").Columns.Count - 1 
                 'place the selected data into the table, starting from _
                range Ax And moving across As many columns As there are _ 
                In the range MyRange. 
                rStartCell.Cells(iRow, iColCount + 1).Value = _ 
                ListBox2.List(iListCount, iColCount) 
                 
            Next iColCount 
        End If 
    Next iListCount 
    Set rStartCell = Nothing 
End Sub

Cdt

lgvba
 

camarchepas

XLDnaute Barbatruc
Re : Copier et effacer valeur a partir listbox-vba

Bonjour LgVBA,

Comme j'ai peur de mal comprendre le terme effacer , je t'ai mis les 2 solutions :

Soit supprimer la ligne (Delete ) soit l'effacer , gardes celle qui t'intéresses , voir dans le code

Bon , comme il n'y a pas de fichier , je n'ai pas pu faire de tests , mais vu la correction mineure cela devrait être ok .

Bien sûr réalise un essai sur un jeu de données test et pas directement dans l'appli.

Code:
Option Explicit

Private Sub complete_ar() '_Click()
     Dim iListCount As Integer, iColCount As Integer
     Dim iRow As Integer
     Dim rStartCell As Range
      'Set a range variable to the first cell to recieve our data
      'Using "End(xlUp).Offset(1, 0)" _
     will give us the cell below the last entry
     Set rStartCell = Spotclassified.Range("A65536").End(xlUp).Offset(1, 0)
      'Loop as many times (less one) as there are entries in our list.
      'We must start from zero to use this in the Selected Property.
     For iListCount = 0 To ListBox2.ListCount - 1
         If ListBox2.Selected(iListCount) = True Then 'User has selected
              
             ListBox2.Selected(iListCount) = False
             iRow = iRow + 1
              'Now loop as many times as there are columns in MyRange
             For iColCount = 0 To Range("plageprogress").Columns.Count - 1
                  'place the selected data into the table, starting from _
                 range Ax And moving across As many columns As there are _
                 In the range MyRange.
                 rStartCell.Cells(iRow, iColCount + 1).Value = _
                 ListBox2.List(iListCount, iColCount)
                  
             Next iColCount
            
'MODIF A FAIRE ICI

              'Supprime la ligne
             Range("Reférence").Rows(3).Delete
             'Efface la ligne
             Range("Reférence").Rows(2).ClearContents

         End If
     Next iListCount
     Set rStartCell = Nothing
 End Sub
 

camarchepas

XLDnaute Barbatruc
Re : Copier et effacer valeur a partir listbox-vba

Et oui ,

bon avec le fichier le test est plus facile.

Voici le code corrigé mais choisi une des 2 méthodes pas la peine de faire les 2
et si l'on supprime il faut rattraper l'indexe don irow = irow-1

'MODIF A FAIRE ICI

'Efface la ligne
Range("SPOTINPROGRESS").Rows(iRow).ClearContents
'OU
'Supprime la ligne
Range("SPOTINPROGRESS").Rows(iRow).Delete
iRow = iRow - 1
 

camarchepas

XLDnaute Barbatruc
Re : Copier et effacer valeur a partir listbox-vba

Bon ,
je crois que j'ai pas tous compris , mais bon , je sais pas trop comment marche ton appli.

d'ou sors tu spotinprogress, car dans la boucle on manipule plageprogress et donc évidemment , ça peut pas marcher .

Il faut vraiment m'expliquer car sinon je ne comprendrais pas
 

lgvba

XLDnaute Nouveau
Re : Copier et effacer valeur a partir listbox-vba

salu

Merci pour ta reponse


"SPOTINPROGRESS" est la feuille excel ou se trouve la plage nommer "plageprogress", donc ce que je voudrais cest d effacer la ligne que j ' ai transferer a partir de ma listbox .
 

camarchepas

XLDnaute Barbatruc
Re : Copier et effacer valeur a partir listbox-vba

lgvba,

Non ça y est j'ai compris , tu as mal adapter le code ,

il faut écrire :

et le irow = irow -1 est la car lorsque l'on efface l'on remonte tout d'une ligne et si tu traites plusieurs lignes tu serais décalé

Next iColCount

'MODIF A FAIRE ICI

'Supprime la ligne
Range("plageprogress").Rows(iRow).Delete
iRow = iRow - 1
 

lgvba

XLDnaute Nouveau
Re : Copier et effacer valeur a partir listbox-vba

Salu

Le code marche , il m ' efface la ligne parcontre ma plage nommer "plageprogress" dans excel qui doit etre afficher dans ma listbox a une erreur , le formule pour nommer la plage devient comme cela apres la macro

plageprogress =OFFSET(SPOTINPROGRESS!#REF!,0,0,COUNTA(SPOTINPROGRESS!$A$2:$A$97),COUNTA(SPOTINPROGRESS!$A$1:$J$1))

Dois je appliquer une autre methode pour nommer ma plage de lecture dans ma listbox

CDT
 

lgvba

XLDnaute Nouveau
Re : Copier et effacer valeur a partir listbox-vba

salu camarchepas

J' ai essayer ton code avec plusieurs exemple.. Malheureusement ton code fonctionne seulement sur la premiere ligne de la liste box si je veux selectionner la deuxieme ligne de listbox c ' est seulement la premiere ligne qui est prise en compte.

Pourrais tu m ' aider a regler ce petit probleme
Je te transmet le fichier avec les exemples .
 

Pièces jointes

  • SPTv2web2.xls
    234 KB · Affichages: 82
  • SPTv2web2.xls
    234 KB · Affichages: 86
  • SPTv2web2.xls
    234 KB · Affichages: 75

camarchepas

XLDnaute Barbatruc
Re : Copier et effacer valeur a partir listbox-vba

Lgvba,

Bon en fait , c'est le irow qui était mal initialisé, maintenant , il est égal à l'indexe de la liste + 1 puisque le 1° indexe est 0

Code:
Private Sub complete_ar_Click()
 Dim iListCount As Integer, iColCount As Integer, iRow As Integer
 Dim rStartCell As Range

 'Set a range variable to the first cell to recieve our data
 'Using "End(xlUp).Offset(1, 0)" _
  will give us the cell below the last entry
 Set rStartCell = SPOTCLASSIFIED.Range("A65536").End(xlUp).Offset(1, 0)
  'Loop as many times (less one) as there are entries in our list.
  'We must start from zero to use this in the Selected Property.
  For iListCount = 0 To ListBox2.ListCount - 1
     If ListBox2.Selected(iListCount) = True Then 'User has selected
 
      'MODIF A FAIRE ICI
       iRow = ListBox2.ListIndex + 1
        
       ListBox2.Selected(iListCount) = False
       'Now loop as many times as there are columns in MyRange
       For iColCount = 0 To Range("plageprogress").Columns.Count - 1
        'place the selected data into the table, starting from _
         range Ax And moving across As many columns As there are _
         In the range MyRange.
         rStartCell.Cells(iRow, iColCount + 1).Value = ListBox2.List(iListCount, iColCount)
                   
       Next iColCount
             
      'MODIF A FAIRE ICI
       Range("plageprogress").Rows(iRow).Delete
            
     End If
  Next iListCount
 Set rStartCell = Nothing
End Sub
 

Pièces jointes

  • SPTv2web2.xls
    111.5 KB · Affichages: 88
  • SPTv2web2.xls
    111.5 KB · Affichages: 106
  • SPTv2web2.xls
    111.5 KB · Affichages: 103

lgvba

XLDnaute Nouveau
Re : Copier et effacer valeur a partir listbox-vba

Salu Camarche pas

Effectivement , j ' ai essayer ton code , et le i row initialiser, malheureusement j ' ai un soucis avec ton code c ' est que quand les donner son transfere dans la feuille "SPOTCLASSIFIED", les lignes transferer sur la feuille ne se suive pas les une apres les autres, il me laisse de grand expace entres les valeurs. c' est juste ce petit soucis qu il faut regler


cdt
Lgvba
 

camarchepas

XLDnaute Barbatruc
Re : Copier et effacer valeur a partir listbox-vba

Lgvba ,

Et oui l'on avance , maois le code d'origine était vraiment pas sain.
Car avec l'objet Rstartcell , tu définis la derniere ligne de la feuille où tu veux copier et ensuite tu décalais de irow lignes et c'est là où cela ne marchait pas .

Ce coup ci je pense te voir revenir avec un grand sourire

Code:
Private Sub complete_ar_Click()
 Dim iListCount As Integer, iColCount As Integer
 Dim iRow As Integer
 Dim rStartCell As Range
 'Set a range variable to the first cell to recieve our data
 'Using "End(xlUp).Offset(1, 0)" _
  will give us the cell below the last entry
 Set rStartCell = SPOTCLASSIFIED.Range("A65536").End(xlUp).Offset(1, 0)
  'Loop as many times (less one) as there are entries in our list.
  'We must start from zero to use this in the Selected Property.
  For iListCount = 0 To ListBox2.ListCount - 1
     If ListBox2.Selected(iListCount) = True Then 'User has selected
 
      'MODIF A FAIRE ICI
       iRow = ListBox2.ListIndex + 1
        
       ListBox2.Selected(iListCount) = False
       'Now loop as many times as there are columns in MyRange
       For iColCount = 0 To Range("plageprogress").Columns.Count - 1
        'place the selected data into the table, starting from _
         range Ax And moving across As many columns As there are _
         In the range MyRange.
         
         'ICI , remplacement de irow par 1 , mais c'était déjà une mauvaise écriture du code
         rStartCell.Cells(1, iColCount + 1).Value = ListBox2.List(iListCount, iColCount)
                   
       Next iColCount
             
      'MODIF A FAIRE ICI
       Range("plageprogress").Rows(iRow).Delete
            
     End If
  Next iListCount
 Set rStartCell = Nothing
End Sub
 

Discussions similaires

Statistiques des forums

Discussions
312 488
Messages
2 088 861
Membres
103 979
dernier inscrit
imed