工作表的事件_Change什么是真的? 我在事件过程中遇到了以下问题_Change而此代码确实适用于其他子例程。
此宏为移动数据透视图而编写。
Microsoft Excel 将关闭并遇到此错误: Microsoft Excel 遇到问题,需要关闭。对于给您带来的不便,我们深表歉意。并将关闭。
Private Sub Worksheet_Change(ByVal Target As Range)
Application.ScreenUpdating = False
Sheets("sheet1").PivotTables("pvtReport").TableRange2.Cut
Cells(Sheets("sheet1").ListObjects("tblReport").Range.Rows.Count + 2, 13).Select
Sheets("sheet1").Paste
ActiveSheet.PivotTables("PvtReport").PivotSelect "", xlDataAndLabel, True
Selection.End(xlDown).Select
Cells(Selection.Row + 2, Selection.Column).Select
Sheets("sheet1").ChartObjects("InsuranceChart").Cut
Sheets("sheet1").Paste
End Sub
请注意,此代码在Sheets("sheet1").ChartObjects("InsuranceChart").Cut
行中遇到错误。
我建议在Worksheet_Change
事件中只保留基本内容,并将其余逻辑、对象和代码放在常规模块的Sub
中。
为了移动数据透视图,您可以使用ChartObject
的Top
和Left
属性。
代码中的更多解释作为注释。
Worksheet_Change代码(工作表模块内(
Private Sub Worksheet_Change(ByVal Target As Range)
Application.ScreenUpdating = False
Application.EnableEvents = False
MovePivotTable Target.Parent ' call the reugular Sub and pass the Worksheet object
Application.ScreenUpdating = True
Application.EnableEvents = True
End Sub
子移动数据透视表(常规模块(
Option Explicit
Sub MovePivotTable(ws As Worksheet)
Dim PvtTbl As PivotTable
Dim TblReport As ListObject
Dim InsurChtObj As ChartObject
With ws
' set the Pivot Table object
Set PvtTbl = .PivotTables("pvtReport")
' set the Table object (ListObject)
Set TblReport = .ListObjects("tblReport")
' set the Pivot Table Chart object
Set InsurChtObj = .ChartObjects("InsuranceChart")
' move the Pivot Table at the end of the Table Object
PvtTbl.TableRange2.Cut Destination:=.Cells(TblReport.Range.Rows.Count + 2, 13)
' move the Pivot chart at the end of the Table
InsurChtObj.Top = PvtTbl.TableRange1.End(xlDown).Offset(2).Top ' go to the bottom of the Pivot-Table range + 2 more rows
InsurChtObj.Left = PvtTbl.TableRange1.Left
End With
End Sub
在朋友的指导下,我找到了专门针对从右到左的合适答案:
似乎TableRange1.Left
和ChartObject().Left
计算之间存在很大差异。
实际上TableRange1.Left
与工作表Direction
相关,而ChartObject().Left
无关,并从工作表的左侧计算。
问题在下面的代码中解决了,并在后面添加了说明。
Private Sub Worksheet_Change(ByVal Target As Range)
If ActiveSheet.Name = "Sheet1" Then
Application.ScreenUpdating = False
Application.EnableEvents = False
Dim ChartLeft, SheetWidth As Double
SheetWidth = Worksheets("sheet1").Cells.Width
ChartLeft = SheetWidth - _
(Sheets("Sheet1").ChartObjects("InsuranceChart").Width + _
Sheets("Sheet1").PivotTables("pvtReport").TableRange1.Left)
Sheets("Sheet1").ChartObjects("InsuranceChart").Top = _
Sheets("Sheet1").PivotTables("pvtReport").TableRange1.End(xlDown).Offset(2).Top
Sheets("Sheet1").ChartObjects("InsuranceChart").Left = ChartLeft
Application.ScreenUpdating = True
Application.EnableEvents = True
End If
End Sub
解释:
我们使用工作表的宽度来计算数据透视图左侧与工作表左上角之间的距离,从工作表的右到左工作表;从数据透视表的右上角到工作表右侧的距离。
请注意,TableRange1.Left
是数据透视表右角与工作表右侧的距离(从右到左的工作表方向(,ChartObject().Left
是数据透视图左角与工作表左侧的距离(在两个工作表方向上(。
由于数据透视图位置和数据透视表之间的关系,我们希望数据透视图位置的功能适用于各自的数据透视表,因此从该数据透视图的右侧计算数据透视表的左侧,及其Width
和工作表宽度,然后通过数据透视图的属性将此值分配给数据透视图的左角ChartObject().Left
。