如果交集包含字符串,则添加多选列表框

我之前已经实现了基于选择在我的工作表中插入多选 ListBox 的代码。

我想添加更多条件以添加ListBox如果在同一行中选择Column 7并且具有字符串Column 2 "variable"

伪代码:

If activeCell.Column = 7 and if activeCell intersection with Column 2 contains the string "variable" then

Add multi-select ListBox below active cell and 

Output selections to activeCell.

这是我目前使用的代码的一部分。它将 ListBox 添加到指定列中的每一行,而不是添加到指定列中的限定行。

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If Target.Cells.Count > 1 Then Exit Sub
    If Not Intersect(Target,ColourArea(activeSheet)) Is Nothing Then
        CreateColourPopUp Target
    Else
        DeleteAllPopUps Target
    End If
End Sub

如何添加上面提到的额外条件?

yaoji2011 回答:如果交集包含字符串,则添加多选列表框

Application.Intersect 方法返回一个 Range 对象,该对象表示两个或多个范围的矩形交集。

我不会使用这个函数来接近你想做的事情。

相反,我会将 Worksheet_SelectionChange 事件的 Target 参数与我的所有 If...Then 逻辑一起使用。

类似这样:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If Target.Cells.Count > 1 Then Exit Sub 'Makes sure no more than 1 cell is selected.
    If Not Intersect(Target,ColourArea(ActiveSheet)) Is Nothing Then 'Not sure if this is required or not but remove this If...Then block if it's not needed.
        If Target.Column = 7 And Target.Offset(0,-5).Value = "Variable" Then
            CreateColourPopUp Target
        End If
    Else 
        DeleteAllPopUps Target
End Sub

这会添加额外的 If...Then 块以在创建列表框之前检查以下条件是否为 True;

  • Target.Column = 7 检查所选单元格在第 7 列中。
  • Target.Offset(0,-5).Value = "Variable" 检查所选单元格左侧 5 列单元格的值为 "Variable"

有关 Offset 属性的语法,请参阅 documentation here

本文链接:https://www.f2er.com/372190.html

大家都在问