2016-05-14 86 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