如何在for循环中重命名数据透视表?

时间:2017-02-08 19:58:30

标签: vba excel-vba for-loop pivot pivot-table

我想通过使用for循环在一个excel表中生成四个不同的数据透视表。但是,当我运行我的vba代码时,它显示已经使用了该数据透视表的名称。然后我想也许我可以生成一个名称向量,然后每次从该向量中选择名称作为pivottable的名称,但它不起作用。我这样做:

Function ReadExcelReport (){
$global:Report = "C:\TEMP\Tools\Scripts\agents.csv"
$Unresponsive = import-csv $global:Report | Where-Object {($_.State -eq "QUEUED" -or $_.State -eq "FAILED")} #Add items to array from spreadsheet where the state is equal to queued or failed
$global:UnresponsiveMachineInfo = @()
foreach ($item in $Unresponsive){
        $global:UnresponsiveMachineInfo += ,@($item.'Hostname', $item.'IP Address',$item.'Error',$item.'Agent Cert ID') #Build the object - Add the following columns hostname, ip address, error, agent cert id
}
ADCheck
}

Function ADCheck (){
$Machine = $null
$global:MachinesInAD = @()
$global:MachinesNotInAD = @()
$global:MachineObject = New-Object system.object
$global:MachineObject = $Null
$global:MachinesInAD = $null

$global:UnresponsiveMachineInfo | foreach-object { #Iterate through each object in the array

    $global:MachineObject = $_
    $Machine = $_[0] #Set Machine to the hostname AKA the first element in the array for the current ($_) object (objects defined above)

    try{ 
        write-host "Checking A.D. for: $Machine"
        if (Get-ADComputer $Machine){ #Check to see if the machine is in A.D.
            write-host "Found $Machine in A.D." -ForegroundColor Green
            $global:MachinesInAD += $global:MachineObject
        }
    }
    catch [Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException] { #If the machine was NOT in A.D. catch the error it creates and...
        write-warning -message "Machine $Machine not found in A.D."
        $global:MachinesNotInAd += $MachineObject
    }
    }
}

另外,如果我只想显示一个fieldpage的结果 任何人都可以帮我看一下吗?并给我任何建议?

这是我的代码。

For n = 0 To 3
    PivotTable_Name(n) = "PivotTable" & "n"
Next n

0 个答案:

没有答案