更新数据透视数据范围字符串或范围

时间:2017-07-06 19:28:46

标签: excel vba excel-vba

我正在编写一个拼凑在一起的代码,但我发现在找到“used”范围之后(由于可靠性,我试图不使用.usedrange)是SourceData:=期待一个字符串(我认为)。

有没有办法通过从Data工作表到数据透视表数据范围的范围?我尝试追加RealUsedRange.Address,但这也不算运气。

Sub UpdatePivotRange()

    Dim Rng1        As Range
    Dim oWB         As Workbook
    Dim oWS         As Worksheet
    Dim DataSheet   As Worksheet
    Dim oPT         As PivotTable

    Set oWB = ThisWorkbook
    Set DataSheet = oWB.Sheets("Data")
    Set Rng1 = RealUsedRange

    If Rng1 Is Nothing Then
        MsgBox "There is no used range, the worksheet is empty."
    Else
        For Each oWS In oWB.Worksheets
            For Each oPT In oWS.PivotTables
            'ERRROR BEGINS HERE #####
                oPT.ChangePivotCache _
                    oWB.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=Rng1)
            'ERROR ENDS HERE #####
            Next oPT
        Next oWS
    End If

End Sub

Public Function RealUsedRange() As Range

    Dim FirstRow        As Long
    Dim LastRow         As Long
    Dim FirstColumn     As Integer
    Dim LastColumn      As Integer
    Dim DataSheet       As Worksheet
    Dim oWB             As Workbook

    On Error Resume Next
    Set oWB = ThisWorkbook
    Set DataSheet = oWB.Sheets("Data")
        With DataSheet

             FirstRow = DataSheet.Cells.Find(What:="*", After:=Cells(1, 1), LookIn:=xlValues, LookAt:= _
             xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext).Row

             FirstColumn = DataSheet.Cells.Find(What:="*", After:=Cells(1, 1), LookIn:=xlValues, LookAt:= _
             xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext).Column

             LastRow = DataSheet.Cells(.Rows.Count, "A").End(xlUp).Row

             LastColumn = DataSheet.Cells(1, Columns.Count).End(xlToLeft).Column

             Set RealUsedRange = Range(Cells(FirstRow, FirstColumn), Cells(LastRow, LastColumn))

         End With
    MsgBox "The range is" & RealUsedRange.Address
    On Error GoTo 0

End Function

1 个答案:

答案 0 :(得分:0)

我能够通过添加:

来修改错误行
<plugin>
  <groupId>org.codehaus.mojo</groupId>
  <artifactId>build-helper-maven-plugin</artifactId>
  <version>3.0.0</version>
  <executions>
    <execution>
      <id>timestamp-property</id>
      <phase>validate</phase> 
      <goals>
        <goal>timestamp-property</goal>
      </goals>
      <configuration>
        <name>build.time</name>
        <pattern>MM-dd-YYYY-hh-mm-SS</pattern>
        <locale>en_US</locale>
        <timezone>EST</timezone>
      </configuration>
    </execution>
  </executions>
</plugin> 

希望这可以帮助任何寻找未来的人。