繁体   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