2017-10-20 60 views
0

我有四個工作表,我在單元格F2中編寫了一個公式,它觸發了所有其他的填充。我已經有觸發器的私人小組來調用我的程序工作。有沒有辦法讓這個子更有效率?基於另一個工作表的單元格的觸發器填充公式

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 

最後一個問題:當時做一個範圍的效率最高,因此它允許我在做第二個範圍之前輸入公式。 (我有六個公式filldown)

謝謝

回答

0

它可能是這個樣子......

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

我嘗試過,但它看起來像大名單 – Mdh

+0

我不上一個未完成的循環t在這裏看到一個循環,我不知道你的意思是什麼......另外,嘗試使用Debug.Printer來調試它停止的位置 – Maldred

+0

調試將幫助它繼續運行?好吧,我要去嘗試一下。我不知道它是獨立的,但事件觸發器永不止步。無論如何,我會盡快回復您 – Mdh

相關問題