Program Page: セル範囲の指定

カテゴリー名: [ExcelVBAとOLE 基本概念とセル操作

2016/08/13

関連の解説ページへ戻る


《このページの目次》


    

1. JScript

△ vovXL03.js

// セル範囲の指定
var fso, bookPath;
var ExlApp, wb, ary, i;
var xlWorkbookNormal = -4143;

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の新規作成
with (wb.ActiveSheet) {
    Range("A1:E1").Value = "test";
    Range(Cells(1,1), Cells(1,5)).Font.Size = 14.0;
    Range("A2").Value = "VBA, OLE, VBScript";
    Range("A2", "C2").MergeCells = true;
    ary = ["牡羊座", "牡牛座", "双子座"];
    for (i=1; i<=ary.length; i++)  Cells(3,i).Value = ary[i-1];
    ary = ["蟹座", "獅子座", "乙女座"];
    for (i=1; i<=ary.length; i++)  Cells(4,i).Value = ary[i-1];
    Range("A3:C4").Borders.LineStyle = true;
}
wb.SaveAs(bookPath, xlWorkbookNormal);
ExlApp.Quit();

目次に戻る


    

2. VBScript

△ vovXL03.vbs

' セル範囲の指定
Option Explicit
Dim FSO, BookPath
Dim EXLapp, WBobj
Const xlWorkbookNormal = -4143

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の新規作成
With WBobj.ActiveSheet
    .Range("A1:E1").Value = "test"
    .Range(.Cells(1,1), .Cells(1,5)).Font.Size = 14.0
    .Range("A2").Value = "VBA, OLE, VBScript"
    .Range("A2", "C2").MergeCells = True
    .Range("A3:C3").Value = Array("牡羊座", "牡牛座", "双子座")
    .Range("A4:C4").Value = Array("蟹座", "獅子座", "乙女座")
    .Range("A3:C4").Borders.LineStyle = True
End With
WBobj.SaveAs BookPath, xlWorkbookNormal
EXLapp.quit

〜 以上 〜