![](/img/trans.png)
[英]How can I adjust the row height of merged cells using a macro in vba excel?
[英]How can I make excel macro run on cells in active row?
我在嘗試制作宏時遇到問題。 我有一個電子表格,我關注 A、Q、R、S 和 T 列。首先,我選擇 Q 列中的一行作為宏的初始活動單元格。 我將 Q 列設為 0。然后我在 A 列中放置了一個求和公式,對我所在的行求和 R、S 和 T。然后我將值復制並粘貼到 A 列單元格上,這樣總和就是一個值. 我讓 T 列 = A 列。然后我在 R 和 S 列中放了 0。
下面是使用記錄宏制作的初始宏,但它僅適用於我記錄宏的行。 我希望宏是這樣的,當我在任何行上執行 Ctrl-g 時,它將為該行運行。
Sub Cashflow()
'
' Keyboard Shortcut: Ctrl+g
'
ActiveCell.FormulaR1C1 = "0"
Range("A74").Select
ActiveCell.FormulaR1C1 = "=SUM(RC[17]:RC[19])"
Range("A74").Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("A74").Select
Application.CutCopyMode = False
Range("T74").Select
ActiveCell.FormulaR1C1 = "=RC[-19]"
Range("R74").Select
ActiveCell.FormulaR1C1 = "0"
Range("S74").Select
ActiveCell.FormulaR1C1 = "0"
Range("T74").Select
End Sub
這是我嘗試修復它所做的,但它不起作用:
Sub Cashflow()
'
' Keyboard Shortcut: Ctrl+g
'
ActiveCell.FormulaR1C1 = "0"
Range(Cells(Selection.Row, 1)).Select
ActiveCell.FormulaR1C1 = "=SUM(RC[17]:RC[19])"
Range(Cells(Selection.Row, 1)).Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range(Cells(Selection.Row, 1)).Select
Application.CutCopyMode = False
Range(Cells(Selection.Row, 20)).Select
ActiveCell.FormulaR1C1 = "=RC[-19]"
Range(Cells(Selection.Row, 18)).Select
ActiveCell.FormulaR1C1 = "0"
Range(Cells(Selection.Row, 19))
ActiveCell.FormulaR1C1 = "0"
Range(Cells(Selection.Row, 20)).Select
End Sub
有任何想法嗎?
避免.Select
和.Activate
。 它增加了動作並減慢了代碼。 只需對范圍做您想做的事情。
無需使用剪貼板,只需將值直接分配給單元格即可。
With
塊有助於減少重復打字的數量。
現在養成向每個范圍對象聲明所有父表的習慣。
用這個:
Sub Cashflow()
Dim rng As Range
Set rng = Selection
If rng.Count > 1 Then Exit Sub
rng = 0
With rng.Parent
With Cells(rng.Row, 1)
.FormulaR1C1 = "=SUM(RC[17]:RC[19])"
.Value = .Value
End With
.Cells(rng.Row, 1).FormulaR1C1 = "=RC[-19]"
.Range(.Cells(rng.Row, 18), .Cells(rng.Row, 19)).Value = 0
End With
End Sub
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.