macro pourn selectionner dans une autre feuille

arturo

XLDnaute Nouveau
Bonjour à tous
j'ai ce code qui repère l'intervalle entre la cellule "A2" et la dernière cellule qui contient quelque chose.
ça marche si je l'utilise dans la feuille où je l'ai enregistré:

Private Sub CommandButton1_Click()
Set LastCell = ActiveSheet.UsedRange.SpecialCells(xlLastCell)
Range(Range("A2"), LastCell).Select
Selection.ClearContents
End Sub

Par contre si je veux appliquer le code dans une autre feuille ça ne marche plus:

Private Sub CommandButton1_Click()
Sheets("Feuil2").Activate
Set LastCell = ActiveSheet.UsedRange.SpecialCells(xlLastCell)
Range(Range("A2"), LastCell).Select
Selection.ClearContents
End Sub

J'ai fait plusieurs essais mais je ne trouve pas la solution:confused:

Quelqu'un peut-il m'aider?
Merci
 

pierrejean

XLDnaute Barbatruc
Re : macro pourn selectionner dans une autre feuille

bonjour arturo

A tester:

Code:
Private Sub CommandButton1_Click()
 With Sheets("Feuil2")
   lastcell = .UsedRange.SpecialCells(xlLastCell)
  .Range(.Range("A2"), .Range("A" & lastcell)).ClearContents
 End With
End Sub
 
Dernière édition:

tototiti2008

XLDnaute Barbatruc
Re : macro pourn selectionner dans une autre feuille

Bonjour PierreJean, gimli, arturo, Benjy

un essai :

Code:
Private Sub CommandButton1_Click()
 With Sheets("Feuil2")
   Set lastcell = .UsedRange.SpecialCells(xlLastCell)
  .Range(.Range("A2"), lastcell).ClearContents
 End With
End Sub
 

Discussions similaires