通过更改源数据来更改数据透视表列过滤器

时间:2019-06-26 19:27:17

标签: excel vba pivot-table

我已经建立了一个连接到SAP的宏,执行其任务,获取响应并将其记录在电子表格中。问题是,在脚本无法执行之前,我需要按群集获取数据。

为便于理解,我列出了客户及其代码,并为每个客户构建了一个主密钥,因为在将其输入到SAP之前,我需要使用相等的主密钥代码来过滤客户,因此SAP会接受的。但是在源文件上,我将拥有多个主键代码。

我做了一个数据透视表并将主密钥放在列上,这样我就可以通过主密钥查看客户代码。

有了这个,我就可以通过主密钥进行过滤,并且具有特定的客户代码,现在可以将其发送到SAP。并重复该文件上的所有主密钥。 销售部门每次都会更改此信息,因此主密钥也会每次更改。

我需要做的是从文件中获取所有主密钥(执行该操作),然后,在我卡住的位置,使用它来隐藏和显示一个循环中的一个。 例如(MK代表万能钥匙):

Customer MK1 MK2 MK3 MK4
   X      1
   Y          1
   Z          1
   A      1

因此,我筛选MK1,为其获取客户代码,运行SAP事物,返回到枢轴,过滤下一个现有代码,获取客户代码,运行SAP并重新做一遍。

If Range("A2") <> "" Then

    Range("A1").Select
    Selection.End(xlDown).Select
    ActiveCell.Offset(-1, 1).Select
    Range(Selection, Selection.End(xlUp)).FillDown

    FILTRO2 = ActiveCell

    Range("A1").Select

            Do While ActiveCell <> ""

                FILTRO = ActiveCell

                Sheets("Dinâmica").Activate

                ActiveWorkbook.RefreshAll

                With ActiveSheet.PivotTables("Tabela dinâmica2").PivotFields("Chave")
                    .PivotItems(Split(FILTRO2.Value, ",")).Visible = False
                    .PivotItems(FILTRO).Visible = True
                End With

如上面的代码所示,我处于死胡同。根本没用。

1 个答案:

答案 0 :(得分:0)

您可以切换Pivotitem.Visible可见的每个“主键”。
请注意,始终必须至少保留1个枢轴项。

可以从PivotField.DataRange中读取生成的可见“客户”,Private Sub FilterMasterkeys() Dim pt As PivotTable Dim pfCustomer As PivotField Dim pfMasterkey As PivotField Dim i As Long Dim c As Range ' a cell Set pt = ActiveWorkbook.Sheets("Dinâmica").PivotTables("Tabela dinâmica2") pt.RefreshTable Set pfCustomer = pt.RowFields(1) Set pfMasterkey = pt.ColumnFields("Chave") ' hide all masterkeys except first pfMasterkey.PivotItems(1).Visible = True For i = 2 To pfMasterkey.PivotItems.Count pfMasterkey.PivotItems(i).Visible = False Next i ' set each masterkey visible and get corresponig customers For i = 1 To pfMasterkey.PivotItems.Count - 1 pfMasterkey.PivotItems(i).Visible = True Debug.Print "Masterkey " & pfMasterkey.PivotItems(i).Caption & " is used by ", For Each c In pfCustomer.DataRange.Cells Debug.Print c.Value, Next c Debug.Print pfMasterkey.PivotItems(i + 1).Visible = True pfMasterkey.PivotItems(i).Visible = False Next i ' get the last one also Debug.Print "Masterkey " & pfMasterkey.PivotItems(i).Caption & "is used by ", For Each c In pfCustomer.DataRange.Cells Debug.Print c.Value, Next c Debug.Print End Sub 代表该枢纽的所有可见枢纽。您可以按单元格寻址该范围单元格,或者将其值分配给数组。

Dim arrCustomers() As Variant
arrCustomers = pfCustomer.DataRange.Value

如果您需要所有相应的客户作为数组,请在切换下一个可见的主键后使用此键:

var context = null;
var players = [];

