Comment remplacer ...

Résolu
Bonjour,
Je souhaite remplacer la ligne

.SetRange Range("G1:Co121") qui a une donnée fixes par la ligne

.setRange (Cells(1, 7), Cells(145, derncol)) qui a une donnée variable

Et dans la première partie

Range( _
"G10:CO10")
par également la reprise de la variable derncol à la place de la colonne (CO)

dans la macro suivante mais il me dit qu'il y a une erreur de syntaxe... et je ne connais évidemment pas bien tous ça car fait avec l'enregistreur de macro...

Merci d'avance pour votre attention et votre aide.


Dim derncol As Integer

derncol = ActiveSheet.Cells(14, Cells.Columns.Count).End(xlToLeft).Column
For n = 7 To derncol
Columns(n).Hidden = False
Next n
Range(Columns(7), Columns(derncol)).Select
ActiveWorkbook.Worksheets("Panorama FM").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Panorama FM").Sort.SortFields.Add Key:=Range( _
"G10:CO10"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortNormal
With ActiveWorkbook.Worksheets("Panorama FM").Sort
.SetRange Range("G1:Co121") ' à remplacer par .setRange (Cells(1, 7), Cells(145, derncol))
.Header = xlGuess
.MatchCase = False
.Orientation = xlLeftToRight
.SortMethod = xlPinYin
.Apply
End With

4 réponses

  1. Bonjour,

    Pour ça il existe une touche "Miracle" : F1
    VBA indique la ligne de code en erreur, il suffit de mettre le pointeur texte sur chaque mot de la ligne et de taper F1
    0
    1. Bonjour,
      Il faut ajouter "Range" :
      .SetRange Range(Cells(1, 7), Cells(145, derncol))

      Cordialement,
      didibonf
      0
      1. Un grand merci didibonf. Ca marche du tonnerre.
        Et dans la partie supérieur je souhaite également remplacer (en souligné)

        ActiveWorkbook.Worksheets("Panorama FM").Sort.SortFields.Add Key:=Range( _
        "G10:CO10"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
        xlSortNormal

        par quelque chose comme :

        ActiveWorkbook.Worksheets("Panorama FM").Sort.SortFields.Add Key:=Range( _
        "G10:COderncol"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
        xlSortNormal

        C'est également un problème de syntaxe ;-)
        Merci d'avance.
        Looping38
        0
        1. C'est pour la dernière colonne ou la dernière ligne ? Si c'est pour la dernière ligne on peut faire :
          Range( "G10:CO" & dernligne) sinon il faut reprendre la même structure qu'auparavant Range(Cells(10, 7), Cells(10, derncol)
          0
      2. Bonjour Didibonf et merci pour ce retour.
        Avec du temps, j'ai pu adapter ta réponse à mon cas, puisqu'il s'agissait de colonnes et non de lignes...
        J'avais mal exprimé ma demande.

        Voila ce que j'ai fais et qui manifestement marche.

        Range(Columns(7), Columns(derncol - 1)).Select
        ActiveWorkbook.Worksheets("Panorama FM").Sort.SortFields.Clear
        ActiveWorkbook.Worksheets("Panorama FM").Sort.SortFields.Add2 Key:=Range( _
        Cells(10, 7), Cells(10, derncol)), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
        ActiveWorkbook.Worksheets("Panorama FM").Sort.SortFields.Add2 Key:=Range( _
        Cells(8, 7), Cells(8, derncol)), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
        With ActiveWorkbook.Worksheets("Panorama FM").Sort
        .SetRange Range(Cells(1, 7), Cells(145, derncol))
        .Header = xlGuess
        .MatchCase = False
        .Orientation = xlLeftToRight
        .SortMethod = xlPinYin
        .Apply
        End With
        0