Erreur 1004 sur une boucle
Salut le forum.
Je m'aguerris peu à peu sur le VBA, mais certains mystères d'excel me résistent toujours
Voici le topo : j'ai des colonnes dont la première valeur est une date dans mes deux classeurs. Si cette date est identique, alors on colle les données de la colonne du premier classeur vers la colonne correspondante dans le deuxième, sinon on test la date suivante.
Sub MaJ_depuis_relevés()
Dim i As Integer
Dim j As Integer
Dim derligne As Integer
Dim dercol As Integer
Dim dercol_Source As Long
Dim Source As Workbook
With ThisWorkbook.Sheets("Suivi Machine")
derligne = .Cells(Rows.Count, 2).End(xlUp).Row
dercol = 118
.Range(.Cells(27, 2), .Cells(derligne + 1, dercol)).ClearContents
Application.Workbooks.Open "Z:\xxxx.xlsm"
Set Source = ActiveWorkbook
dercol_Source = Source.Sheets("Concentrations").Cells(3, Columns.Count).End(xlToLeft).Column
For i = 19 To dercol
For j = 5 To dercol_Source
If .Cells(25, i).Value = Source.Sheets("Concentrations").Cells(3, j).Value Then
Source.Sheets("Concentrations").Range(Source.Sheets("Concentrations").Cells(4, j), Source.Sheets("Concentrations").Cells(64, j)).Copy
.Range(.Cells(27, i)).Paste
End If
Next j
Next i
End With
End SubL'erreur se fait au moment de coller sur .Range(.Cells(27, i)).Paste. A noter qu'au moment de l'erreur je peux parfaitement aller dans la feuille et coller manuellement mes données. J'en déduis que c'est ma façon de "sélectionner" ma cellule (27,i) qui est mauvaise, mais je ne vois pas en quoi. J'ai vérifier que mes variables (dercol, i, j, etc...) sont bien attribuées correctement.
Merci d'avance !
Bonjour,
Essaie ainsi :
Option Explicit
Sub MaJ_depuis_relevés()
Dim WB As Workbook, WBSource As Workbook
Dim WS As Worksheet, WSSource As Worksheet
Dim lastCol As Long, lastCol2 As Long, lastRow As Long
Dim I As Long, J As Long
Application.ScreenUpdating = False
'----------------------------------------------------------------------
Set WB = ThisWorkbook
Set WS = WB.Worksheets("Suivi Machine")
With WS
lastRow = .Cells(Rows.Count, 2).End(xlUp).Row
.Range(.Cells(27, 2), .Cells(lastRow + 1, lastCol)).ClearContents
End With
lastCol = 118
'----------------------------------------------------------------------
Set WBSource = Workbooks.Open("Z:\xxxx.xlsm")
Set WSSource = WBSource.Worksheets("Concentrations")
lastCol2 = WSSource.Cells(3, Columns.Count).End(xlToLeft).Column
'----------------------------------------------------------------------
For I = 19 To lastCol
For J = 5 To lastCol2
If WS.Cells(25, I).Value = WSSource.Cells(3, J).Value Then
WSSource.Range(Cells(4, J), Cells(64, J)).Copy _
Destination:=WS.Cells(27, I)
End If
Next J
Next I
'----------------------------------------------------------------------
Set WSSource = Nothing: Set WS = Nothing
Set WBSource = Nothing: Set WB = Nothing
End SubSalut,
Merci pour ton aide.
J'ai défini lastcol un peu plus haut (sinon j'ai une erreur 1004 au moment d'effacer mon tableau).
J'ai aussi remplacé les Long par des Integer, moins consommateurs (je reste largement sous les 2^15 valeurs).
Petit up. J'ai voulu un peu étoffer ma macro et... je crois que je l'ai cassé :/ (toujours erreur 1004).
Si je supprime mon bloc avec les FormulaR1C1 j'ai quand même une erreur 1004 sur :
WSSource.Range(Cells(4, J), Cells(64, J)).Copy _
Destination:=WS.Cells(27, I)
Voici le code mis à jour :
Sub MaJ_depuis_relevés()
Dim WB As Workbook, WBSource As Workbook
Dim WS As Worksheet, WSSource As Worksheet
Dim lastCol As Long, lastCol2 As Long, lastRow As Long
Dim I As Long, J As Long
Application.ScreenUpdating = False
'----------------------------------------------------------------------
Set WB = ThisWorkbook
Set WS = WB.Worksheets("Suivi Machine")
With WS
lastRow = .Cells(Rows.Count, 2).End(xlUp).Row
lastCol = 118
.Range(.Cells(27, 2), .Cells(lastRow + 1, lastCol)).ClearContents
End With
'----------------------------------------------------------------------
Set WBSource = Workbooks.Open("Z:xxxx.xlsm")
Set WSSource = WBSource.Worksheets("Concentrations")
lastCol2 = WSSource.Cells(3, Columns.Count).End(xlToLeft).Column
'----------------------------------------------------------------------
WSSource.Range("B4:C64").Copy
WS.Range("B27").PasteSpecial Paste:=xlValues
For I = 27 To lastRow
WS.Range("D" & I).FormulaR1C1 = "=INDEX(Min;EQUIV($B" & I & ";Machines;0))"
WS.Range("E" & I).FormulaR1C1 = "=INDEX(Max;EQUIV($B" & I & ";Machines;0))"
Next I
For I = 19 To lastCol
For J = 5 To lastCol2
If WS.Cells(25, I).Value = WSSource.Cells(3, J).Value Then
WSSource.Range(Cells(4, J), Cells(64, J)).Copy _
Destination:=WS.Cells(27, I)
End If
Next J
Next I
lastRow = WSSource.Cells(Rows.Count, 2).End(xlUp).Row
WBSource.Close False
'----------------------------------------------------------------------
Set WSSource = Nothing: Set WS = Nothing
Set WBSource = Nothing: Set WB = Nothing
End SubDu coup j'ai réessayé de partir sur le code de Jean-Eric et... Erreur 1004 "La méthode "Range" de l'objet "_Worsheet" a échouée".
Pourtant je n'ai pas fait de modif, je ne comprend pas
Edith : Bon ok, j'avais ajouté des valeurs sous le tableau, manifestement la macro n'a pas aimé. reste que ça bloque sur mes Range.formula maintenant.