【问题标题】:Limit Worksheet Event Macro to Certain Columns将工作表事件宏限制到某些列
【发布时间】:2022-12-24 13:19:18
【问题描述】:

以下代码将根据双击单元格将单元格更新为 1-5。

我希望将其限制在电子表格中的几列(例如,如果我双击 A2,则不会发生任何事情)。

显然 .Columns("B:C") 不在正确的位置。

Private Sub Worksheet.Columns("B:C")_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
Cancel = True

If Target.Value < 5 Then
    Target.Value = Target.Value + 1

Else
    Target.Value = 5

End If

End Sub

【问题讨论】:

  • 您使用 if 块来测试目标是否在范围内:If Not Intersect(Range("B:C"),Target) is Nothing then

标签: excel vba


【解决方案1】:

正如 Scott Craner 在 cmets 中提到的那样,处理此问题的正确方法是测试目标范围是否与列相交。

我还建议设置Cancel = True。这可以防止单元格进入编辑模式。

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
    If Not Intersect(Columns("B:C"), Target) Is Nothing Then
        If Target.Value < 5 Then
            Target.Value = Target.Value + 1
        Else
            Target.Value = 5
        End If
        Cancel = True
    End If
End Sub

【讨论】:

    【解决方案2】:

    双击前的工作表:增加单元格值

    • 调整事件过程中的值。
    Option Explicit
    
    Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
        ' Reference (set) the range (columns).
        Dim srg As Range: Set srg = SetColumnsUR(Me, "B2:C2,E2,G2:H2")
        ' Check if the double-clicked cell ('Target') doesn't intersect.
        If Intersect(srg, Target) Is Nothing Then Exit Sub
        ' Write the integer according to the logic.
        Target.Value = MyIntegerLogic(Target.Value, 1, 5)
        ' Suppress the default behavior of double-clicking a cell.
        Cancel = True
    End Sub
    
    Function SetColumnsUR( _
        ByVal ws As Worksheet, _
        ByVal FirstRowAddress As String) _
    As Range
        With ws.Range(FirstRowAddress)
            With .Areas(1).Resize(ws.Rows.Count - .Row + 1)
                Set SetColumnsUR = Intersect(.Cells, ws.UsedRange)
            End With
            If .Areas.Count = 1 Then Exit Function
            Set SetColumnsUR = Intersect(SetColumnsUR.EntireRow, .EntireColumn)
        End With
    End Function
    
    Function MyIntegerLogic( _
        ByVal Value As Variant, _
        ByVal MinInteger As Long, _
        ByVal MaxInteger As Long) _
    As Long
        Dim Number As Long: Number = MinInteger - 1
        If VarType(Value) = vbDouble Then ' is a number
            If Int(Value) = Value Then ' is a whole number (integer)
                Select Case Value
                    Case MinInteger To MaxInteger - 1: Number = Value + 1 ' 1.)
                    Case MaxInteger: Number = MaxInteger ' 2.)
                    Case Else ' covered below
                End Select
            End If
        End If
        If Number = MinInteger - 1 Then Number = MinInteger ' 3.) all other cases
        MyIntegerLogic = Number
    End Function
    

    【讨论】:

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