2016-11-18 212 views
0

在我收到的每日輸入中,我做了一個數據透視表,我手動創建了一次。數據透視表具有有效的過濾器。一旦我得到新的輸入,我刷新主鍵。在簡化的方式,它看起來像這樣:如何使用Excel VBA迭代動態數據透視表?

Pivot Table

用小VBA宏,我想找回人的名字,他訪問過的國家,然後做兩個值的檢查。我如何迭代這個動態數據透視表來獲取每一行的名稱和國家(例如Name = Amanda,Country = Spain)?

+0

可以共享數據,這樣我們就不必鍵入所有的東西? – Andreas

+0

爲什麼你必須輸入那個「東西」? – Airlike

+0

測試我認爲可能是一種解決方案 – Andreas

回答

0

此代碼將遍歷A列並填充兩個變量Country和Name。
爲了調試目的,我輸出一個字符串。

Sub Travel() 

    Lastrow = Range("A" & Rows.Count).End(xlUp).Row 
    For i = 4 To Lastrow ' Change i to first row with a name in pivot table 
     If Range("A" & i).IndentLevel = 0 Then ' New person 
      Name = Range("A" & i).Value 
      Country = "" 
     Else 
      Country = Range("A" & i).Value 
     End If 

     If Country <> "" Then 
      ' For debug purpose 
      Range("C" & i).Value = Name & " was in " & Country 
     Else 
      ' The code is "in between" two names 
     End If 
    Next i 


End Sub 

enter image description here

+0

@Airlike無法正常工作或沒有按照您的預期工作? – Andreas

+1

感謝您的代碼片段。我今天將在辦公室進行測試,儘快回覆你 – Airlike