Boucle For - Concatener 2 variables pour en créer une 3ème

Bonjour,

Ci-dessous le code sur lequel je sèche

Dans la boucle for je souhaiterais que ColName & i fasse référence à ma variable ColName1 quand i = 1 (c'est à dire la valeur SALORG).

Sub Col_Sort2()
Dim i As Integer
Dim ColName1 As String
Dim ColName2 As String
Dim ColName3 As String

ColName1 = "SALORG"
ColName2 = "NOTI"
ColName3 = "DESC"

For i = 1 To 3

If Cells(1, i).Value <> ColName & i Then

Set Ref = Range("A1:DE1").Find(What:=ColName & i, LookIn:=xlValues, LookAt:=xlWhole)
Intersect(Range("A1").CurrentRegion, Ref.EntireColumn).Select
Selection.Cut

Columns(i).Select
Selection.Insert Shift:=xlToRight
Range("C1").Select

End If

Next

End Sub

Edit modo : code à mettre entre balises avec le bouton </> merci d'y faire attention la prochaine fois

Comment faire?

Merci pour votre support

Bonjour xicor,

Le plus simple, définir un tableau, voir ci-dessous.

Sub Col_Sort2()

Dim i As Integer
Dim aColName(1 To 3) As String

    aColName(1) = "SALORG"
    aColName(2) = "NOTI"
    aColName(3) = "DESC"

    For i = 1 To 3

        If Cells(1, i).Value <> aColName(i) Then

            Set Ref = Range("A1:DE1").Find(What:=aColName(i), LookIn:=xlValues, LookAt:=xlWhole)
            Intersect(Range("A1").CurrentRegion, Ref.EntireColumn).Select
            Selection.Cut

            Columns(i).Select
            Selection.Insert Shift:=xlToRight
            Range("C1").Select

        End If

    Next

End Sub

Cdlt,

Cylfo

bonjour,

utilise une variable de type array

Sub Col_Sort2()

    Dim i As Integer
    Dim ColName(3) As String

    ColName(1) = "SALORG"
    ColName(2) = "NOTI"
    ColName(3) = "DESC"

    For i = 1 To 3

        If Cells(1, i).Value <> ColName(i) Then

            Set Ref = Range("A1:DE1").Find(What:=ColName(i), LookIn:=xlValues, LookAt:=xlWhole)
            Intersect(Range("A1").CurrentRegion, Ref.EntireColumn).Select
            Selection.Cut

            Columns(i).Select
            Selection.Insert Shift:=xlToRight
            Range("C1").Select

        End If

    Next

End Sub
Rechercher des sujets similaires à "boucle concatener variables creer 3eme"