2016-05-16 85 views
1

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

Sub Rowcount() 
    'Charles, M 
    Dim H As Long 
    Set sh = Worksheets("Report-Charles, M") 


    H = sh.UsedRange.Rows.Count 

     Range("I2:I" & H).Formula = "=IF((AND(OR(E2=""Unrated"",E2=""""),OR(G2<>""Unrated"",G2<>""""))),G2,IF(E2>=""4,25"",""High Performance"",IF(E2<""3,35"",""Low Performance"",IF(AND(E2>=""3,35"",E2<""4,25""),""Medium Performance"",""Unrated""))))" 

    MsgBox (H) & "Rows have been Autofilled with 3 scale Rating Results" 
End Sub 

回答

0

看一看到自动填充功能:

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

如果'Range(“I2:I”&H).AutoFill'只是'Range(“I2”)。AutoFill'? – Jeeped

+0

我认为这并不重要,因为他的所有领域(“I2:I”和H)都包含相同的基础。两者都应该工作。 – Clemens

+0

回去测试一下。你会发现你错了,我做到了。 – Jeeped

0

首先,你只是含蓄地引用报告查尔斯,男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是不必要的,培养基为默认响应。使用TEXT(,)与说""相同,不必将双引号加倍。