[英]Excel VBA fill row after row according to the value of another column
我想制作一個以下列方式修改表格的宏:每次手動填充 A 列時,值“1”,我希望 A 列中的所有行在列中立即取值“1” B 包含與第一個填充單元格相同的值。 如果我將 1 放入單元格 A4,我希望 B 包含“Ananas”的任何地方都放入 1,因此在 A2、A3、A10、A11 中......然后如果在 A7 中手動輸入 1(b7 =“Kiwi”) ,我希望將 1 放入 A15、A16 等...我有 5000 行,B 中的值也為空。這里是 Excel 表的圖像
我相信這就是你想要的。 這是一個工作表事件,因此它必須放在工作表編碼模塊中。
Private Sub Worksheet_Change(ByVal Target As Range)
Application.EnableEvents = False
Application.ScreenUpdating = False
If Target.Column = 1 Then
Dim newVal As Variant: newVal = Target.Value
Dim match As String: match = Target.Offset(0, 1).Value
Dim ws As Worksheet: Set ws = ActiveSheet
Dim lastRow As Long: lastRow = ws.Cells(ws.Rows.Count, 2).End(xlUp).Row
Dim r As Long
For r = 2 To lastRow
If ws.Cells(r, 2).Value = match Then
ws.Cells(r, 1).Value = newVal
End If
Next r
End If
Application.EnableEvents = True
Application.ScreenUpdating = True
End Sub
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.