我在excel中写了一个Case结构,我想覆盖一些行

时间:2019-02-18 19:12:03

标签: excel vba

在excel中,我编写了一个案例结构(3,4,5,6),该结构填充了适当的列。我从没想到我需要能够覆盖某些行的大小写。现在,我需要能够覆盖整行(第3、4、5、6列)。我对此并不很精通,因此任何帮助甚至指导都将是有用的。

我设计这是为了计算4个人之间的房地产中物品的价值。不管谁拿到了东西,仍然欠其他三个人25%的钱。运行良好。但是现在有一些物体在身体上平均分配给所有四个人。我尝试覆盖这些数字,但仍会继续关注这些情况。

On Error GoTo ErrorHandler '<--| be sure to catch any error and enable events handling back
    Dim RowNumber As Integer

    Select Case Target.Column

        Case "3"        
            RowNumber = Target.Cells.Row

            Me.Cells(RowNumber, 4).Value = Me.Cells(RowNumber, 2).Value * 0.25
            Me.Cells(RowNumber, 5).Value = Me.Cells(RowNumber, 2).Value * 0.25
            Me.Cells(RowNumber, 6).Value = Me.Cells(RowNumber, 2).Value * 0.25

            Me.Cells(RowNumber, 3).Value = -(Me.Cells(RowNumber, 2).Value * 0.75)
            Me.Cells(RowNumber, 3).Font.Color = vbRed

        Case "4"

            RowNumber = Target.Cells.Row        

            Me.Cells(RowNumber, 3).Value = Me.Cells(RowNumber, 2).Value * 0.25
            Me.Cells(RowNumber, 5).Value = Me.Cells(RowNumber, 2).Value * 0.25
            Me.Cells(RowNumber, 6).Value = Me.Cells(RowNumber, 2).Value * 0.25
            Me.Cells(RowNumber, 4).Value = -(Me.Cells(RowNumber, 2).Value * 0.75)
           Me.Cells(RowNumber, 4).Font.Color = vbRed

        Case "5"

            RowNumber = Target.Cells.Row           
            Me.Cells(RowNumber, 3).Value = Me.Cells(RowNumber, 2).Value * 0.25
            Me.Cells(RowNumber, 4).Value = Me.Cells(RowNumber, 2).Value * 0.25
            Me.Cells(RowNumber, 6).Value = Me.Cells(RowNumber, 2).Value * 0.25
            Me.Cells(RowNumber, 5).Value = -(Me.Cells(RowNumber, 2).Value * 0.75)
           Me.Cells(RowNumber, 5).Font.Color = vbRed
          ' MsgBox "test of recalculate " & Me.Cells(RowNumber, 3).Value

            ... Etc.

1 个答案:

答案 0 :(得分:0)

您可以利用另一列中的标志来标记要手动覆盖的行,然后将Select-Case(我假设是在Worksheet_Change事件中)包含在{{1 }}声明。

If

编辑:

我假设您已经有一个错误处理程序,可以根据您在代码代码中的注释重新打开Private Sub Worksheet_Change(ByVal Target As Range) 'Application.EnableEvents = False On Error GoTo ErrorHandler '<--| be sure to catch any error and enable events handling back Dim RowNumber As Long RowNumber = Target.Row If Me.Cells(RowNumber, 7) <> "O" Then '<-make column 7 your helper column where you can use letter "O" to flag rows for Override Select Case Target.Column Case "3" Me.Cells(RowNumber, 4).Value = Me.Cells(RowNumber, 2).Value * 0.25 Me.Cells(RowNumber, 5).Value = Me.Cells(RowNumber, 2).Value * 0.25 Me.Cells(RowNumber, 6).Value = Me.Cells(RowNumber, 2).Value * 0.25 Me.Cells(RowNumber, 3).Value = -(Me.Cells(RowNumber, 2).Value * 0.75) Me.Cells(RowNumber, 3).Font.Color = vbRed Case "4" Me.Cells(RowNumber, 3).Value = Me.Cells(RowNumber, 2).Value * 0.25 Me.Cells(RowNumber, 5).Value = Me.Cells(RowNumber, 2).Value * 0.25 Me.Cells(RowNumber, 6).Value = Me.Cells(RowNumber, 2).Value * 0.25 Me.Cells(RowNumber, 4).Value = -(Me.Cells(RowNumber, 2).Value * 0.75) Me.Cells(RowNumber, 4).Font.Color = vbRed Case "5" Me.Cells(RowNumber, 3).Value = Me.Cells(RowNumber, 2).Value * 0.25 Me.Cells(RowNumber, 4).Value = Me.Cells(RowNumber, 2).Value * 0.25 Me.Cells(RowNumber, 6).Value = Me.Cells(RowNumber, 2).Value * 0.25 Me.Cells(RowNumber, 5).Value = -(Me.Cells(RowNumber, 2).Value * 0.75) Me.Cells(RowNumber, 5).Font.Color = vbRed 'etc...for subsequent cases End Select End If 'Application.EnableEvents = True 'Exit Sub 'ErrorHandler: 'Error Handling to turn back on Application.EnableEvents End Sub ,因此在我的示例中,我只是将它们添加为伪代码注释。