Excel 对整个列运行宏

Excel 对整个列运行宏,excel,vba,drop-down-menu,Excel,Vba,Drop Down Menu,在从属下拉菜单上处理此宏 Private Sub Worksheet_Change(ByVal Target As Range) If Target.Address = "$M$10" Then Range("O10").Value = "--select--" End If End Sub 我需要为列中的所有单元格运行此宏。它只在第一个牢房工作 有人能帮我吗? 谢谢 您需要将Application.Intersect与.Offset()结合使用,而不是使用Ta

在从属下拉菜单上处理此宏

Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Address = "$M$10" 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
选项显式
私有子工作表_更改(ByVal目标作为范围)
变暗影响范围作为范围
Set AffectedRange=Application.Intersect(Target,Me.Range(“B:B”))查找列B中更改的所有单元格
如果AffectedRange为Nothing,则退出子项“如果B列中没有任何更改,则退出”
Application.EnableEvents=False“确保我们的值更改不会触发另一个工作表更改事件(无止境循环)
On Error GoTo ENABLE_EVENTS'确保即使发生错误,事件也已启用
暗淡单元格作为范围
对于AffectedRange中的每个单元格。单元格的循环通过B列中所有更改的单元格
Cell.Offset(columnpoffset:=1).Value=“”从B向右移一列并重置值
下一个细胞
ENABLE_EVENTS:'如果发生错误,请启用事件并报告错误
Application.EnableEvents=True
如果错误号为0,则
Err.Raise Err.Number、Err.Source、Err.Description、Err.HelpFile、Err.HelpContext
如果结束
端接头

这将观察B列,并在更改B中的单元格时删除C中的值。

您需要使用
应用程序。Intersect
.Offset()
结合使用,而不是
目标.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
选项显式
私有子工作表_更改(ByVal目标作为范围)
变暗影响范围作为范围
Set AffectedRange=Application.Intersect(Target,Me.Range(“B:B”))查找列B中更改的所有单元格
如果AffectedRange为Nothing,则退出子项“如果B列中没有任何更改,则退出”
Application.EnableEvents=False“确保我们的值更改不会触发另一个工作表更改事件(无止境循环)
On Error GoTo ENABLE_EVENTS'确保即使发生错误,事件也已启用
暗淡单元格作为范围
对于AffectedRange中的每个单元格。单元格的循环通过B列中所有更改的单元格
Cell.Offset(columnpoffset:=1).Value=“”从B向右移一列并重置值
下一个细胞
ENABLE_EVENTS:'如果发生错误,请启用事件并报告错误
Application.EnableEvents=True
如果错误号为0,则
Err.Raise Err.Number、Err.Source、Err.Description、Err.HelpFile、Err.HelpContext
如果结束
端接头

这将观察B列,并在更改B中的单元格时删除C中的值。

由于
Target.Address=“$M$10”
此代码仅在单元格M10上运行,无法在第一个单元格上运行。请注意,有几个关于dpendent下拉框的好教程。你查过了吗?依赖下拉菜单很好用。。。宏的目的是在我对B列中的过滤器进行任何更改时重置C列中的过滤器。我现在使用的是目标。Column=B。在B列中修改的任何单元格都会触发C列中的更改,但仅针对第一个单元格。我如何确保B1中的更改会重置C1上的过滤器、C2上的B2、C3上的B3等?thxdue to
Target.Address=“$M$10”
此代码仅在单元格M10上运行,无法在第一个单元格上运行。请注意,有几个关于dpendent下拉框的好教程。你查过了吗?依赖下拉菜单很好用。。。宏的目的是在我对B列中的过滤器进行任何更改时重置C列中的过滤器。我现在使用的是目标。Column=B。在B列中修改的任何单元格都会触发C列中的更改,但仅针对第一个单元格。我如何确保B1中的更改会重置C1上的过滤器、C2上的B2、C3上的B3等?谢谢