Méthode Range a échoué
Bonjour à tous,
Je vous sollicite pour régler mon problème de Méthode range...
Je tente de mettre à jour des valeurs d'un classeur avec les données disponibles dans un autre classeur.
Ce qui complique la chose, c'est que la colonne et la ligne des deux classeurs sont variables.
Tout fonctionne très bien jusqu'à la ligne : bd.Cells(i, statbudg).Value = budget.Range(maligne & statutbudg).Value
Private Sub budget()
Dim wbbudget As Workbook
Dim budget As Worksheet
Set wbbudget = ActiveWorkbook
Set budget = wbbudget.Sheets("Sommaire")
Dim anneefin As String
Application.DisplayAlerts = False
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
anneefin = Sheets("Sommaire").ComboBox1.Value
ActiveSheet.AutoFilterMode = False
Application.Calculation = xlCalculationManual
Application.ScreenUpdating = False
'Définition des variables budget
Dim Nbheures As Integer
Dim estimcout As Integer
Dim tauxhor As Integer
Dim estimefforts As Integer
Dim probabilite As Variant
Dim source As String
Dim statutbudg As String
Dim pourgdcex As Integer
Dim pourformex As Integer
Dim budgcoutgdc As Integer
Dim budgcoutform As Integer
Dim budgcoutext As Integer
Dim budgcouttotal As Integer
Dim budgcoutextpond As Integer
Dim budgcouttotalpond As Integer
statutbudg = budget.Cells.Find(What:="Statut budget", After:=Cells(2, 1), searchorder:=xlByColumns, SearchDirection:=xlNext, LookAt:=xlWhole, MatchCase:=True).Column
tauxhor = budget.Cells.Find(What:="Taux horaire ($/h) externe", After:=Cells(2, 1), searchorder:=xlByColumns, SearchDirection:=xlNext, LookAt:=xlWhole, MatchCase:=True).Column
probabilite = budget.Cells.Find(What:="Probabilité projet", After:=Cells(2, 1), searchorder:=xlByColumns, SearchDirection:=xlNext, LookAt:=xlPart, MatchCase:=True).Column
pourgdcex = budget.Cells.Find(What:="Pourcentage des heures GDC à l'externe", After:=Cells(2, 1), searchorder:=xlByColumns, SearchDirection:=xlNext, LookAt:=xlWhole, MatchCase:=True).Column
pourformex = budget.Cells.Find(What:="Pourcentage des heures Formation à l'externe", After:=Cells(2, 1), searchorder:=xlByColumns, SearchDirection:=xlNext, LookAt:=xlWhole, MatchCase:=True).Column
budgcoutgdc = budget.Cells.Find(What:="Total coût GDC et communications", After:=Cells(2, 1), searchorder:=xlByColumns, SearchDirection:=xlNext, LookAt:=xlWhole, MatchCase:=True).Column
budgcoutform = budget.Cells.Find(What:="Total formation", After:=Cells(2, 1), searchorder:=xlByColumns, SearchDirection:=xlNext, LookAt:=xlWhole, MatchCase:=True).Column
budgcoutext = budget.Cells.Find(What:="Estimation coût total externe $ sans accompagnement", After:=Cells(2, 1), searchorder:=xlByColumns, SearchDirection:=xlNext, LookAt:=xlWhole, MatchCase:=True).Column
budgcouttotal = budget.Cells.Find(What:="Estimation coût total $", After:=Cells(2, 1), searchorder:=xlByColumns, SearchDirection:=xlNext, LookAt:=xlWhole, MatchCase:=True).Column
budgcoutextpond = budget.Cells.Find(What:="Coût externe pondéré", After:=Cells(2, 1), searchorder:=xlByColumns, SearchDirection:=xlNext, LookAt:=xlWhole, MatchCase:=True).Column
budgcouttotalpond = budget.Cells.Find(What:="Coût pondéré le plus probable", After:=Cells(2, 1), searchorder:=xlByColumns, SearchDirection:=xlNext, LookAt:=xlWhole, MatchCase:=True).Column
Dim plage As Long
plage = budget.Range("b500").End(xlUp).Row
Dim wbbd As Workbook
Dim bd As Worksheet
Var_Chemin = "\\dfs\Repertoires communs\Performance et mobilisation\0- Fiches mandats\BDDO.xlsx" '******************************************
Workbooks.Open Var_Chemin, 0, ReadOnly:=False
Set wbbd = ActiveWorkbook
Set bd = wbbd.Sheets("Feuil1")
'Définition des variables BD
Dim prob As Integer
Dim coutint As Integer
Dim hrsintpond As Integer
Dim hrsext As Integer
Dim hrsextpond As Integer
Dim coutext As Integer
Dim couttotal As Integer
Dim coutextpond As Integer
Dim couttotalpond As Integer
Dim coutgdc As Integer
Dim coutform As Integer
Dim gdcexpour As Integer
Dim formexpour As Integer
Dim total As Integer
Dim statbudg As String
gdcexpour = bd.Cells.Find(What:="gdcexpour", After:=Cells(1, 1), searchorder:=xlByColumns, SearchDirection:=xlNext, LookAt:=xlWhole, MatchCase:=True).Column
formexpour = bd.Cells.Find(What:="formexpour", After:=Cells(1, 1), searchorder:=xlByColumns, SearchDirection:=xlNext, LookAt:=xlWhole, MatchCase:=True).Column
total = bd.Cells.Find(What:="total", After:=Cells(1, 1), searchorder:=xlByColumns, SearchDirection:=xlNext, LookAt:=xlWhole, MatchCase:=True).Column
prob = bd.Cells.Find(What:="prob", After:=Cells(1, 1), searchorder:=xlByColumns, SearchDirection:=xlNext, LookAt:=xlWhole, MatchCase:=True).Column
coutint = bd.Cells.Find(What:="coutint", After:=Cells(1, 1), searchorder:=xlByColumns, SearchDirection:=xlNext, LookAt:=xlWhole, MatchCase:=True).Column
hrsintpond = bd.Cells.Find(What:="hrsintpond", After:=Cells(1, 1), searchorder:=xlByColumns, SearchDirection:=xlNext, LookAt:=xlWhole, MatchCase:=True).Column
hrsext = bd.Cells.Find(What:="hrsext", After:=Cells(1, 1), searchorder:=xlByColumns, SearchDirection:=xlNext, LookAt:=xlWhole, MatchCase:=True).Column
hrsextpond = bd.Cells.Find(What:="hrsextpond", After:=Cells(1, 1), searchorder:=xlByColumns, SearchDirection:=xlNext, LookAt:=xlWhole, MatchCase:=True).Column
coutext = bd.Cells.Find(What:="coutext", After:=Cells(1, 1), searchorder:=xlByColumns, SearchDirection:=xlNext, LookAt:=xlWhole, MatchCase:=True).Column
coutextpond = bd.Cells.Find(What:="coutextpond", After:=Cells(1, 1), searchorder:=xlByColumns, SearchDirection:=xlNext, LookAt:=xlWhole, MatchCase:=True).Column
couttotal = bd.Cells.Find(What:="couttotal", After:=Cells(1, 1), searchorder:=xlByColumns, SearchDirection:=xlNext, LookAt:=xlWhole, MatchCase:=True).Column
couttotalpond = bd.Cells.Find(What:="couttotalpond", After:=Cells(1, 1), searchorder:=xlByColumns, SearchDirection:=xlNext, LookAt:=xlWhole, MatchCase:=True).Column
coutgdc = bd.Cells.Find(What:="coutgdc", After:=Cells(1, 1), searchorder:=xlByColumns, SearchDirection:=xlNext, LookAt:=xlWhole, MatchCase:=True).Column
coutform = bd.Cells.Find(What:="coutform", After:=Cells(1, 1), searchorder:=xlByColumns, SearchDirection:=xlNext, LookAt:=xlWhole, MatchCase:=True).Column
statbudg = bd.Cells.Find(What:="statbudg", After:=Cells(1, 1), searchorder:=xlByColumns, SearchDirection:=xlNext, LookAt:=xlWhole, MatchCase:=True).Column
Dim nbrows As Integer
nbrows = Range("b500").End(xlUp).Row
For i = 2 To nbrows
Dim recherche As Variant
recherche = bd.Cells(i, 2).Value
budget.Activate
budget.Range(Cells(3, 2), Cells(plage, 2)).Find(What:=recherche, LookIn:=xlValues, LookAt:=xlWhole).Activate
Dim maligne As Integer
maligne = ActiveCell.Row
bd.Activate
bd.Cells(i, statbudg).Value = budget.Range(maligne & statutbudg).Value
bd.Cells(i, prob).Value = budget.Range(maligne & probabilite).Value
bd.Cells(i, gdcexpour).Value = budget.Range(maligne & pourgdcex).Value
bd.Cells(i, formexpour).Value = budget.Range(maligne & pourformex).Value
bd.Cells(i, coutext).Value = budget.Range(maligne & budgcoutext).Value
bd.Cells(i, coutextpond).Value = budget.Range(maligne & budgcoutextpond).Value
bd.Cells(i, couttotal).Value = budget.Range(maligne & budgcouttotal).Value
bd.Cells(i, couttotalpond).Value = budget.Range(maligne & budgcouttotalpond).Value
bd.Cells(i, coutgdc).Value = budget.Range(maligne & budgcoutgdc).Value
bd.Cells(i, coutform).Value = budget.Range(maligne & budgcoutform).Value
Next i
End Sub
Merci de votre aide!
Bonsoir,
remplace ceci
Range(maligne & statutbudg)
par
Cells(maligne, statutbudg)
et fais de même pour toutes les lignes qui suivent
Bonjour h2S4,
Je l'ai testé, mais malheureusement, cela ne fonctionne pas...
philidor
Bonjour,
concrètement qu'est-ce qui ne fonctionne pas ? aide-nous à t'aider et surtout jette un oeil sur les conseils d'utilisation du forum.
Bonjour,
Lorsque j'exécute la macro, elle reconnait chacune des variables, mais elle ne réussit pas à ajouter la valeur à la cellule.
et j'ai le message Méthode range a échouée...
Je veux mettre la valeur de budget dans BD....
bonsoir,
sans autre information, l'erreur est due à tes données, ... qu'apparemment tu ne veux pas nous communiquer.
Salut,
ah non, je n'ai pas de problèmes à les communiquer. (Désolé du délai, j'étais en dehors de la ville).
voilà!
Bonjour,
j'ai fait la correction proposée sur les 3 premières lignes en question. Je te laisse faire la suite.
'lignes corrigées
bd.Cells(i, statbudg).Value = budget.Cells(statbudg, maligne).Value
bd.Cells(i, prob).Value = budget.Cells(probabilite, maligne).Value
bd.Cells(i, gdcexpour).Value = budget.Cells(pourgdcex, maligne).Value
'lignes à corriger de la même manière (remplace range par cells inverser les 2 paramètres et remplacer & apr une virgule
bd.Cells(i, formexpour).Value = budget.Range(maligne & pourformex).Value
bd.Cells(i, coutext).Value = budget.Range(maligne & budgcoutext).Value
bd.Cells(i, coutextpond).Value = budget.Range(maligne & budgcoutextpond).Value
bd.Cells(i, couttotal).Value = budget.Range(maligne & budgcouttotal).Value
bd.Cells(i, couttotalpond).Value = budget.Range(maligne & budgcouttotalpond).Value
bd.Cells(i, coutgdc).Value = budget.Range(maligne & budgcoutgdc).Value
bd.Cells(i, coutform).Value = budget.Range(maligne & budgcoutform).Value
Next i
Merci!! Finalement, je ne devais pas inverser les deux critères de ma cellule, mais la ça marche très bien.
Un grand merci de ta patience!