当前位置:网站首页>Excel VBA: realize automatic drop-down filling formula to the last line

Excel VBA: realize automatic drop-down filling formula to the last line

2022-07-26 05:03:00 Avasla

Problem scenario :

As shown below , Need basis B Column “ project ” Content , lookup G-H In the column , Whether each item belongs to expenditure or income , Then match and fill D In the column .

PS: Here through if() Function to identify the amount of positive and negative output expenditure / income . This method is not considered in this paper , Mainly through the case study VBA Implementation method .
 Insert picture description here

Xlookup + Auto fill formula

Ideas : First , stay D2 The cells are filled with the corresponding Xlookup The formula (vlookup Can also be , But the upgraded version is more recommended Xlookup); Through VBA Realize the function of automatically filling formulas from the drop-down .
 Insert picture description here
Be careful : VBA Auto fill needs to specify the end position , So we need to find “C” Column (AB Columns can also be ) Last line , Use Cells(Rows.Count, "C").End(xlUp).Row

Sub autofill()
    Application.ScreenUpdating = False ' Prevent screen jitter when macros are running 
    Sheets("Sheet1").Select ' choice Sheet1
    Range("D2").Select ' choice D2  First cell with formula 
     ' Run the auto fill function on the selected cells , Range is from D2 To D Last row of column ,( The reference here is C Last row position of column )
    Selection.autofill Destination:=Sheets("Sheet1").Range("D2:D" & Sheets("Sheet1").Cells(Rows.Count, "C").End(xlUp).Row)
End Sub

Results after operation :

D The columns are automatically filled with the corresponding Xlookup The formula
 Insert picture description here

原网站

版权声明
本文为[Avasla]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/207/202207260457266846.html