Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/28.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 公式自动填充VBA_Excel_Vba_Formula - Fatal编程技术网

Excel 公式自动填充VBA

Excel 公式自动填充VBA,excel,vba,formula,Excel,Vba,Formula,我设法让它工作,但问题是我必须指定范围(在本例中,我只是硬编码C2:c25,文件每次都有不同的行计数) 有没有办法只对有数据的行执行此操作 Sub addFormulas() ThisWorkbook.Worksheets("Sheet2").Range("C2").Formula = "=(B2/12)*100" Range("C2").Select Selection.AutoFill Destination:

我设法让它工作,但问题是我必须指定范围(在本例中,我只是硬编码C2:c25,文件每次都有不同的行计数)

有没有办法只对有数据的行执行此操作

Sub addFormulas()

ThisWorkbook.Worksheets("Sheet2").Range("C2").Formula = "=(B2/12)*100"
Range("C2").Select
Selection.AutoFill Destination:=Range("C2:C25")
End Sub

您可以使用xlDown根据B列查找最后一行号,这样下次就不必更改代码了

Sub addFormulas()
    ThisWorkbook.Worksheets("Sheet2").Range("C2").Formula = "=(B2/12)*100"
    Range("C2").Select
    'find last row with value
    last_row = Range("B2").End(xlDown).Row
    Selection.AutoFill Destination:=Range("C2:C" & last_row)
End Sub
但我假设B列的值之间没有空白单元格。如果B列可能有空白单元格,您可以运行FOR循环来查找最后一行。这效率要低得多,但只要您没有太多的行,它就可以正常工作:

Sub addFormulas2()
    ThisWorkbook.Worksheets("Sheet2").Range("C2").Formula = "=(B2/12)*100"
    Range("C2").Select
    For i = 1 To 20
        If Range("B" & i) & "" > "" Then last_row = i
    Next i
    Selection.AutoFill Destination:=Range("C2:C" & last_row)
End Sub
编辑:刚刚在这里了解到,使用xlUp ir比FOR更有效,也比xlDown更可靠,因为如果B列中有一些空白单元格,它不会有任何问题:

Sub addFormulas_()
    ThisWorkbook.Worksheets("Sheet2").Range("C2").Formula = "=(B2/12)*100"
    Range("C2").Select
    With Sheets("Sheet2")
        last_row = .Range("B" & .Rows.Count).End(xlUp).Row
    End With
    Selection.AutoFill Destination:=Range("C2:C" & last_row)
End Sub

无需使用
AutoFill
Select
即可完成任务。最好使用
With…End With
语句。下面的代码中提供了注释

Sub addFormulas()
    With ThisWorkbook.Worksheets("Sheet2") 'used to set the focus on the worksheet object
    
        .Cells(2, 3).Resize(.Cells(.Rows.Count, 2).End(xlUp).Row - 1).Formula = "=(B2/12)*100"
            'writes the formula in the variable range in column C    
            'Breakdown...
                'the "." (dot) e.g. (.Cells and .Rows) is used to refer to the worksheet object in the With statement
                '".Cells(2, 3)" is the start of the range you want to write the formula
                '".Resize(.Cells(.Rows.Count, 2).End(xlUp).Row")expands the range to the last use row in column B
                'the "- 1" adjusts your range because you started on row 2
    End With
End Sub

你的意思是你需要吗?是的,基本上只运行公式直到最后一行(这将是一个可变的行数,即每次都不同)你点击了我评论中的链接了吗?这是要采取的方法。最好使用
xlUp
查找第一条评论中链接中显示的最后一行。也不需要循环。谢谢,我不知道这种方法,我只知道xlDown在有空白单元格的情况下不可靠,因为大多数时候我没有太多行,我只是用来做循环。