Back to Top

Daily Archives: 2016年3月23日

VB.NET Excelの名前付きセルの位置を取得する

Excelの名前付きセルの位置および、列数・行数を取得する方法を記載します。

名前付きセルの位置および行数・列数を取得する

Dim xlApp As Excel.Application = New Excel.Application()
Dim xlBook As Excel.Workbook = xlApp.Workbooks.Open("C:\work\myBook.xls")
Dim xlSheet As Excel.Worksheet = xlBook.Worksheets("Sheet1")

Dim xlRange As Excel.Range = xlSheet.Range("ALL")

'列の位置
Dim x As Integer = xlRange.Column
'行の位置
Dim y As Integer = xlRange.Row

'列の数
Dim cols As Integer = xlRange.Columns.Count
'行の数
Dim rows As Integer = xlRange.Rows.Count

xlSheet.Activate()
xlApp.Visible = True

System.Threading.Thread.Sleep(3000)
xlApp.Quit()

System.Runtime.InteropServices.Marshal.ReleaseComObject(xlRange)
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlSheet)
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlBook)
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp)

 

VB.NET配列文

配列の確保割り当てには複数のやり方があります。

方法1
Dim 変数名(要素数) As 型名

方法2
Dim 変数名() As 型名 = {値1 [, 値2...]}

方法3
Dim 変数名() As 型名
変数名 = New 型名(要素数){}

方法4
Dim 変数名() As 型名
変数名 = New 型名(要素数){値1 [, 値2...]}
'方法1

'配列を確保する
Dim arr1(5) As Integer

-------------------------------------------------------
'方法2

'配列を確保し値を割り当てる
Dim arr2() As Integer = {100, 200, 300, 400, 500}

-------------------------------------------------------
'方法3

'参照型の配列変数のみ確保する
Dim arr3() As Integer

'配列の実体を確保し割り当てる
arr3 = New Integer(4){}

-------------------------------------------------------
'方法4

'参照型の配列変数のみ確保する
Dim arr4() As Integer

'配列に値を割り当てる
arr4 = New Integer(){ 1, 2, 3, 4, 5 }