カテゴリー名: [ExcelVBAとOLE 基本概念とセル操作]
// セルの挿入
var fso, bookPath;
var ExlApp, wb, ws;
var xlWorkbookNormal = -4143;
var xlShiftDown = -4121;
var xlShiftToRight = -4161;
fso = WScript.CreateObject("Scripting.FileSystemObject");
bookPath = fso.GetAbsolutePathName("Book1.xls");
if (fso.FileExists(bookPath)) fso.DeleteFile(bookPath);
ExlApp = WScript.CreateObject("Excel.Application"); // Excelの起動
ExlApp.Visible = true; // Excelを見える状態に
wb = ExlApp.Workbooks.Add(); // Workbookの新規作成
ws = wb.ActiveSheet;
with (ws) {
PutData(Range("A1"));
Range("A1").Insert; // 1個だけ挿入:下に移動
PutData(Range("A11"));
Range("A11").Insert(xlShiftToRight); // 1個だけ挿入:右シフトを指示
PutData(Range("K1"));
Range("K1:K2").Insert; // 縦長を挿入:右にシフト
PutData(Range("K11"));
Range("K11:L11").Insert; // 横長を挿入:下にシフト
}
wb.SaveAs(bookPath, xlWorkbookNormal);
ExlApp.Quit();
function PutData(base) {
var ary = [["1番", "2番", "3番"],
["4番", "5番", "6番"], ["7番", "8番", "9番"]]
for (var i=1; i<=ary.length; i++) {
for (var j=1; j<=ary[i-1].length; j++) {
base.Cells(i,j).Value = ary[i-1][j-1];
}
}
}
' セルの挿入
Option Explicit
Dim FSO, BookPath
Dim EXLapp, WBobj, WSobj
Const xlWorkbookNormal = -4143
Const xlShiftDown = -4121
Const xlShiftToRight = -4161
Set FSO = CreateObject("Scripting.FileSystemObject")
BookPath = FSO.GetAbsolutePathName("Book1.xls")
If (FSO.FileExists(BookPath) = True) Then FSO.DeleteFile(BookPath)
Set EXLapp = CreateObject("Excel.Application") ' Excelの起動
EXLapp.Visible = True ' Excelを見える状態に
Set WBobj = EXLapp.Workbooks.Add() ' Workbookの新規作成
Set WSobj = WBobj.ActiveSheet
With WSobj
PutData .Range("A1")
.Range("A1").Insert ' 1個だけ挿入:下に移動
PutData .Range("A11")
.Range("A11").Insert xlShiftToRight ' 1個だけ挿入:右シフトを指示
PutData .Range("K1")
.Range("K1:K2").Insert ' 縦長を挿入:右にシフト
PutData .Range("K11")
.Range("K11:L11").Insert ' 横長を挿入:下にシフト
End With
WBobj.SaveAs BookPath, xlWorkbookNormal
EXLapp.quit
Sub PutData(base)
With base
.Range("A1:C1").Value = Array("1番", "2番", "3番")
.Range("A2:C2").Value = Array("4番", "5番", "6番")
.Range("A3:C3").Value = Array("7番", "8番", "9番")
End With
End Sub
〜 以上 〜