function DrMixPlayer(ele, obj, idx) {
  players.push(this);

  var _idx = idx;
  var _ele = $(ele);
  var _bUrl = obj.beforeUrl; // ControllerPath + "?action=GetDemo&type=before&mID=" + obj.mId;
  var _aUrl = obj.afterUrl; // ControllerPath + "?action=GetDemo&type=after&mID=" + obj.mId;
  var _playingObj = null;
  var _isPlaying = false;
  var _beforeObj = "beforeAudio" + idx;
  var _afterObj = "afterAudio" + idx;
  var _curPosition = 0;
  var _duration = 0;
  var _beforeLoaded = false;
  var _afterLoaded = false;

  var playAfter = function() {
    if (_ele.hasClass("loading")) return false;
    if (_isPlaying && _playingObj != null && _playingObj.id == _afterObj.id)
      return false;
    _Stop();
    _playingObj = _afterObj;
    _Play();
    return false;
  };

  var playBefore = function() {
    if (_ele.hasClass("loading")) return false;
    if (_isPlaying && _playingObj != null && _playingObj.id == _beforeObj.id)
      return false;
    _Stop();
    _playingObj = _beforeObj;
    _Play();
    return false;
  };

  var playPause = function() {
    if (_ele.hasClass("loading")) return false;
    if (_playingObj == null) _playingObj = _beforeObj;
    if (_isPlaying) _Stop();
    else _Play();
    return false;
  };

  var _Play = function() {
    _Stop();
    if (!_beforeLoaded) {
      _beforeLoaded = true;
      _beforeObj.load();
    }
    if (!_afterLoaded) {
      _afterLoaded = true;
      _afterObj.load();
    }
    _ele.addClass("loading");

    if (_playingObj != null) {
      _playingObj.setPosition(_curPosition);
      _playingObj.play();

      _isPlaying = true;
      _ele
        .find(".playPause")
        .html("Stop")
        .val("Stop");
      _ele
        .find(".before,.playPause,.after")
        .removeClass("active")
        .removeClass("inactive");
      if (_playingObj.id == _beforeObj.id) {
        _ele.find(".before,.playPause").addClass("active");
        _ele.find(".after").addClass("inactive");
      } else if (_playingObj.id == _afterObj.id) {
        _ele.find(".after,.playPause").addClass("active");
        _ele.find(".before").addClass("inactive");
      }
    }
    return false;
  };

  var _Stop = function() {
    soundManager.stopAll();
    for (var o in players) {
      players[o].Stop();
    }
    return false;
  };

  this.Stop = function() {
    _isPlaying = false;
    _ele
      .find(".playPause")
      .html("Play")
      .val("Play");
    _ele
      .find(".before,.after,.playPause")
      .removeClass("active")
      .removeClass("inactive");
    return false;
  };

  _ele.find(".playPause").click(playPause);
  _ele.find(".before").click(playBefore);
  _ele.find(".after").click(playAfter);

  _beforeObj = soundManager.createSound({
    id: _beforeObj,
    url: _bUrl,
    onfinish: function() {
      _playingObj = null;
      _curPosition = 0;
      _isPlaying = false;
      _ele
        .find(".playPause")
        .html("Play")
        .val("Play");
      _ele
        .find(".before,.after,.playPause")
        .removeClass("active")
        .removeClass("inactive");
    },
    whileplaying: function() {
      _ele.removeClass("loading");
      _curPosition = this.position;
      _duration = this.duration;
      $("#console").html(_curPosition);
      $("#console3").html(this.url);
    },
    whileloading: function() {
      per = this.bytesLoaded * 100 / this.bytesTotal;
      $("#console1").html(
        this.bytesLoaded +
          " of " +
          this.bytesTotal +
          " (" +
          per +
          ") - " +
          this.duration
      );
    },
    onload: function() {
      _beforeLoaded = true;
    },
    volume: 100
  });

  _afterObj = soundManager.createSound({
    id: _afterObj,
    url: _aUrl,
    onfinish: function() {
      _playingObj = null;
      _curPosition = 0;
      _isPlaying = false;
      _ele
        .find(".playPause")
        .html("Play")
        .val("Play");
      _ele
        .find(".before,.after,.playPause")
        .removeClass("active")
        .removeClass("inactive");
    },
    whileplaying: function() {
      _ele.removeClass("loading");
      _curPosition = this.position;
      _duration = this.duration;
      $("#console").html(_curPosition);
      $("#console3").html(this.url);
    },
    whileloading: function() {
      per = this.bytesLoaded * 100 / this.bytesTotal;
      $("#console2").html(
        this.bytesLoaded +
          " of " +
          this.bytesTotal +
          " (" +
          per +
          ") - " +
          this.duration
      );
    },
    onload: function() {
      _afterLoaded = true;
    },
    volume: 100
  });
}

function initPlayer(items) {
  for (var o in items) {
    if (
      $("#" + items[o].Id).length == 1 &&
      $("#" + items[o].Id).get(0).tagName != "OBJECT"
    )
      new DrMixPlayer("#" + items[o].Id, items[o], o);
  }
}