开发者

Is there a way to automatically fill cells in a row when it is added in Excel

I don't really know much about excel especially VBA. But i need to write something(macro for ex.) that does the following:

if someone inserts a row and fills the first 4 cells of it, it needs to sort the rows (i have a macro for sorting called "sortingmacro") and then it needs to fill cells 7 and 8 by copying the cells above them.

for example if i insert a row to 4th line and fill A4,B4,C4 and D4 it needs to copy G3 to G4 and H3 to H4. (after sorting of course)

any help is appreciated.

edit: i hav开发者_开发百科e formulas in the cells that are to be copied. the formulas for the first row(the top one according to sorting) is a different one and all the others are the same.


You can find out if a cell has been changed with the Worksheet_Change-Event.

Private Sub Worksheet_Change(ByVal Target As Range)
    Debug.Print Target.Address
End Sub

Output (something like this):

$A$14
$B$17
$C$13
$C$21
$C$16

So if a matching cell is changed, look for the other 3 in the row and start your macro.


So put a button on the page, then go to the code for it (the click event). In that, use your sortingmacro to sort all the rows. Then you just do something simple like:

Rows(4).Cells(1,7).Formula = Rows(3).Cells(1,7).Formula
Rows(4).Cells(1,8).Formula = Rows(3).Cells(1,8).Formula

Of course instead of hardcoding 4 in there, you'd have a variable for the row (make sure and determine it after sorting).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