工作表更改事件仅在选择区域时有效 - 如何调整为自动更新

时间:2015-03-11 17:01:16

标签: excel excel-vba excel-formula vba

该子模块

中的组合
Sub hithere3()
Dim Rng As Range
Dim Unique As Boolean

For Each Rng In Worksheets("Sheet8").Range("FS3:FS30") 'for each cell     in     your B1 to B30 range, sheet1
Unique = True 'we'll assume it's unique
Lastunique = Worksheets("TRADES").Range("C:C").Find("*",     SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
For i = 3 To Lastunique 'for each cell in the unique ID cache
    If Rng.Value = Worksheets("TRADES").Cells(i, 3).Value Then 'we       check    if it is equal
        Unique = False 'if yes, it is not unique
    End If
Next
If Unique Then Worksheets("TRADES").Cells(Lastunique + 1, 3) = Rng 'adds    if it is unique
Next
End Sub

使用工作表更改事件中的循环检查

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Intersect(Target, Range("FS3:FS33")) Is Nothing Then
'Do nothing '
Else
Call hithere3
End If
End Sub

除了只在我选择FS3中的一个单元格时才更新:FS33

有谁能建议如何克服这个问题?

可能有下面的工作变更范围选择类型?

Private Sub Worksheet_Change(ByVal Target As Range)
Dim Rng As Range, Dn As Range, n As Long
Dim RngB As Range, RngC As Range
If Target.Column = 2 And Target.Count = 1 And Target.Row > 1 Then
With CreateObject("scripting.dictionary")
.CompareMode = vbTextCompare
Set RngB = Range(Range("B2"), Range("B" & Rows.Count).End(xlUp))
Set RngC = Range(Range("C2"), Range("C" & Rows.Count).End(xlUp))
ray = Array(RngB, RngC)
For n = 0 To 1
    For Each Dn In ray(n)
        If Not Dn.Address(0, 0) = "C1" And Not Dn.Value = "" Then
            .Item(Dn.Value) = Empty
        End If
    Next Dn
Next n
Range("C2").Resize(.Count) = Application.Transpose(.Keys)
End With
End If

4 个答案:

答案 0 :(得分:1)

使用工作表计算事件或工作表更改事件:

  1. 如果范围包含公式
  2. ,请使用计算
  3. 如果范围中的单元格已更改,则更改

答案 1 :(得分:0)

If Intersect(Target, Range("FS3:FS33")) Is Nothing是罪魁祸首。您必须将Range("FS3:FS33")更改为您希望影响此更改的范围。

答案 2 :(得分:0)

最后想通了,下面的代码可以工作:

Private Sub Worksheet_calculate()
If Range("FS3:FS33") Is Nothing Then
'Do nothing'
Else
Call hithere3
End If
End Sub

答案 3 :(得分:0)

Private Sub Worksheet_Change(ByVal Target As Range) '<<delete the "Selection" from the name of event
If Intersect(Target, Range("FS3:FS33")) Is Nothing Then
'Do nothing '
Else
Call hithere3
End If
End Sub