[英]Excel VBA loop macro for each selected cell
我有一個宏,它只是更改所選行中單元格的值。
Cells(Application.ActiveCell.Row, 20).Select
ActiveCell.Value = "withdraw"
但是,我希望能夠選擇一定數量的行(不一定是連續的),例如從下面的示例中選擇A1,A3和A4,並為每個選擇的單元格設置宏更改列B,因此,
A B
1 Brian
2 James
3 Jenny
4 Frank
5 Tim
變
A B
1 Brian Withdraw
2 James
3 Jenny Withdraw
4 Frank Withdraw
5 Tim
我將如何從選定的范圍中獲取活動行並為每行循環宏?
這是一個可能的解決方案:
Private Sub a()
Dim sSel As String
Dim aSel As Variant
Dim rX As Range
Dim i As Integer
Dim j As Integer
sSel = Selection.Address
aSel = Split(sSel, ",")
For i = 0 To UBound(aSel)
Set rX = Application.ActiveCell.Parent.Range(CStr(aSel(i)))
For j = 1 To rX.Rows.Count
rX.Rows(j).EntireRow.Cells(1, 2) = "withdraw"
Next
Next
End Sub
請注意,這不允許您在任何行中的任意位置選擇任意數量的單元格。
這個簡單的代碼應該為您工作:
Sub Button1_Click()
Dim r As Range
Set r = Selection
r.Offset(, 1) = "Withdraw"
End Sub
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.