动态公式自动填充

时间:2016-05-16 13:36:39

标签: excel vba excel-formula autofill

我想知道如何将此代码转换为第I列的动态范围自动填充,以便以后可以自由编辑Excel工作表而无需调整vba代码。

build.gradle

2 个答案:

答案 0 :(得分:0)

查看自动填充功能:

Range("I2:I" & H).AutoFill Destination:=Range("I2:I2000"), Type:=xlFillDefault

答案 1 :(得分:0)

首先,您只是隐含地将 Report-Charles,M 引用为ActiveSheet property以将.Formula放入其中;没有明确指出直接参考。

其次,要分配公式,请使用直接.Formula书写,Range.AutoFill methodRange.FillDown method

最后,通过VBA编写公式.Formula应该使用EN-US语法和区域设置。如果您要使用非{EN-US}区域设置,例如3,354,25,请使用Range.FormulaLocal property

最重要的是,将数字视为数字;不像文本那样看起来像数字。

with Worksheets("Report-Charles, M")
     h = .UsedRange.Rows.Count
    .Range("I2:I" & H).FormulaLocal = _
      "=IF((AND(OR(E2=""Unrated"", E2=TEXT(,)), OR(G2<>""Unrated"", G2<>TEXT(,)))), G2, IF(E2>=4,25, ""High Performance"", IF(E2<3,35, ""Low Performance"", ""Medium Performance"")))"
    .Range("I2:I" & H).Formula = _
      "=IF((AND(OR(E2=""Unrated"", E2=TEXT(,)), OR(G2<>""Unrated"", G2<>TEXT(,)))), G2, IF(E2>=4.25, ""High Performance"", IF(E2<3.35, ""Low Performance"", ""Medium Performance"")))"
end with

with Worksheets("Report-Charles, M")
     'any of the following will fill column I with formulas to the last row in .UsedRange
     'all of the following assumes a valid formula in I2
     h = .UsedRange.Rows.Count
    .Range("I2:I" & H).Formula = .Range("I2").Formula
    .Range("I2:I" & H).FillDown
    .Range("I2:I" & H).DataSeries Rowcol:=xlColumns, Type:=xlAutoFill
    .Range("I2").AutoFill Destination:=.Range("I2:I" & H), Type:=xlFillDefault
end with

我缩短了一个IF条件,因为AND是不必要的,而Medium是默认响应。使用TEXT(,)与说""相同,而不必将双引号加倍。

相关问题