2017-10-20 19 views
0

J'ai quatre feuilles de calcul et j'écris une formule dans la cellule F2 qui déclenche un remplissage pour tout le reste. J'ai déjà le déclencheur private sub pour appeler ma procédure qui fonctionne. Y a-t-il un moyen de rendre ce sous-marin plus efficace?déclenchez la formule de remplissage basée sur la cellule d'une autre feuille de calcul

Option Explicit 

Sub FillDownFormula_test2() 
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' 
' Filldown a formula for in column of data. 
' Assumes a data table with headings in the first row, 
' the formula in the second row and is the active cell. 
' this one is tested based on set range 
' Test results were positive 
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' 
Dim rng As Range, rng2 As Range 
Dim rngData As Range 
Dim rngFormula As Range, rng2Formula As Range 
Dim rowData As Long 
Dim colData As Long 
Dim wb As Workbook: Set wb = ThisWorkbook 
Dim ws As Worksheet, ws2 As Worksheet 

Set ws = wb.Sheets("Feuil1") 


With ws 

' Set the ranges 
Set rng = wb.Sheets("Feuil1").Range("F2") 
Set rngData = rng.CurrentRegion 

' Set the row and column variables 
rowData = rngData.CurrentRegion.Rows.Count 
colData = rng.Column 

' Set the formula range and fill down the formula 
Set rngFormula = rngData.Offset(1, colData - 1).Resize(rowData - 1, 1) 
rngFormula.FillDown 

'G2 is a different formulas but same on every sheets 
Set rng2 = wb.Sheets("Feuil1").Range("G2") 


' Set the row and column variables 
rowData = rngData.CurrentRegion.Rows.Count 
colData = rng2.Column 

' Set the formula range and fill down the formula 
Set rng2Formula = rngData.Offset(1, colData - 1).Resize(rowData - 1, 1) 
rng2Formula.FillDown 
End With 



With ws2 
Set rng = wb.Sheets("Feuil2").Range("F2") 
Set rngData = rng.CurrentRegion 

' Set the row and column variables 
    rowData = rngData.CurrentRegion.Rows.Count 
    colData = rng.Column 

' Set the formula range and fill down the formula 
Set rngFormula = rngData.Offset(1, colData - 1).Resize(rowData - 1, 1) 
rngFormula.FillDown 

Set rng2 = wb.Sheets("Feuil2").Range("G2") 


' Set the row and column variables 
rowData = rngData.CurrentRegion.Rows.Count 
colData = rng2.Column 

' Set the formula range and fill down the formula 
Set rng2Formula = rngData.Offset(1, colData - 1).Resize(rowData - 1, 1) 
    rng2Formula.FillDown 



End With 
End Sub 

Une dernière question: quel aurait été le plus efficace de faire une plage à l'époque, il me permet d'entrer dans la formule avant de faire le second. (J'ai six formules à filldown)

merci

Répondre

0

Il pourrait ressembler à quelque chose comme ça ...

Option Explicit 

Sub FillDownFormula_test2() 
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' 
' Filldown a formula for in column of data. 
' Assumes a data table with headings in the first row, 
' the formula in the second row and is the active cell. 
' this one is tested based on set range 
' Test results were positive 
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' 
    Dim wb As Workbook 
    Set wb = ActiveWorkbook 

    Dim ws As Worksheet 
    Dim ws2 As Worksheet 

    Set ws = wb.Sheets("Feuil1") 

    With ws 
     Call FillDownData(.Range("F2"), Range("F2").CurrentRegion) 
     Call FillDownData(.Range("G2"), Range("G2").CurrentRegion) 
    End With 

    Set ws2 = wb.Sheets("Feuil2") 

    With ws2 
     Call FillDownData(.Range("F2"), Range("F2").CurrentRegion) 
     Call FillDownData(.Range("G2"), Range("G2").CurrentRegion) 
    End With 

End Sub 

Function FillDownData(rng As Range, rngData As Range) 

    Dim rowData As Long 
    Dim colData As Long 
    Dim rngFormula As Range 

' Set the row and column variables 
    rowData = rngData.CurrentRegion.Rows.Count 
    colData = rng.column 
Debug.Print rowData & " " & colData 
' Set the formula range and fill down the formula 
    'Set rngFormula = rngData.Offset(1, colData - 1).Resize(rowData - 1, 1) 
    'rngFormula.FillDown 

End Function 
+0

Je l'ai essayé, mais il semble comme une boucle inachevée sur la liste plus – Mdh

+0

Je ne Je ne vois pas du tout une boucle ici, je ne suis pas sûr de ce que vous voulez dire ... Aussi, essayez d'utiliser 'Debug.Printer' quelque part pour déboguer là où il s'arrête – Maldred

+0

Debug va vous aider à continuer à fonctionner? ok je vais l'essayer. Je ne sais pas que ça marche tout seul mais avec l'événement déclencheur ça ne s'arrête jamais. Quoi qu'il en soit, je reviendrai vers vous – Mdh