2010-10-05 19 views
1

我有一个通过数据源填充的工作表,该数据源列出垂直和水平的日期。Excel - 根据垂直值选择一个水平列

  • 垂直日期是 需要采取措施的项目。
  • 水平日期是即将到来的工作日日期。

在这个时候,所有我想要做的是选择基于最新的垂直行中发现的水平行的相应列。我会尝试做一个简单的图表。

日期3 2 1 4 5 6 7 8 9

在此示例;我想选择水平行的列为1(如果这是一个新的工作表id期望它是列D我也需要能够通过运行一个宏,即我不能点击最初的任何细胞。

+0

要添加,水平列中的3,2,1代表基本上是延迟的过去的天数,4,5,6,7,8,9是一周的当前/未来几天。 – CaRDiaK 2010-10-05 10:45:39

回答

1

玉家伙,感谢您的方向。

我要扩展超出了这个问题。基本上w ^我最终想要做的一件事就是根据从数据源输入到Excel中的值绘制一张Gannt图表。我的工作需要用预测的开始和结束来完成,所以我打开我的工作表以便按照sql服务器的降序填充部门和日期,然后运行代码。这是一个人在这里2天手动(许多部门)

现在显然这是我的独特之处,但我发现操纵这些日期有点棘手。如果有人在某个时间点寻找类似的东西,我会发布模块的整个代码。

它产生这种; (我强调,我隐藏日期字段。) alt text

说真的,这花了我所有的日子,所以我当然希望它可以帮助别人;) 佩斯

代码;

Sub One_Macro_To_Rule_Them_All() 

' 

'This clears the WOP sheet for formatting 

    Sheets("WOP").Select 
    Range("A8").Select 
    Selection.CurrentRegion.Select 
    Selection.Offset(1, 0).Resize(Selection.Rows.Count - 1, _ 
    Selection.Columns.Count).Select 

    Selection.ClearContents 
    Cells.Select 
    With Selection.Interior 
     .PatternColorIndex = xlAutomatic 
     .ThemeColor = xlThemeColorDark1 
     .TintAndShade = 0 
     .PatternTintAndShade = 0 
    End With 
    Cells(3, 2) = "Date : " & Format(Date, "dd/mm/yyyy") 



'************** 

'Copy the data to the WOP Sheet 

    Sheets("Data").Select 

     Rows("1:1").Select 
    Range(_ 
     "Table_FromMyServer_view_ForwardJobsLive_WOP[[#Headers],[Job No]]") _ 
     .Activate 
    Range("B2").Select 
    Selection.CurrentRegion.Select 
    Selection.Offset(1, 0).Resize(Selection.Rows.Count - 1, _ 
    Selection.Columns.Count - 1).Select 




    Selection.Copy 
     Sheets("WOP").Select 
     Range("A8").Select 
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _ 
     :=False, Transpose:=False 
     Range("A8").Select 
     Cells(2, 2) = "Works Order Priority Sheet - " & Cells(8, 1) 
    Selection.CurrentRegion.Select 
    Selection.Offset(0, 6).Resize(Selection.Rows.Count, _ 
    Selection.Columns.Count - 6).Select 

    curdate = Format(Date, "dd/mm/yyyy") 
    Dim dt As String 
    dt = CStr(curdate) 

    'find the start of the date range 

     Range("A8").Select 
    Selection.CurrentRegion.Select 
    Selection.Offset(0, 6).Resize(Selection.Rows.Count, _ 
    Selection.Columns.Count - 6).Select 
    Dim rngetosearch As Range 
    Set rngetosearch = Selection 

rngetosearch.Find(What:="EARLIER", After:=ActiveCell, LookIn:=xlFormulas _ 
     , LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _ 
     MatchCase:=False, SearchFormat:=False).Select 
     Selection.Offset(0, 1).Resize(Selection.Rows.Count, Selection.Columns.Count + 1).Select 

     dterangestart = ActiveCell 



    '********* 





'************* 

'Format todays column as yellow 
    Range("A8").Select 
    Selection.CurrentRegion.Select 
    Selection.Offset(0, 6).Resize(Selection.Rows.Count, _ 
    Selection.Columns.Count - 6).Select 

Dim sel As Range 
Dim rangetosearch As Range 
Set rangetosearch = Selection 
Dim strdate As String 

strdate = Date 

    strdate = Format(strdate, "Short Date") 

Set sel = rangetosearch.Find(What:=CDate(strdate), After:=ActiveCell, LookIn:=xlFormulas _ 
     , LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _ 
     MatchCase:=False, SearchFormat:=False) 

    If sel Is Nothing = False Then 
    sel.Activate 
    End If 


     ActiveSheet.Range(sel.Cells.Address, ActiveSheet.Range(sel.Cells.Address).End(xlDown)).Select 


    With Selection.Interior 
     .Color = 65535 
    End With 
    '*************** 
    'Cycle Through the rows and change the blocks 
    Sheets("WOP").Select 
    Selection.CurrentRegion.Select 

     Selection.Offset(1, 0).Resize(Selection.Rows.Count - 1, _ 
    Selection.Columns.Count - 0).Select 

    Dim strtdte As Date 
    Dim enddte As Date 
    Dim actdte As Date 
    Dim diff As Integer 
    Dim selrnge As Range 
    Set selrnge = Selection 


    For Each rwrow In selrnge.Rows 
     strtdte = rwrow.Cells(5) 
     enddte = rwrow.Cells(7) 
     actdte = rwrow.Cells(6) 
     cell = rwrow.Cells(1) 

     If strtdte < dterangestart Then 
      'strtdte = dterangestart 
      diff = DateDiff("d", dterangestart, enddte) + 1 

     Else 
     diff = DateDiff("d", strtdte, enddte) 
     End If 



     strdate = strtdte 
     strdate = Format(strdate, "Short Date") 

    Range("A8").Select 
    Selection.CurrentRegion.Select 
    Selection.Offset(0, 6).Resize(Selection.Rows.Count, _ 
    Selection.Columns.Count - 6).Select 

    Set rngetosearch = Selection 
    If strtdte < dterangestart Then 
    Set sel = rngetosearch.Find(What:="EARLIER", After:=ActiveCell, LookIn:=xlFormulas _ 
     , LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _ 
     MatchCase:=False, SearchFormat:=False) 
    Else 
Set sel = rngetosearch.Find(What:=CDate(strdate), After:=ActiveCell, LookIn:=xlFormulas _ 
     , LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _ 
     MatchCase:=False, SearchFormat:=False) 
    End If 
    If sel Is Nothing = False Then 
    Dim col As Integer 
    col = CInt(sel.Column) 

    Selection.CurrentRegion.Select 
    ActiveSheet.Cells(CInt(rwrow.Row), col).Select 

    Selection.Offset(0, 0).Resize(Selection.Rows.Count, Selection.Columns.Count + diff).Select 
    With Selection.Interior 
    .Color = getcolor(CStr(cell)) 
     If actdte <> #12:00:00 AM# Then 
     .TintAndShade = -0.249977111117893 
     .PatternTintAndShade = 0.399975585192419 
     Else 
     .TintAndShade = 0.399975585192419 
     .PatternTintAndShade = 0.399975585192419 
     End If 
    End With 
    End If 

Next 


    '************* 
    Range("A8").Select 

End Sub 
+0

如果您有兴趣,黑暗是已经开始,灯光尚未开始。 – CaRDiaK 2010-10-06 13:58:09

1

我不认为它可以使用宏或标准XLS函数来完成。

你需要编写一个脚本VBA比较值。一旦这样写可以叫上点击一个按钮,或在XLS打开。