运行 对整列的宏
run macro to an entire column
在相关下拉菜单上处理此宏
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$M" Then
Range("O10").Value = "--select--"
End If
End Sub
我需要为列中的所有单元格 运行 这个宏。它只适用于第一个单元格
有人可以帮帮我吗?
谢谢!
您需要结合使用 Application.Intersect
和 .Offset()
而不是 Target.Address
方法。
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
Dim AffectedRange As Range
Set AffectedRange = Application.Intersect(Target, Me.Range("B:B")) 'find all cells that were changed in column B
If AffectedRange Is Nothing Then Exit Sub 'exit if nothing in column B was changed
Application.EnableEvents = False 'make sure our value change doesn't trigger another Worksheet_Change event (endless loop)
On Error GoTo ENABLE_EVENTS 'make sure events get enabled even if an error occurs
Dim Cell As Range
For Each Cell In AffectedRange.Cells 'loop through all changed cells in column B
Cell.Offset(ColumnOffset:=1).Value = "" 'move from B one column to the right and reset value
Next Cell
ENABLE_EVENTS: 'in case of error enable events and report the error
Application.EnableEvents = True
If Err.Number <> 0 Then
Err.Raise Err.Number, Err.Source, Err.Description, Err.HelpFile, Err.HelpContext
End If
End Sub
这将观察 B 列,并在 B 中的单元格发生更改时删除 C 中的值。
在相关下拉菜单上处理此宏
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$M" Then
Range("O10").Value = "--select--"
End If
End Sub
我需要为列中的所有单元格 运行 这个宏。它只适用于第一个单元格
有人可以帮帮我吗? 谢谢!
您需要结合使用 Application.Intersect
和 .Offset()
而不是 Target.Address
方法。
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
Dim AffectedRange As Range
Set AffectedRange = Application.Intersect(Target, Me.Range("B:B")) 'find all cells that were changed in column B
If AffectedRange Is Nothing Then Exit Sub 'exit if nothing in column B was changed
Application.EnableEvents = False 'make sure our value change doesn't trigger another Worksheet_Change event (endless loop)
On Error GoTo ENABLE_EVENTS 'make sure events get enabled even if an error occurs
Dim Cell As Range
For Each Cell In AffectedRange.Cells 'loop through all changed cells in column B
Cell.Offset(ColumnOffset:=1).Value = "" 'move from B one column to the right and reset value
Next Cell
ENABLE_EVENTS: 'in case of error enable events and report the error
Application.EnableEvents = True
If Err.Number <> 0 Then
Err.Raise Err.Number, Err.Source, Err.Description, Err.HelpFile, Err.HelpContext
End If
End Sub
这将观察 B 列,并在 B 中的单元格发生更改时删除 C 中的值。