Private Sub Worksheet_Change(ByVal Target As Range)

时间:2016-09-23 14:43:33

标签: excel vba excel-vba

我试图创建一个宏,只要E,H或S列发生变化就会自动运行。它运行几个表和数据透视表来更新工作簿,我一直在多个错误。我是VBA的真正初学者,所以我道歉,如果这没有意义,但我真的需要一些帮助

Private Sub Worksheet_Change(ByVal Target As Range)
Application.ScreenUpdating = False
If Target.Column = 5 Or Target.Column = 8 Or Target.Column = 19 Then
Sheets("PlanData").Visible = True
LastRow = Range("E" & Rows.Count).End(xlUp).Row
Range("E2:E" & LastRow).Select
Selection.Copy
Sheets("PlanData").Select
ActiveSheet.[A2].Select
Selection.PasteSpecial Paste:=xlPasteValues
  Sheets("Plans").Select
 LastRow = Range("H" & Rows.Count).End(xlUp).Row
Range("H2:H" & LastRow).Select
  Selection.Copy
 Sheets("PlanData").Select
 ActiveSheet.[B2].Select
   Selection.PasteSpecial Paste:=xlPasteValues
  Sheets("Plans").Select
 LastRow = Range("S" & Rows.Count).End(xlUp).Row
Range("S2:S" & LastRow).Select
Selection.Copy
Sheets("PlanData").Select
 ActiveSheet.[C2].Select
Selection.PasteSpecial Paste:=xlPasteValues
ActiveSheet.Columns("A:A").Select
ActiveWorkbook.Worksheets("PlanData").ListObjects("PlanDataTable").Sort. _
    SortFields.Clear
ActiveWorkbook.Worksheets("PlanData").ListObjects("PlanDataTable").Sort. _
SortFields.Add Key:=Range("PlanDataTable[[#All],[PlanType]]"), SortOn:= _
    xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("PlanData").ListObjects("PlanDataTable").Sort
    .Header = xlYes
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
End With
Selection.SpecialCells(xlCellTypeBlanks).Select
Selection.EntireRow.Delete
ActiveSheet.Columns("B:B").Select
ActiveWorkbook.Worksheets("PlanData").ListObjects("PlanDataTable").Sort. _
    SortFields.Clear
ActiveWorkbook.Worksheets("PlanData").ListObjects("PlanDataTable").Sort. _
SortFields.Add Key:=Range("PlanDataTable[[#All],[ProviderName]]"), SortOn:= _
    xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("PlanData").ListObjects("PlanDataTable").Sort
    .Header = xlYes
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
End With
Selection.SpecialCells(xlCellTypeBlanks).Select
Selection.EntireRow.Delete
Sheets("Top5Providers").Select
  ActiveSheet.ChartObjects("Chart 1").Activate
  ActiveChart.PivotLayout.PivotTable.PivotCache.Refresh
Sheets("Top5PPolicies").Visible = True
Sheets("Top5PPolicies").Select
ActiveSheet.[A3].ShowDetail = False
ActiveSheet.[A4].ShowDetail = False
ActiveSheet.[A5].ShowDetail = False
ActiveSheet.[A6].ShowDetail = False
ActiveSheet.[A7].ShowDetail = False
Sheets("#1ProviderData").Visible = True
Sheets("#2ProviderData").Visible = True
Sheets("#3ProviderData").Visible = True
Sheets("#4ProviderData").Visible = True
Sheets("#5ProviderData").Visible = True
Sheets("#1ProviderData").Select
Dim lastro As Long
Dim LastColum As Long
Dim StartCel As Range
Set StartCel = Range("A2")
lastro = Cells(Rows.Count, StartCel.Column).End(xlUp).Row
LastColum = Cells(StartCel.Row, Columns.Count).End(xlToLeft).Column
Range(StartCel, Cells(lastro, LastColum)).Select
Selection.Delete Shift:=xlUp
Sheets("Top5PPolicies").Select
ActiveSheet.[A3].ShowDetail = True
ActiveSheet.PivotTables("PivotTable1").PivotSelect "PlanType[All]", _
    xlLabelOnly + xlFirstRow, True
