![]()
筆箱にVBAのカンニングペーパーを入れる係のみすくです。こんにちは。

Excelのシートから読込み、Collectionに格納します。
配列で読み込んじゃったほうが簡単だけれど、
取り込み対象の列が増えたときやあれこれ編集する場合は、
配列は修正しづらいので、Collectionで実装してみました。
使うときは
SheetDataBeanとSheetDataAggregateを修正。
イテレータのインターフェイス
Option Explicit Public Function hasNext() As Boolean End Function Public Function nextItem() As Object End Function
集合体のインターフェイス
Option Explicit
Public Function iterator() As IIterator
End Function
読込対象のワークシートの列をプロパティに持つデータクラス。
Option Explicit
Private this_category As String 'カテゴリー
Private this_vegetable As String '野菜
Private this_fruits As String 'くだもの
Private this_seasoning As String '調味料
Private this_confection As String 'お菓子
'---------------------------------------
' 列インデックス
'---------------------------------------
Private Enum eCOL
COL_CATEGORY = 1
COL_VEGETABLE
COL_FRUITS
COL_SEASONING
COL_CONFECTION
End Enum
'-------------------------------------------------------------------------------
' 【getter・setter】カテゴリー
'-------------------------------------------------------------------------------
Public Property Get category() As String
category = this_category
End Property
Public Property Let category(ByVal value As String)
this_category = value
End Property
'-------------------------------------------------------------------------------
' 【getter・setter】野菜
'-------------------------------------------------------------------------------
Public Property Get vegetable() As String
vegetable = this_vegetable
End Property
Public Property Let vegetable(ByVal value As String)
this_vegetable = value
End Property
'-------------------------------------------------------------------------------
' 【getter・setter】くだもの
'-------------------------------------------------------------------------------
Public Property Get fruits() As String
fruits = this_fruits
End Property
Public Property Let fruits(ByVal value As String)
this_fruits = value
End Property
'-------------------------------------------------------------------------------
' 【getter・setter】調味料
'-------------------------------------------------------------------------------
Public Property Get seasoning() As String
seasoning = this_seasoning
End Property
Public Property Let seasoning(ByVal value As String)
this_seasoning = value
End Property
'-------------------------------------------------------------------------------
' 【getter・setter】お菓子
'-------------------------------------------------------------------------------
Public Property Get confection() As String
confection = this_confection
End Property
Public Property Let confection(ByVal value As String)
this_confection = value
End Property
'---------------------------------------------------------------------------------------------------
'【処 理 名】初期処理
'【処理概要】初期処理を行う
'【引 数】[I]ByVal sh As Worksheet ワークシートオブジェクト
' [I]ByVal index As Long 読み込み対象行
'【返 却 値】なし
'---------------------------------------------------------------------------------------------------
Public Sub initBean(ByVal sh As Worksheet, ByVal index As Long)
'カテゴリー
category = sh.Cells(index, eCOL.COL_CATEGORY).value
'野菜
vegetable = sh.Cells(index, eCOL.COL_VEGETABLE).value
'くだもの
fruits = sh.Cells(index, eCOL.COL_FRUITS).value
'調味料
seasoning = sh.Cells(index, eCOL.COL_SEASONING).value
'お菓子
confection = sh.Cells(index, eCOL.COL_CONFECTION).value
End Sub
Option Explicit
Implements IAggregate
Const SH_NAME As String = "sheet2"
Private items As New Collection
Private sh As Worksheet 'ワークシートオブジェクト
'-------------------------------------------------------------------------------
' 【関 数 名】デストラクタ
' 【処理概要】終期化を行う
' 【引 数】なし
' 【戻 り 値】なし
'-------------------------------------------------------------------------------
Private Sub Class_Terminate()
Set sh = Nothing
End Sub
'-------------------------------------------------------------------------------
' 【関 数 名】イテレータ
' 【処理概要】イテレータ
' 【引 数】なし
' 【戻 り 値】イテレータ
'-------------------------------------------------------------------------------
Public Function IAggregate_iterator() As IIterator
Dim res As New SheetDataIterator
Call res.init(Me)
Set IAggregate_iterator = res
End Function
'-------------------------------------------------------------------------------
' 【関 数 名】要素数取得
' 【処理概要】要素数を取得する
' 【引 数】なし
' 【戻 り 値】要素数
'-------------------------------------------------------------------------------
Public Function getCount() As Integer
getCount = items.Count
End Function
'-------------------------------------------------------------------------------
' 【関 数 名】要素取得
' 【処理概要】要素を取得する
' 【引 数】[I]ByVal index As Integer 要素インデックス
' 【戻 り 値】要素
'-------------------------------------------------------------------------------
Public Function getItem(ByVal index As Integer) As SheetDataBean
Set getItem = items.Item(index)
End Function
'---------------------------------------------------------------------------------------------------
'【処 理 名】シート読込
'【処理概要】ワークシートのデータを取得する
'【引 数】
'【返 却 値】なし
'---------------------------------------------------------------------------------------------------
Public Sub loadSheet()
Dim sh As Worksheet
Dim lastRowIndex As Long '行インデックス
Dim index As Long '行カウンタ
Set sh = ThisWorkbook.Worksheets(SH_NAME)
lastRowIndex = sh.Cells(Rows.Count, "A").End(xlUp).Row
For index = 2 To lastRowIndex
Dim bean As New SheetDataBean
Call bean.initBean(sh, index)
items.Add Item:=bean
Set bean = Nothing
Next index
End Sub
Option Explicit
Implements IIterator
Private aggr As New SheetDataAggregate
Private index As Long
'-------------------------------------------------------------------------------
' 【関 数 名】次要素有無判定
' 【処理概要】次の要素があるか判定する
' 【引 数】なし
' 【戻 り 値】True 次要素あり
' False 次要素なし
'-------------------------------------------------------------------------------
Public Function IIterator_hasNext() As Boolean
If index <= aggr.getCount Then
IIterator_hasNext = True
Else
IIterator_hasNext = False
End If
End Function
'-------------------------------------------------------------------------------
' 【関 数 名】次要素取得
' 【処理概要】次の要素を取得する
' 【引 数】なし
' 【戻 り 値】次要素あり
'-------------------------------------------------------------------------------
Public Function IIterator_nextItem() As Object
Set IIterator_nextItem = aggr.getItem(index)
index = index + 1
End Function
'-------------------------------------------------------------------------------
' 【関 数 名】初期化
' 【処理概要】初期化を行う
' 【引 数】[I]ByVal aggregate As IAggregate 集合体
' 【戻 り 値】なし
'-------------------------------------------------------------------------------
Public Sub init(ByVal aggregate As IAggregate)
Set aggr = aggregate
index = 1
End Sub
使い方
Public Sub loadDataSheet2()
Dim aggr As New SheetDataAggregate
Dim bean As New SheetDataBean
Dim it As IIterator
aggr.loadSheet
Set it = aggr.IAggregate_iterator
Do While (it.hasNext)
Set bean = it.nextItem
Debug.Print bean.category
Loop
Set aggr = Nothing
Set it = Nothing
End Sub
最近のコメント



コメントを残す