数据透视表:“工作表中已有数据,是否要替换它?”有没有VBA代码可以找到要替换的单元格?

问题描述 投票:0回答:1

数据透视表:“工作表中已有数据,是否要替换它?”有没有VBA代码可以找到要替换的单元格?

在 Excel 中工作,我正在更新工作簿中的所有数据透视表,并且收到“工作表中已有数据,是否要替换它?”消息,指示工作表,但不指示地址

有没有VBA代码可以找到要替换的单元格?

谢谢!

我已经搜索过,但只找到了 VBA 来查找重叠的数据透视表

excel vba replace pivot-table
1个回答
0
投票

每个数据透视表周围有 3 个范围可供测试。 直接向右的区域、直接下方的区域以及直接向下和右侧的区域。 要检查多少行和列可能需要判断。

这可能会让您开始:

Sub LoopPvtTbls_ActiveSheet()
Dim pvt As PivotTable
Dim pvtAddress As String
Dim pvtRows As Integer
Dim pvtCols As Integer
Dim chkRows, chkCols As Integer     'number of rows and columns below and to the right of table to check
Dim a, b, c As Range

chkRows = 10        'Select a reasonable number to check
chkCols = 10        'Select a reasonable number to check

'Loop through each PivotTable in ActiveSheet
For Each pvt In ActiveSheet.PivotTables
    
    'Get the address and row count
    pvtAddress = ActiveSheet.PivotTables(1).TableRange2.Address
    pvtRows = pvt.TableRange2.Rows.Count
    
    'Check 3 ranges.  Area to directrly to the right of table (a), area directly below table (b), and an area down and right of table (c)
    Set a = Cells(pvt.TableRange2.Row, pvt.TableRange2.Columns.Count).Offset(, 1).Resize(pvtRows, chkCols)
    Set b = Cells(pvt.TableRange2.Row, pvt.TableRange2.Column).Offset(pvtRows).Resize(chkRows, pvt.TableRange2.Columns.Count)
    Set c = Cells(pvt.TableRange2.Row, pvt.TableRange2.Columns.Count).Offset(pvtRows, 1).Resize(chkRows, chkCols)
    If WorksheetFunction.CountA(a) Then MsgBox ("Range " & a.Address & "is not empty")
    If WorksheetFunction.CountA(b) Then MsgBox ("Range " & a.Address & "is not empty")
    If WorksheetFunction.CountA(c) Then MsgBox ("Range " & a.Address & "is not empty")
Next pvt

结束子

© www.soinside.com 2019 - 2024. All rights reserved.