Range(Selection, Selection.End(xlToRight)).Select
Selection.Copy
Sheets("#1ProviderData").Select
ActiveSheet.Range("B2").Select
Selection.PasteSpecial Paste:=xlPasteValues
Sheets("Top5PPolicies").Select
ActiveSheet.[A3].ShowDetail = False
[A3].Copy
Sheets("#1ProviderData").Select
ActiveSheet.Range("A2").Select
Range(Selection, Selection.End(xlDown)).Select
ActiveSheet.Paste
Sheets("#1ProviderPlanAnalysis").Select
ActiveChart.PivotLayout.PivotTable.PivotCache.Refresh
 Range("H18").Select
 ActiveCell.FormulaR1C1 = _"=CONCATENATE(""How Many Policy Numbers Does Each Plan Have For"","" "",'#1ProviderPlanAnalysis'!R2C1)"
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues
ActiveSheet.ChartObjects("Chart 1").Activate
ActiveChart.ChartTitle.Caption = "='#1ProviderPlanAnalysis'!$H$18"
Sheets("#2ProviderData").Select
Dim lastr As Long
Dim LastColu As Long
Dim StartCe As Range
Set StartCe = Range("A2")
lastr = Cells(Rows.Count, StartCe.Column).End(xlUp).Row
LastColu = Cells(StartCe.Row, Columns.Count).End(xlToLeft).Column
Range(StartCe, Cells(lastr, LastColu)).Select
Selection.Delete Shift:=xlUp
Sheets("Top5PPolicies").Select
[A4].ShowDetail = True
ActiveSheet.PivotTables("PivotTable1").PivotSelect "PlanType[All]", _
    xlLabelOnly + xlFirstRow, True
Range(Selection, Selection.End(xlToRight)).Select
Selection.Copy
Sheets("#2ProviderData").Select
 Range("B2").Select
Selection.PasteSpecial Paste:=xlPasteValues
Sheets("Top5PPolicies").Select
[A4].ShowDetail = False
[A4].Copy
Sheets("#2ProviderData").Select
Range("A2").Select
Range(Selection, Selection.End(xlDown)).Select
ActiveSheet.Paste
Sheets("#2ProviderPlanAnalysis").Select
ActiveChart.PivotLayout.PivotTable.PivotCache.Refresh
 Range("H18").Select
ActiveCell.FormulaR1C1 = _
    "=CONCATENATE(""How Many Policy Numbers Does Each Plan Have For"","" "",'#2ProviderPlanAnalysis'!R2C1)"
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues
ActiveSheet.ChartObjects("Chart 1").Activate
ActiveChart.ChartTitle.Caption = "='#2ProviderPlanAnalysis'!$H$18"
Sheets("#3ProviderData").Select
Dim last As Long
Dim LastCol As Long
Dim StartC As Range
Set StartC = Range("A2")
last = Cells(Rows.Count, StartC.Column).End(xlUp).Row
LastCol = Cells(StartC.Row, Columns.Count).End(xlToLeft).Column
Range(StartC, Cells(last, LastCol)).Select
Selection.Delete Shift:=xlUp
Sheets("Top5PPolicies").Select
[A5].ShowDetail = True
ActiveSheet.PivotTables("PivotTable1").PivotSelect "PlanType[All]", _
    xlLabelOnly + xlFirstRow, True
Range(Selection, Selection.End(xlToRight)).Select
Selection.Copy
Sheets("#3ProviderData").Select
 Range("B2").Select
Selection.PasteSpecial Paste:=xlPasteValues
Sheets("Top5PPolicies").Select
[A5].ShowDetail = False
[A5].Copy
Sheets("#3ProviderData").Select
Range("A2").Select
Range(Selection, Selection.End(xlDown)).Select
ActiveSheet.Paste
Sheets("#3ProviderPlanAnalysis").Select
ActiveChart.PivotLayout.PivotTable.PivotCache.Refresh
 Range("H18").Select
