2016-07-15 91 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