【VB.NET】Visual Basic .NET から Excel を自動化して、配列を使用して範囲内のデータを入力または取得する方法

<VB.NETの学習シリーズは固定ページで記録していきます>

Visual Basic .NET から Excel を自動化して、配列を使用して範囲内のデータを入力または取得する方法

https://docs.microsoft.com/ja-jp/previous-versions/office/troubleshoot/office-developer/automate-excel-from-visual-basic-net

 

⓪VisualStudio Community  2019 -16.7.0をインストールします。
  選択したコンポーネントは3個だけです
   .NETデスクトップ開発  
  C++によるデスクトップ開発
  ユニバーサルWindowsプラットフォーム開発

①VB.NETのWindowsフォームアプリ新規プロジェクトを作ります。
  真っ白なForm1.vbが生成されますので、ここにツールボックスを使ってオブジェクトを加えます。

ボタン1、ボタン2、チェックボックス1をドラッグドロップで作成
 
チェックボックス1のPropertyを開いて、(Name)に FillWithStrings
と書き込む

②EXCELと接続するためのオブジェクトライブラリーを参照に追加します
メニューの プロジェクト=>参照の追加 をクリック
COM指定で、ライブラリーのリストがでるので、
Microsoft Excel 12.0 Object Library をチェックする
※EXCEL2007用です。
※インストールされているExcelに合わせたライブラリーがでます

 

 

③MSドキュメントのページからソースをForm1.vb(コード)に
コピペする。

Visual Basic .NET から Excel を自動化して、配列を使用して範囲内のデータを入力または取得する方法

https://docs.microsoft.com/ja-jp/previous-versi

ここからダウンロードしてコンパイルが通ったソースをGISTにアップしてあります。
https://gist.github.com/dj1711572002/2731de4e0426247d1a7377f2c61d123a

Imports Microsoft.Office.Interop
Public Class Form1’Keep the application object and the workbook object global, so you can
‘retrieve the data in Button2_Click that was set in Button1_Click.
Dim objApp As Excel.Application
Dim objBook As Excel._WorkbookPrivate Sub Button1_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles Button1.Click
Dim objBooks As Excel.Workbooks
Dim objSheets As Excel.Sheets
Dim objSheet As Excel._Worksheet
Dim range As Excel.Range’ Create a new instance of Excel and start a new workbook.
objApp = New Excel.Application()
objBooks = objApp.Workbooks
objBook = objBooks.Add
objSheets = objBook.Worksheets
objSheet = objSheets(1)’Get the range where the starting cell has the address
‘m_sStartingCell and its dimensions are m_iNumRows x m_iNumCols.
range = objSheet.Range(“A1”, Reflection.Missing.Value)
range = range.Resize(5, 5)If (Me.FillWithStrings.Checked = False) Then
‘Create an array.
Dim saRet(5, 5) As Double

‘Fill the array.
Dim iRow As Long
Dim iCol As Long
For iRow = 0 To 5
For iCol = 0 To 5

‘Put a counter in the cell.
saRet(iRow, iCol) = iRow * iCol
Next iCol
Next iRow

‘Set the range value to the array.
range.Value = saRet

Else
‘Create an array.
Dim saRet(5, 5) As String

‘Fill the array.
Dim iRow As Long
Dim iCol As Long
For iRow = 0 To 5
For iCol = 0 To 5

‘Put the row and column address in the cell.
saRet(iRow, iCol) = iRow.ToString() + “|” + iCol.ToString()
Next iCol
Next iRow

‘Set the range value to the array.
range.Value = saRet
End If

‘Return control of Excel to the user.
objApp.Visible = True
objApp.UserControl = True

‘Clean up a little.
range = Nothing
objSheet = Nothing
objSheets = Nothing
objBooks = Nothing
End Sub

Private Sub Button2_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles Button2.Click
Dim objSheets As Excel.Sheets
Dim objSheet As Excel._Worksheet
Dim range As Excel.Range

‘Get a reference to the first sheet of the workbook.
On Error GoTo ExcelNotRunning
objSheets = objBook.Worksheets
objSheet = objSheets(1)

ExcelNotRunning:
If (Not (Err.Number = 0)) Then
MessageBox.Show(“Cannot find the Excel workbook. Try clicking Button1 to ” +
“create an Excel workbook with data before running Button2.”,
“Missing Workbook?”)

‘We cannot automate Excel if we cannot find the data we created,
‘so leave the subroutine.
Exit Sub
End If

‘Get a range of data.
range = objSheet.Range(“A1”, “E5”)

‘Retrieve the data from the range.
Dim saRet(,) As Object
saRet = range.Value

‘Determine the dimensions of the array.
Dim iRows As Long
Dim iCols As Long
iRows = saRet.GetUpperBound(0)
iCols = saRet.GetUpperBound(1)

‘Build a string that contains the data of the array.
Dim valueString As String
valueString = “Array Data” + vbCrLf

Dim rowCounter As Long
Dim colCounter As Long
For rowCounter = 1 To iRows
For colCounter = 1 To iCols

‘Write the next value into the string.
valueString = String.Concat(valueString,
saRet(rowCounter, colCounter).ToString() + “, “)

Next colCounter

‘Write in a new line.
valueString = String.Concat(valueString, vbCrLf)
Next rowCounter

‘Report the value of the array.
MessageBox.Show(valueString, “Array Values”)

‘Clean up a little.
range = Nothing
objSheet = Nothing
objSheets = Nothing
End Sub

End Class

④ 開始を押してコンパイルビルドする

5x5の配列変数の値がシートに書き込みできました。

 

●関連学習でEXCELイベントを取得する記事もあります。動画で操作記録してあります。

【VB.NET】Visual Basic .NET から Excel を自動化して、配列を使用して範囲内のデータを入力または取得する方法

 

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です