2016-05-14 83 views
0

我使用excel监视我的学习习惯。我有一个像使用另一个过滤一张表作为参考

表1表:

| Start Time | End Time | Date | Pages | 
|------------|----------|--------|-------| 
| 07:40:00 | 08:00:00 | 12 May | 12 | 
| 08:13:00 | 08:25:00 | 12 May | 5  | 
| 08:40:00 | 08:55 | 12 May | 8  | 

正如你可以看到,有一排的结束时间和下一行的起始时间之间的时间间隔。这是因为我通常会因为互联网而分心。我知道如何让谷歌浏览器的历史到格式的表格 -

表2:

| Time Visited | URL Visited    | 
|--------------|---------------------------| 
| 08:03:00  | https://www.reddit.com/ | 
| 08:08:00  | https://www.facebook.com/ | 
| 08:28:00  | https://www.youtube.com/ | 
| 08:32:00  | https://www.facebook.com/ | 
| 08:34:00  | https://www.reddit.com/ | 

有了明显多了很多的网址。我正在寻找一种方式,如果我点击表1中的一行(在Excel中),表2将筛选到该行的End Time与下一行的Start Time之间访问的网站。我是一名优秀的noob,尽可能避免VBA,但如果有人能为我提供任何解决方案,我将不胜感激。也有像可以在这里使用的数据透视表等数据结构?

谢谢

+3

'| | 09:00:00 | http://stackoverflow.com/help/ |' – Ambie

+0

@Ambie,有趣,快速。就此而言,就此而言。 Waivek,虽然这是一个有趣的项目,但你应该展示你的尝试。 –

+0

所以我试图从这两个表格制作一个数据透视表,但这是不可能的。我也尝试使用一个Excel数组公式使用聚合和一个如果它里面,但这是导致每行一个单元这不是我想要的 – waivek

回答

0

花了整晚学习VBA。解决了问题!

解决方案表(转到命名为第二片材 “MySheet的工作”) -

https://drive.google.com/file/d/0Bx5DxTdpRXyyekZlenlxSTZsTW8/view?usp=sharing

代码(其被包括在Excel文件)=

Private Sub Worksheet_SelectionChange(ByVal Target As Range) 
    If Selection.Count = 1 Then 
     Dim name As String 
     Dim triggerCol As String 
     Dim tableToFilter As String 
     Dim ClickedInTable As Boolean 

     Dim lowerColName As String 
     Dim lower As String 
     Dim upperColName As String 
     Dim upper As String 

     Dim colToFilterRelativeIndex As Integer 
     Dim colToFilter As String 

     Dim inHeader As Boolean 
     Dim inTriggerCol As Boolean 

     Dim lowerIsHeader As Boolean 

     Dim lowerCell As Range 
     Dim upperCell As Range 

     tableToFilter = "Olive" 
     colToFilter = "Time Visited" 

     lowerColName = "End time" 
     upperColName = "Start time" 
     triggerCol = "ClickMe" 

     ClickedInTable = Not (ActiveCell.ListObject Is Nothing) 

     If ClickedInTable Then 
      inTriggerCol = (GetColumnName() = triggerCol) 
      If inTriggerCol Then 

       inTableBody = Not (ActiveCell.ListObject.Range.row = ActiveCell.row) 

       If inTableBody Then 

        Set lowerCell = Range(GetNeighborCell(lowerColName)).Offset(-1) 
        Set upperCell = Range(GetNeighborCell(upperColName)) 

        lowerIsHeader = (lowerCell.row = ActiveCell.ListObject.Range.row) 
        If Not (lowerIsHeader) Then 
         lower = lowerCell.Text 
         upper = upperCell.Text 

         colToFilterRelativeIndex = ActiveSheet _ 
          .ListObjects(tableToFilter) _ 
          .ListColumns(colToFilter).index 

         ActiveSheet.ListObjects(tableToFilter).Range.AutoFilter _ 
          Field:=colToFilterRelativeIndex, _ 
          Criteria1:=">=" & lower, _ 
          Operator:=xlAnd, _ 
          Criteria2:="<=" & upper 

         ActiveCell.Value = lower & " - " & upper 
        Else 
         ActiveCell.Value = "N/A" 
        End If 
       End If 
      End If 
     End If 
    End If 
End Sub 

和辅助功能是

Function GetColumnName() 
    Dim rowHeader As Integer 
    Dim colCurrent As Integer 
    Dim name As String 
    rowHeader = ActiveCell.ListObject.Range.row 
    colCurrent = ActiveCell.Column 
    name = Cells(rowHeader, colCurrent).Value 
    GetColumnName = name 
End Function 


Function GetNeighborCell(colName As String) 
    Dim firstCol As Integer 
    Dim offsetCol 
    Dim col 
    Dim row As Integer 
    Dim val As String 

    firstCol = ActiveCell.ListObject.Range.Column 
    offsetCol = ActiveCell.ListObject.ListColumns(colName).index - 1 
    col = firstCol + offsetCol 

    row = ActiveCell.row 
    val = Cells(row, col).Value 
    GetNeighborCell = Cells(row, col).Address 
End Function