Excel VBA(通过JavaScript) - 将工作表移动到新位置

时间:2015-06-16 15:07:48

标签: javascript excel vba excel-vba

使用以下代码,我试图将Excel工作簿中的工作表从一个位置移动到另一个位置。但是,Excel不会进行移动 - 而是创建新的工作簿。如何在同一工作簿中将工作表从一个位置移动到另一个位置?

///////////////////////////////////////////////////////////////////////////
//                                                                       //
// function serviceInitExcel($scope)                                     //
// Version:                                                              //
// Description: Initialize Excel Spreadsheet                             //
// Date:                                                                 //
// To Do:                                                                //
// Author:                                                               //
//                                                                       //
///////////////////////////////////////////////////////////////////////////

var serviceInitExcel = function() {

    var _SCFR;

    var _filename = "emptyServiceValue";
    var _serviceBrowser = "";

    var service = {};

    service.soundOff = function() {

        alert("Another generic method");

    }

    service.initExcel = function() {

        var _scfr = new ActiveXObject("Excel.Application");
            _scfr.Visible = true;
            _scfr.Workbooks.Add;

        // alert("Application Initialized: " + _scfr.Name);

        _SCFR = _scfr;

        return _SCFR;

    } // genericMethod

    service.createSheet = function(iWorkbook, sheetName) {

        var pblSheet = iWorkbook.Sheets("Payable");
            pblSheet.Range("A1").Value = "Payable Sheet";

        var newSheet = iWorkbook.Worksheets.Add;
            newSheet.Name = sheetName;

            newSheet.Select

            newSheet.Range("A1").Value = "_SCFR.Name: "     + _SCFR.Name;
            newSheet.Range("A2").Value = "ActiveWorkbook: " + _SCFR.ActiveWorkbook.Name;
            newSheet.Range("A3").Value = "Sheet 1: " + _SCFR.ActiveWorkbook.Sheets(1).Name;
            newSheet.Range("A4").Value = "Sheet 2: " + _SCFR.ActiveWorkbook.Sheets(2).Name;
            newSheet.Range("A5").Value = "Sheet 3: " + _SCFR.ActiveWorkbook.Sheets(3).Name;

            newSheet.Range("A6").Value = "testSheet: " + iWorkbook.Sheets("testSheet").Name;
            newSheet.Range("A7").Value = "Payable: "   + iWorkbook.Sheets("Payable").Name;
            newSheet.Range("A8").Value = "Paid: "      + iWorkbook.Sheets("Paid").Name;

            for (var s=0; s < 500; s++) {

                newSheet.Range("A9").Value = s;

                newSheet.Select

                iWorkbook.Sheets("Payable").Select

            }

            iWorkbook.Sheets(newSheet.Name).Move + "After:=" + iWorkbook.Sheets("Payable");

            // newSheet.Move + "After:=" + iWorkbook.Sheets(3);

            // Sheets("Payable").Select
            // Sheets.Add
            // Sheets("Sheet6").Select
            // Sheets("Sheet6").Move After:=Sheets(3)

            // _SCFR.Workbooks("Book1.xlsx").Sheets("testSheet").Move + "After:=" + _SCFR.Workbooks("Book1.xlsx").Sheets("Payable");

        return iWorkbook;

    } // genericMethod

    return service;

}

2 个答案:

答案 0 :(得分:2)

这应该这样做:

newSheet.Move(null, iWorkbook.Sheets("Payable"))

答案 1 :(得分:1)

我认为如果你取出连接它会很好。试试这个:

iWorkbook.Sheets(newSheet.Name).Move After:=iWorkbook.Sheets("Payable")