2016-07-15 92 views
0

我正在編寫一個時間軸,其中包含一個時間軸,其中包含事件從開始日期到結束日期的事件。vba時間線日期爲範圍,根據2個單元格值中的日期選擇子範圍Excel2013

我有我的日期從「E19」一路開始「RU19」(結束日期列可以是任意的。日期是從03月 - 2016年31月 - 2017年。

活動有3列:

  • 開始日期:A22
  • 結束日期:B22
  • 事件名稱:C22

我成立了一個RAN從「E19」到文件「RU19」的最後一列。是否可以設置一個基於單元值的子範圍,該值將遍歷主範圍並返回子範圍從開始到結束的值?

因此,例如,如果我在A22單元格中的子範圍開始日期是2016年4月5日,B22單元格中的結束日期是08年4月8日,我會選擇一個子範圍「G19:J19」。

當前的代碼:

Dim LastCol As Long 
Dim startDate As Range 
'find last column in the document 
LastCol = Cells(19, Columns.Count).End(xlToLeft).Column 
'set timeline range from start of date data to last column 
Set startDate = Range(Cells(19, 5), Cells(19, LastCol)) 
+0

哎呀沒錯,這是一個錯字。現在修好 – ExcelUsr019

回答

0

感謝您的建議YowE3k,但我想我找到了解決方案。

我創建了3個範圍,rngSelect,rngStart和rngEnd。

rngStart和rngEnd在主範圍中查找我的開始和結束單元格的值,rngSelect只是簡單地放置找到的日期的地址值並選擇它。

現在我可以繼續將範圍偏移到與事件相同的行併爲範圍着色。

代碼如下:

LastCol = Cells(19, Columns.Count).End(xlToLeft).Column 

Set startDate = Range(Cells(19, 5), Cells(19, LastCol)) 
Set rngStart = startDate.Find(Range("A22")) 
Set rngEnd = startDate.Find(Range("B22")) 
Set rngSelect = Range(rngStart.Address, rngEnd.Address) 

    rngSelect.Select 
0

你像後:

Dim StartDate As Range 
Dim EndDate As Range 
Dim EventRange As Range 

Set StartDate = Cells(19, 5 + Cells(22, 1) - Cells(19, 5)) 
Set EndDate = Cells(19, 5 + Cells(22, 2) - Cells(19, 5)) 
Set EventRange = Range(StartDate, EndDate) 
EventRange.Value = Cells(22, 3) 

這會寫在你的19列有日期,所以我猜這是不完全你在追求什麼,但希望它會給你一些線索來繼續。如果在E19:RU19中實際上沒有日期,只需將「Set StartDate」和「Set EndDate」語句末尾的「Cells(19,5)」替換爲實際日期I從問題想到會在那裏,即

Set StartDate = Cells(19, 5 + Cells(22, 1) - DateSerial(2016, 4, 3)) 
Set EndDate = Cells(19, 5 + Cells(22, 2) - DateSerial(2016, 4, 3)) 

繼在你的答案的意見,下面將選擇的子區間爲每行22〜30(更改循環,以滿足您的需求。)

For rowToProcess = 22 To 30 
    Set StartDate = Cells(rowToProcess, 5 + Cells(rowToProcess, 1) - Cells(19, 5)) 
    Set EndDate = Cells(rowToProcess, 5 + Cells(rowToProcess, 2) - Cells(19, 5)) 
    Set EventRange = Range(StartDate, EndDate) 
    'Do whatever you need with the subrange here, e.g. fill in cells with black colour to make it look like a project plan 
    range(Cells(rowToProcess, 5), Cells(rowToProcess, Cells.SpecialCells(xlCellTypeLastCell).Column).Interior.Color = xlNone 
    EventRange.Interior.Color = vbBlack 
Next