将Pivot表粘贴到自身时,Excel VBA挂起

时间:2017-08-18 21:08:49

标签: excel vba excel-vba pivot-table copy-paste

当我尝试将数据透视表粘贴到自身上时,以下代码会挂起。我之前使用了完全相同的代码,但它在这里不起作用。我一步一步地找到了问题。下面是整个代码,问题以粗体显示,接近代码末尾。我添加了整个代码,万一有人可以看到更高的问题,我怀疑,因为它顺利运行到最后的粘贴。

    SaveFileDialog sfd = new SaveFileDialog();
    OpenFileDialog ofd = new OpenFileDialog();
    public static string fileName;

    private void open_btn_Click(object sender, EventArgs e)
    {
        ofd.Filter = "CSV Files (.csv)|*.csv";
        ofd.Title = "Open CSV File";

        if (ofd.ShowDialog() == DialogResult.OK)
        {
            richTextBox1.LoadFile(ofd.FileName, RichTextBoxStreamType.PlainText);

            fileName = ofd.FileName;
            open_label.Text = System.IO.Path.GetFileName(fileName);
        }
    }

    private void save_btn_Click(object sender, EventArgs e)
    {
        sfd.Filter = "JSON Files (.json)|*.json";
        sfd.Title = "Save JSON File";

        if (sfd.ShowDialog() == DialogResult.OK)
        {
            //how do I convert the loaded .csv file into the json format below???
        }
    }

    public class Rootobject
    {
        public Player[] Players { get; set; }
    }

    public class Player
    {
        public int Id { get; set; }
        public int Sea { get; set; }
        public string First { get; set; }
        public string Last { get; set; }
        public string Team { get; set; }
        public string Coll { get; set; }
        public int Num { get; set; }
        public int Age { get; set; }
        public int Hgt { get; set; }
        public int Wgt { get; set; }
        public string Pos { get; set; }
        public Attr Attr { get; set; }
        public Per Per { get; set; }
        public Skills Skills { get; set; }
        public string Flg { get; set; }
        public string Trait { get; set; }
    }

    public class Attr
    {
        public int Str { get; set; }
        public int Agi { get; set; }
        public int Arm { get; set; }
        public int Spe { get; set; }
        public int Han { get; set; }
        public int Intel { get; set; }
        public int Acc { get; set; }
        public int PBl { get; set; }
        public int RBl { get; set; }
        public int Tck { get; set; }
        public int KDi { get; set; }
        public int KAc { get; set; }
        public int End { get; set; }
    }

    public class Per
    {
        public int Lea { get; set; }
        public int Wor { get; set; }
        public int Com { get; set; }
        public int TmPl { get; set; }
        public int Spor { get; set; }
        public int Soc { get; set; }
        public int Mny { get; set; }
        public int Sec { get; set; }
        public int Loy { get; set; }
        public int Win { get; set; }
        public int PT { get; set; }
        public int Home { get; set; }
        public int Mkt { get; set; }
        public int Mor { get; set; }
    }

    public class Skills
    {
        public int G { get; set; }
        public int T { get; set; }
        public int C { get; set; }
        public int WR { get; set; }
        public int TE { get; set; }
        public int DT { get; set; }
        public int DE { get; set; }
        public int LB { get; set; }
        public int SS { get; set; }
        public int CB { get; set; }
        public int FS { get; set; }
        public int RB { get; set; }
        public int FB { get; set; }
        public int QB { get; set; }
        public int K { get; set; }
        public int P { get; set; }
    }

1 个答案:

答案 0 :(得分:0)

我已经稍微修改了你的代码,它运行得很好:

' already defined and set the pt object, why not use it
pt.TableRange2.Copy '<-- copy the TableRange2 of the Pivot-Table

' paste to range "A3"
wsIssues.Range("A3").PasteSpecial xlPasteValues