XL 2010 Simplifier une macro

jlbcall

XLDnaute Occasionnel
Bonjour à toutes et à tous,

Je début dans le VBA et aimerais avoir un peu d'aide pour simplifier la macro ci-dessous:
Je recherche des valeurs par rapport à un code sur plusieurs mois(12) et ces formules doivent s’étendre jusqu'à la dernier ligne.
Je suis arrivé + ou - à faire ce que je voulais mais je pense qu'avec une boucle cela serait mieux.
Et j'aimerai surtout que la formule disparaisse en laissant uniquement la valeur.
ci-joint le fichier si besoin.

Sub ImportJourMois()
'
Dim DernLigne As Long
DernLigne = Range("A" & Rows.Count).End(xlUp).Row
Sheets("ImportData").Select
Range("AR1").Select
ActiveCell.FormulaR1C1 = "Janvier"
Range("AR1").Select
Selection.AutoFill Destination:=Range("AR1:BC1"), Type:=xlFillDefault

Range("AR2").Select
ActiveCell.FormulaR1C1 = "=VLOOKUP(RC1,NbrJourMois,2,FALSE)"
Range("AS2").Select
ActiveCell.FormulaR1C1 = "=VLOOKUP(RC1,NbrJourMois,3,FALSE)"
Range("AT2").Select
ActiveCell.FormulaR1C1 = "=VLOOKUP(RC1,NbrJourMois,4,FALSE)"
Range("AU2").Select
ActiveCell.FormulaR1C1 = "=VLOOKUP(RC1,NbrJourMois,5,FALSE)"
Range("AV2").Select
ActiveCell.FormulaR1C1 = "=VLOOKUP(RC1,NbrJourMois,6,FALSE)"
Range("AW2").Select
ActiveCell.FormulaR1C1 = "=VLOOKUP(RC1,NbrJourMois,7,FALSE)"
Range("AX2").Select
ActiveCell.FormulaR1C1 = "=VLOOKUP(RC1,NbrJourMois,8,FALSE)"
Range("AY2").Select
ActiveCell.FormulaR1C1 = "=VLOOKUP(RC1,NbrJourMois,9,FALSE)"
Range("AZ2").Select
ActiveCell.FormulaR1C1 = "=VLOOKUP(RC1,NbrJourMois,10,FALSE)"
Range("BA2").Select
ActiveCell.FormulaR1C1 = "=VLOOKUP(RC1,NbrJourMois,11,FALSE)"
Range("BB2").Select
ActiveCell.FormulaR1C1 = "=VLOOKUP(RC1,NbrJourMois,12,FALSE)"
Range("BC2").Select
ActiveCell.FormulaR1C1 = "=VLOOKUP(RC1,NbrJourMois,13,FALSE)"

Range("AR2:bc2").AutoFill Destination:=Range("AR2:BC" & DernLigne)
Range("AR1").Select

End Sub
 

Pièces jointes

  • Test4_Macro.xlsm
    3 MB · Affichages: 20

vgendron

XLDnaute Barbatruc
Hello
en simplifiant comme ceci??
VB:
Sub ImportJourMois()
'
Dim DernLigne As Long

With Sheets("ImportData")
    DernLigne = .Range("A" & .Rows.Count).End(xlUp).Row
    .Range("AR1").FormulaR1C1 = "Janvier"
    .Range("AR1").AutoFill Destination:=.Range("AR1:BC1"), Type:=xlFillDefault
   
    For i = 1 To 12
        .Range("AR2").Offset(0, i - 1).FormulaR1C1 = "=VLOOKUP(RC1,NbrJourMois," & i + 1 & ",FALSE)"
    Next i
    .Range("AR2:bc2").AutoFill Destination:=.Range("AR2:BC" & DernLigne)
    .Range("AR2:BC" & DernLigne) = .Range("AR2:BC" & DernLigne).Value
End With
End Sub
 

jlbcall

XLDnaute Occasionnel
Merci beaucoup c'est top
j'ai testé et cela fonctionne bien
J'essai de comprendre la boucle:
Serait il possible de m'expliquer le code en rouge si possible
.Range("AR2").Offset(0, i - 1).FormulaR1C1 = "=VLOOKUP(RC1,NbrJourMois," & i + 1 & ",FALSE)"

Merci encore et bonne soirée
 

Statistiques des forums

Discussions
312 195
Messages
2 086 082
Membres
103 112
dernier inscrit
cuq-laet