首页 文章

在Excel中具有下拉菜单的列

提问于
浏览
0

我是业余VBA用户,需要一些指导......

我在Excel中有两个工作表(1)计费,(2) Contract . Billing Sheet有一个列,其中包含带有结算说明的下拉菜单 . B&C列是开始和停止时间 . D列是 Contract 表中的“成本”,与结算说明相关联 . 根据结算说明,填充D列(Billing Sheet)中的成本 .

我需要一个公式(如果可能)或VBA代码的帮助,如果用户从下拉列表中输入超出 Contract 的内容,“ Contract 描述”将替换初始下拉条目,超额描述将自动下降下到下面的行,并自动填充同一行中的开始和停止时间 . 任何帮助将不胜感激 .

1 回答

  • 0

    像这样的东西(进入工作表代码模块)

    Private Sub Worksheet_Change(ByVal Target As Range)
    
        Const VAL_TRIPLE As String = "Triple"
        Const VAL_DOUBLE As String = "Double"
        Dim rng As Range, cost
    
        On Error GoTo haveError
    
        Set rng = Target.Cells(1)
    
        If rng.Column = 1 And rng.Row > 1 Then
    
            DoEvents 'run the cost lookup...
            cost = rng.EntireRow.Cells(4).Value
    
            With rng
                If .Value = VAL_DOUBLE Or .Value = VAL_TRIPLE Then
                    Application.EnableEvents = False
    
                    .Offset(1, 0).Insert
                    .Offset(1, 0).Value = .Value
                    If IsNumeric(cost) Then
                       .Offset(1, 3).Value = cost * IIf(.Value = VAL_TRIPLE, 2, 1)
                    End If
                    .Value = "replacement" 'how to know what goes here?
                    .Offset(1, 1).Value = .Offset(0, 1).Value
                    .Offset(1, 2).Value = .Offset(0, 2).Value
    
                    Application.EnableEvents = True
                End If
            End With
        End If
    
        Exit Sub
    
    haveError:
        Application.EnableEvents = True
    
    End Sub
    

相关问题