ActiveCell.FormulaR1C1 = _
    "=CONCATENATE(""How Many Policy Numbers Does Each Plan Have For"","" "",'#3ProviderPlanAnalysis'!R2C1)"
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues
ActiveSheet.ChartObjects("Chart 1").Activate
ActiveChart.ChartTitle.Caption = "='#3ProviderPlanAnalysis'!$H$18"
Sheets("#4ProviderData").Select
Dim las As Long
Dim LastCo As Long
Dim Start As Range
Set Start = Range("A2")
las = Cells(Rows.Count, Start.Column).End(xlUp).Row
LastCo = Cells(Start.Row, Columns.Count).End(xlToLeft).Column
Range(Start, Cells(las, LastCo)).Select
Selection.Delete Shift:=xlUp
Sheets("Top5PPolicies").Select
[A6].ShowDetail = True
ActiveSheet.PivotTables("PivotTable1").PivotSelect "PlanType[All]", _
    xlLabelOnly + xlFirstRow, True
Range(Selection, Selection.End(xlToRight)).Select
Selection.Copy
Sheets("#4ProviderData").Select
 Range("B2").Select
Selection.PasteSpecial Paste:=xlPasteValues
Sheets("Top5PPolicies").Select
[A6].ShowDetail = False
[A6].Copy
Sheets("#4ProviderData").Select
Range("A2").Select
Range(Selection, Selection.End(xlDown)).Select
ActiveSheet.Paste
Sheets("#4ProviderPlanAnalysis").Select
ActiveChart.PivotLayout.PivotTable.PivotCache.Refresh
Range("H18").Select
ActiveCell.FormulaR1C1 = _
    "=CONCATENATE(""How Many Policy Numbers Does Each Plan Have For"","" "",'#4ProviderPlanAnalysis'!R2C1)"
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues
ActiveSheet.ChartObjects("Chart 1").Activate
ActiveChart.ChartTitle.Caption = "='#4ProviderPlanAnalysis'!$H$18"
Sheets("#5ProviderData").Select
Dim la As Long
Dim LastC As Long
Dim Star As Range
Set Star = Range("A2")
la = Cells(Rows.Count, Star.Column).End(xlUp).Row
LastC = Cells(Star.Row, Columns.Count).End(xlToLeft).Column
Range(Star, Cells(la, LastC)).Select
Selection.Delete Shift:=xlUp
Sheets("Top5PPolicies").Select
[A7].ShowDetail = True
ActiveSheet.PivotTables("PivotTable1").PivotSelect "PlanType[All]", _
    xlLabelOnly + xlFirstRow, True
Range(Selection, Selection.End(xlToRight)).Select
Selection.Copy
Sheets("#5ProviderData").Select
 Range("B2").Select
Selection.PasteSpecial Paste:=xlPasteValues
Sheets("Top5PPolicies").Select
[A7].ShowDetail = False
[A7].Copy
Sheets("#5ProviderData").Select
Range("A2").Select
Range(Selection, Selection.End(xlDown)).Select
ActiveSheet.Paste
Sheets("#5ProviderPlanAnalysis").Select
ActiveChart.PivotLayout.PivotTable.PivotCache.Refresh
Range("H18").Select
ActiveCell.FormulaR1C1 = _
    "=CONCATENATE(""How Many Policy Numbers Does Each Plan Have For"","" "",'#5ProviderPlanAnalysis'!R2C1)"
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues
ActiveSheet.ChartObjects("Chart 1").Activate
ActiveChart.ChartTitle.Caption = "='#5ProviderPlanAnalysis'!$H$18"
Sheets("Top5PPolicies").Visible = False
Sheets("#1ProviderData").Visible = False
Sheets("#2ProviderData").Visible = False
Sheets("#3ProviderData").Visible = False
Sheets("#4ProviderData").Visible = False
Sheets("#5ProviderData").Visible = False
Sheets("PlanData").Visible = False
Sheets("Top5Providers").Select

End If


End Sub

0 个答案:

没有答案