需要Excel VBA指导

时间:2013-05-22 19:23:46

标签: vba excel-vba excel

我希望解决的问题: 我想查看一个自动填充截止日期的列。我的组织希望在两个工作日(M-Th)之前执行内部截止日期。要根据实际截止日期的一周中的一天自动填充内部截止日期的内部截止日期,我已写下此子目录:

Sub In_House()

Dim In_HouseDue
Dim CurrentCell

CurrentCell = Range("D3").Value

Select Case CurrentCell

Case Weekday(CurrentCell) = 1
In_HouseDue.Value = DateAdd("d", 5, CurrentCell)
     ActiveCell.Offset(1, 0).Select

Case Weekday(CurrentCell) = 2
   In_HouseDue.Value = DateAdd("d", 5, CurrentCell)
 ActiveCell.Offset(1, 0).Select

Case Weekday(CurrentCell) = 3
In_HouseDue.Value = DateAdd("d", 2, CurrentCell)
 ActiveCell.Offset(1, 0).Select
Case Weekday(CurrentCell) = 4
In_HouseDue.Value = DateAdd("d", 2, CurrentCell)
 ActiveCell.Offset(1, 0).Select
Case Weekday(CurrentCell) = 5
In_HouseDue.Value = DateAdd("d", 2, CurrentCell)
 ActiveCell.Offset(1, 0).Select
Case Weekday(CurrentCell) = 6
In_HouseDue.Value = DateAdd("d", 3, CurrentCell)
 ActiveCell.Offset(1, 0).Select
Case Weekday(CurrentCell) = 7
In_HouseDue.Value = DateAdd("d", 4, CurrentCell)
 ActiveCell.Offset(1, 0).Select

In_HouseDue = Range("E3").Value

End Select

End Sub

它运行没有任何错误,但实际上并没有填充以Cell“E3”开头的内部截止日期列

任何帮助都会很棒!

3 个答案:

答案 0 :(得分:2)

除了PowerUser所说的内容之外,重做Select Case块,如下所示:

Select Case Weekday(CurrentCell)

Case 1
In_HouseDue = DateAdd("d", 5, CurrentCell)
     ActiveCell.Offset(1, 0).Select

Case 2
   In_HouseDue = DateAdd("d", 5, CurrentCell)
 ActiveCell.Offset(1, 0).Select

答案 1 :(得分:1)

我认为最后一行应该是另一种方式。尝试

Range("E3").Value=In_HouseDue

答案 2 :(得分:0)

这会奏效。您的Select Case语句错误,您的变量也未正确声明,这通常会导致错误或其他不良后果。

Sub In_House()

Dim In_HouseDue As Date
Dim CurrentCell As Date

CurrentCell = Range("D3").Value

    Select Case Weekday(CurrentCell)

        Case 1
            In_HouseDue = DateAdd("d", 5, CurrentCell)
             ActiveCell.Offset(1, 0).Select

        Case 2
           In_HouseDue = DateAdd("d", 5, CurrentCell)
            ActiveCell.Offset(1, 0).Select

        Case 3
            In_HouseDue = DateAdd("d", 2, CurrentCell)
             ActiveCell.Offset(1, 0).Select
        Case 4
            In_HouseDue = DateAdd("d", 2, CurrentCell)
             ActiveCell.Offset(1, 0).Select
        Case 5
            In_HouseDue = DateAdd("d", 2, CurrentCell)
            ActiveCell.Offset(1, 0).Select
        Case 6
            In_HouseDue = DateAdd("d", 3, CurrentCell)
             ActiveCell.Offset(1, 0).Select
        Case 7
            In_HouseDue = DateAdd("d", 4, CurrentCell)
             ActiveCell.Offset(1, 0).Select

    End Select

    Range("E3").Value = In_HouseDue

End Sub