【问题标题】:Excel: Macro affects multiple columns in table when I only want it to affect a specific oneExcel:当我只希望宏影响特定列时,宏会影响表中的多个列
【发布时间】:2018-08-07 14:26:20
【问题描述】:

我在 Excel 中创建了一个包含两列(C 和 H)的表格,其中包含一个下拉列表,每列引用一个不同的列表(见下图)。我找到了一个宏,它允许您从列表中选择多个名称,并在添加它们时用逗号分隔它们。这对于我希望它适用的列(C 列:“责任”)非常有用。但是,我意识到代码也适用于 H 列:“状态”,这不是我想要的。我以为我按照宏的编写方式将它限制在 C 列。你能帮我找到一种方法来解决它,使它只适用于“责任”列吗?该表还连接到图表。我想确保宏会在表收缩/扩展时动态应用于表,因为可以添加或删除任务。代码已经这样做了,但是我愿意接受建议以使其更高效/整洁。我在下面提供了宏:

Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)
Dim cht As Chart
Dim varValues As Variant
Dim rngDV As Range
Dim oldVal As String
Dim newVal As String
Dim strVal As String
Dim i As Long
Dim lCount As Long
Dim Ar As Variant
On Error Resume Next
Dim lType As Long

Set cht = Worksheets("Sheet1").ChartObjects("Chart 5").Chart
varValues = cht.SeriesCollection(2).XValues

If Target.Count > 1 Then GoTo exitHandler

lType = Target.Validation.Type
If lType = 3 Then
    Application.EnableEvents = False
    newVal = Target.Value
    Application.Undo
    oldVal = Target.Value
    Target.Value = newVal
    If Target.Column = 3 And Target.Row > cht.SeriesCollection(2).Points(LBound(varValues) + 1) And Target.Row < cht.SeriesCollection(2).Points(UBound(varValues) + 1) Then
        If oldVal = "" Then
        Else
            If newVal = "" Then
            Else
                On Error Resume Next
                Ar = Split(oldVal, ", ")
                strVal = ""
                For i = LBound(Ar) To UBound(Ar)
                    Debug.Print strVal
                    Debug.Print CStr(Ar(i))
                    If newVal = CStr(Ar(i)) Then
                        strVal = strVal
                        lCount = 1
                    Else
                        strVal = strVal & CStr(Ar(i)) & ", "
                    End If
                Next i
                If lCount > 0 Then
                    Target.Value = Left(strVal, Len(strVal) - 2)
                Else
                    Target.Value = strVal & newVal
                End If
            End If
        End If
    End If
End If

exitHandler:
  Application.EnableEvents = True
End Sub

这是一张桌子的样子。我在 H 列中突出显示了我遇到的问题。

【问题讨论】:

  • Target 的范围是如何定义的?
  • @cybernetic.nomad 在Worksheet_Change() 事件中。此外,Target.Count &gt; 1 是顶部的一个条件,用于在多个单元格被更改触发事件时结束例程。
  • 我在这里看不到任何会导致这种情况的东西。 If Target.Column = 3 很清楚。我无法想象允许H 通过的场景。你能在IF 语句上设置一个断点,看看H 受到影响时target.column 的值是多少?
  • @JNevill 列值恢复为 8(如预期的那样),所以我仍然不知道它是如何通过的。
  • 有什么方法可以查看已删除的答案吗?我想弄清楚最近删除了他们答案的用户是谁,因为虽然它没有用,但他们引导我朝着正确的方向前进,我想归功于他们

标签: excel vba list dropdown


【解决方案1】:

我不确定为什么这会有所不同,但是通过向上移动 If Target.Column = 3 语句重新排列它可以使代码按预期工作。我用 ----->

Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)
Dim cht As Chart
Dim varValues As Variant
Dim rngDV As Range
Dim oldVal As String
Dim newVal As String
Dim strVal As String
Dim i As Long
Dim lCount As Long
Dim Ar As Variant
On Error Resume Next
Dim lType As Long

If Target.Count > 1 Then GoTo exitHandler

   ---->If Target.Column = 3 Then<----
        Set cht = Worksheets("Sheet1").ChartObjects("Chart 5").Chart
        varValues = cht.SeriesCollection(2).XValues
        lType = Target.Validation.Type
        If lType = 3 Then
            Application.EnableEvents = False
            newVal = Target.Value
            Application.Undo
            oldVal = Target.Value
            Target.Value = newVal
            If Target.Row > cht.SeriesCollection(2).Points(LBound(varValues) + 1) And Target.Row < cht.SeriesCollection(2).Points(UBound(varValues) + 1) Then
                If oldVal = "" Then
                Else
                    If newVal = "" Then
                    Else
                        On Error Resume Next
                        Ar = Split(oldVal, ", ")
                        strVal = ""
                        For i = LBound(Ar) To UBound(Ar)
                            Debug.Print strVal
                            Debug.Print CStr(Ar(i))
                            If newVal = CStr(Ar(i)) Then
                                strVal = strVal
                                lCount = 1
                            Else
                                strVal = strVal & CStr(Ar(i)) & ", "
                            End If
                        Next i
                        If lCount > 0 Then
                            Target.Value = Left(strVal, Len(strVal) - 2)
                        Else
                            Target.Value = strVal & newVal
                        End If
                    End If
                End If
            End If
        End If
    End If

exitHandler:
  Application.EnableEvents = True
End Sub

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-06
    • 1970-01-01
    • 2020-06-17
    • 1970-01-01
    相关资源
    最近更新 更多