カテゴリー名: [ExcelVBAとOLE 基本概念とセル操作]
// セルの削除
var fso, bookPath;
var ExlApp, wb, ws;
var xlWorkbookNormal = -4143;
var xlShiftToLeft = -4159;
var xlShiftUp = -4162;
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;
PutData(ws, 1);
ws.Range("A1").Delete; // 1個だけ削除:上に繰り上がり
PutData(ws, 5);
ws.Range("A5").Delete(xlShiftToLeft); // 1個だけ削除:左シフトを指示
PutData(ws, 9);
ws.Range("A9:A10").Delete; // 縦長を削除:左にシフト
PutData(ws, 13);
ws.Range("A13:B13").Delete; // 横長を削除:上に繰り上がり
wb.SaveAs(bookPath, xlWorkbookNormal);
ExlApp.Quit();
function PutData(ws, ri) {
var ary = [["1番", "2番", "3番"],
["4番", "5番", "6番"], ["7番", "8番", "9番"]]
for (var i=0; i<ary.length; i++) {
for (var j=0; j<ary[i].length; j++) {
ws.Cells(ri+i, j+1).Value = ary[i][j];
}
}
}
' セルの削除
Option Explicit
Dim FSO, BookPath
Dim EXLapp, WBobj, WSobj
Const xlWorkbookNormal = -4143
Const xlShiftToLeft = -4159
Const xlShiftUp = -4162
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
Call PutData(WSobj, 1)
WSobj.Range("A1").Delete ' 1個だけ削除:上に繰り上がり
Call PutData(WSobj, 5)
WSobj.Range("A5").Delete xlShiftToLeft ' 1個だけ削除:左シフトを指示
Call PutData(WSobj, 9)
WSobj.Range("A9:A10").Delete ' 縦長を削除:左にシフト
Call PutData(WSobj, 13)
WSobj.Range("A13:B13").Delete ' 横長を削除:上に繰り上がり
WBobj.SaveAs BookPath, xlWorkbookNormal
EXLapp.quit
Sub PutData(WSobj, Ri)
With WSobj
.Range(.Cells(Ri,1), .Cells(Ri,3)).Value = _
Array("1番", "2番", "3番")
.Range(.Cells(Ri+1,1), .Cells(Ri+1,3)).Value = _
Array("4番", "5番", "6番")
.Range(.Cells(Ri+2,1), .Cells(Ri+2,3)).Value = _
Array("7番", "8番", "9番")
End With
End Sub
〜 以上 〜