0

我要解决的问题:我想查看一个自动填充截止日期的列。我的组织希望提前两个工作日 (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

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

任何帮助都会很棒!

4

3 回答 3

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

等等

于 2013-05-22T19:41:14.483 回答
1

我认为最后一行应该反过来。尝试

Range("E3").Value=In_HouseDue

于 2013-05-22T19:32:28.763 回答
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
于 2013-05-22T19:46:14.003 回答