0

我相当精通 Excel VBA(有很多在线帮助!!)......但我偶然发现了一个让我完全难过的问题。

我正在为项目规划创建甘特图。我的问题是我希望让用户指定项目开始日期和项目结束日期,然后让电子表格填充从开始日期到结束日期的列。

见基本布局: 在此处输入图像描述

我已经用 InputBoxes 触底了用户输入

Sub Set_Project_Start_Date()

' Written 2nd August 13
' P.J. Callaghan
'

ActiveSheet.Select
Dim projStartDate

showInputBox_Start:
projStartDate = Application.InputBox("Please enter Project Start Date" & Chr(10) & "Must be a Monday" & Chr(10) & "Format is: dd/mm/yyyy")

' Set Message Box such that clicking cancel ends the sub-routine for projStartdate variable
If projStartDate = False Then
MsgBox "You clicked the Cancel button, Input Box will close.", 64, "Cancel was clicked."
Exit Sub
ElseIf projStartDate = "" Then
MsgBox "You must click Cancel to exit.", 48, "You clicked Ok but entered nothing."
GoTo showInputBox_Start
Else
MsgBox "You entered " & projStartDate & ".", 64, "Please click OK to resume."
Range("c6").Select
With Selection
.Value = projStartDate
.NumberFormat = "dd-mmm-yy"
End With
Range("e10").Select
With Selection
.Value = projStartDate
.NumberFormat = "dd-mmm-yy"
.Orientation = 90
End With

End If

End Sub

Sub Set_Project_End_Date()

' Written 2nd August 13
' P.J. Callaghan
'

ActiveSheet.Select
Dim projEndDate

showInputBox_End:
projEndDate = Application.InputBox("Please enter Project End Date" & Chr(10) & "Must be a Monday" & Chr(10) & "Format is: dd/mm/yyyy")

' Set Message Box such that clicking cancel ends the sub-routine for projStartdate variable
If projEndDate = False Then
MsgBox "You clicked the Cancel button, Input Box will close.", 64, "Cancel was clicked."
Exit Sub
ElseIf projEndDate = "" Then
MsgBox "You must click Cancel to exit.", 48, "You clicked Ok but entered nothing."
GoTo showInputBox_End
Else
MsgBox "You entered " & projEndDate & ".", 64, "Please click OK to resume."
Range("c7").Select
With Selection
.Value = projEndDate
.NumberFormat = "dd-mmm-yy"
End With

End If

End Sub

我坚持的一点是编写代码以仅从开始日期填充到结束日期。我敢肯定这一定是某种循环安排……但我还没有弄清楚。

我想知道你们中是否有人可以提出解决方案?

提前致谢,

保罗

4

2 回答 2

1

如果您的目标是使用从项目开始日期到结束日期的每日日期填充以单元格 E10 开头的行,那么您可以使用自动填充:

With Worksheets("Gantt")
    Set sourceRange = .Range("E10")
    sourceRange.Value = projStartDate
    Set fillRange = .Range(sourceRange, Cells(sourceRange.Row, _
        sourceRange.Column + projEndDate - projStartDate + 1))
    sourceRange.AutoFill Destination:=fillRange, Type:=xlFillDays
End With
于 2013-08-04T15:35:32.963 回答
0

呵呵,钱就对了。

我已经获取了您的代码并对其进行了一些修改……由于某种原因,它没有作为复制和粘贴执行

Sub Date_Fill()

' http://stackoverflow.com/questions/18043084/excel-populate-columns-with-dates-based-on-user-input

Dim projEndDate As Date
Dim projStartDate As Date
Dim projDuration As Integer

projStartDate = Range("c6").Value
projEndDate = Range("c7").Value

With Worksheets("Gantt")
    Set SourceRange = .Range("E10")
    SourceRange.Value = projStartDate
    Set fillRange = .Range(SourceRange, Cells(SourceRange.Row, _
        SourceRange.Column + projEndDate - projStartDate + 1))
    SourceRange.AutoFill Destination:=fillRange, Type:=xlFillDays
End With

End Sub

非常感谢您的帮助......这个已经困扰了我几个星期了。我今天正在研究循环,因为我认为答案可能就在那里。

再次......非常感谢。

于 2013-08-04T20:46:23.697 回答