优化枢轴物品的内部颜色

时间:2018-05-01 13:47:35

标签: vba performance pivot

有更好的方法可以更快地执行此代码吗?这将持续12个月,我不知道为什么。我尝试使用变量来定义颜色,但它没有多大帮助。

感谢。

Sub Color()

Application.ScreenUpdating = False

With ActiveSheet.PivotTables("Calendar").PivotFields("Month")
.PivotItems("jan").LabelRange.Interior.Color = RGB(255, 255, 0)
.PivotItems("jan").DataRange.Interior.Color = RGB(255, 255, 0)
.PivotItems("feb").LabelRange.Interior.Color = RGB(255, 153, 0)
.PivotItems("feb").DataRange.Interior.Color = RGB(255, 153, 0)
End With

Application.ScreenUpdating = True

End Sub

2 个答案:

答案 0 :(得分:0)

也许设置Application.Calculation = xlCalculationManual然后返回Application.Calculation = xlCalculationAutomatic

这将阻止您的数据透视表每次执行操作时都刷新。我认为如果它只是因为你正在用颜色进行修饰而感到很奇怪,但它的擅长和优秀可能会很奇怪。

Sub Color()

  Application.ScreenUpdating = False
  Application.Calculation = xlCalculationManual

  With ActiveSheet.PivotTables("Calendar").PivotFields("Month")
    .PivotItems("jan").LabelRange.Interior.Color = RGB(255, 255, 0)
    .PivotItems("jan").DataRange.Interior.Color = RGB(255, 255, 0)
    .PivotItems("feb").LabelRange.Interior.Color = RGB(255, 153, 0)
    .PivotItems("feb").DataRange.Interior.Color = RGB(255, 153, 0)
  End With

  'Force a calc before toggling calculation back on
  Application.Calculate
  Application.Calculation = xlCalculationAutomatic
  Application.ScreenUpdating = True

End Sub

您可能还想在Application.EnableEvents运行时切换Worksheet_SelectionChange以覆盖您的基地。这将阻止事件触发工作表中的任何其他代码。像import React, { Component } from "react"; const teamAPI = 'http://localhost:8080/api/teams/' const playerAPI = 'http://localhost:8080/api/playersByTeam/' const matchAPI = 'http://localhost:8080/api/match/' class View extends Component { constructor() { super(); this.state = { data: [], playersData: [], update: [], team1: [], team2: [], matchData: [], testTeam: [], }; } componentWillReceiveProps(newProps) { if (newProps.matchId !== this.props.matchId) { fetch(matchAPI + newProps.matchId) .then((matchResponse) => matchResponse.json()) .then((matchfindresponse) => { console.log(matchfindresponse.team1.name); console.log(this.props.matchId + ' ' + newProps.matchId); this.setState({ testTeam:matchfindresponse.team1.name, }) }) } } componentDidMount() { const promises = [ fetch(teamAPI).then(resp => resp.json()), fetch(playerAPI + 82).then(resp => resp.json()) ]; Promise.all(promises).then(([teamData, playerData]) => { console.log(teamData); console.log(playerData); this.setState({ team1:teamData[0].name, team2:teamData[1].name, playersData: playerData, }) }); } reply_click = id => { return () => { this.setState({ targetId: id }) } } updateScore() { fetch('http://localhost:8080/api/updateMatch?matchId=15&result=2:2') } render() { return ( <div class="container"> <div class="row"> <div class="col-md-6 col-md-offset-3"> <div class="jumbotron text-center"> <form> <div class="form-group"> <label for="teamSelect">Select team:</label> <select class="form-control" id='?' onChange={this.reply_click}> <option>{this.state.testTeam}</option> <option>{this.state.team2}</option> </select> </div> ... 或你有什么。

答案 1 :(得分:0)

这对我有用。它只用了几秒钟。

Sub Color_two()
Dim Color(12)
    Color(1) = RGB(255, 255, 0)
    Color(2) = RGB(255, 153, 0)
    Color(3) = RGB(255, 153, 0)
    Color(4) = RGB(255, 0, 0)
    Color(5) = RGB(255, 0, 255)
    Color(6) = RGB(102, 0, 204)
    Color(7) = RGB(51, 0, 255)
    Color(8) = RGB(0, 0, 255)
    Color(9) = RGB(51, 102, 102)
    Color(10) = RGB(153, 255, 0)
    Color(11) = RGB(153, 153, 153)
    Color(12) = RGB(153, 255, 0)
With ActiveSheet.PivotTables("Calendar")
    For i = 1 To 12
        Mmonth= "'" & i & "'"
        .PivotSelect "Month[" & Mmonth & "]", xlDataAndLabel, True
        Selection.Interior.Color = Color(i)
    Next i
End With
End Sub