簡體   English   中英

根據另一個工作表的單元格觸發填充公式

[英]trigger filldown formula based on cell of another worksheet

我有四個工作表,並在單元格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

最后一個問題:一次做一個范圍是最有效的,因此它允許我在做第二個之前輸入公式。 (我有六個公式可以填寫)

謝謝

它可能看起來像這樣...

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM