2013-08-04 70 views
0

我相當精通Excel VBA(有很多在線幫助!!)......但是我偶然發現了一個讓我完全陷入困境的問題。Excel - 使用基於用戶輸入的日期填充列

我正在爲項目計劃創建甘特圖。我的問題是,我希望讓用戶指定項目開始日期和項目結束日期,然後讓電子表格填充從開始日期到結束日期的列。

請參閱基本佈局: enter image description here

我已經見底與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 

位,我只能和正在編寫代碼從起始日期來填充到最終用戶輸入僅限日期。我肯定這肯定是某種循環安排......至今我還沒有弄明白。

我想知道你們中的任何人是否可以提出解決方案?

由於提前,

保羅

+1

你的意思是「讓電子表格填充從開始日期到結束日期的列」。 – Tarik

+0

嗨Tarik,我希望電子表格用用戶定義的項目開始日期(變量projStartDate)填充單元格e10,然後用項目開始日期到項目結束日期的每個日期填充第10行中的所有後續列。我希望宏一旦單元格值達到項目結束日期(變量projEndDate)就停止填充。我正在查找的功能是一個圖表,範圍從用戶定義的開始日期到用戶定義的項目結束日期。 –

回答

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 
0

Chuff,這是正確的錢。

我已經採取了你的代碼,並修改了它咯...因爲某些原因沒有執行的複製和粘貼

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 

謝謝你曾經這麼多的幫助......這一項一直在竊聽我幾個星期。我今天正在考慮循環,因爲我認爲答案可能在那裏。

再次......非常感謝。