| 
    
     |  | いつもお世話になっております。 表の仕様(どこの列に何のデータが入っている等)の情報とプログラムを分離する為に下記の仕組みを考えました。
 チーム内に展開し使用したいのですが、あんまり自信ないので良くない箇所など後指摘頂ければ助かります。
 運用してしまってからだと変更工数が大きいので...
 
 背景:
 各種表(10数個)を元に色々な集計を行っています。
 何を集計するかは頻繁に変わります。
 
 目的:
 テーブルの使用をカプセル化し、既存のテーブルで
 新たな集計を行う際にはテーブルの仕様を考えずに
 コーディングしたい
 
 モジュールの構成
 ・試験用ルートモジュール
 テスト用のメインです。
 ・Rバグ票
 1レコードのクラスです。
 テーブルの仕様に依存します。
 ・Rバグ票data
 Rバグ票のクラス変数を入れる
 ・TblFRows
 ・クラス変数
 
 その他
 ・テーブル追加の作業が面倒です。もう少し減らしたい。
 ・実際には列の数は何十個かあり、20個程度を判定等に使用します。
 
 ^^^^^^^^^^^^^^^^^^^^^^^^ 試験用ルートモジュール
 Sub classDebug()
 Dim tbl As New TblFRows
 Dim rcd As New Rバグ票
 Dim s As Worksheet
 Dim i As Integer
 Dim cnt As Integer
 initClassData
 Set s = ThisWorkbook.Worksheets("Sheet1")
 tbl.init s.Range("C2")
 rcd.init tbl
 cnt = 0
 For i = 0 To tbl.nRows - 1
 rcd.idx = i
 If rcd.ランク = "A" Then cnt = cnt + 1
 Next
 MsgBox (cnt)
 End Sub
 
 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Rバグ票
 Private cData As Rバグ票data
 Public tbl As TblFRows
 Public idx As Integer
 
 Public Function 管理番号() As Range
 Dim i As Integer
 i = Me.idx
 Set 管理番号 = Me.tbl.sht.Cells(Me.tbl.getRecStartRow(Me.idx), クラス変数.gCD_Rバグ票.myCol管理番号 + Me.tbl.oriCol)
 End Function
 
 Public Function ランク() As Range
 Set ランク = Me.tbl.sht.Cells(Me.tbl.getRecStartRow(Me.idx), クラス変数.gCD_Rバグ票.myColランク + Me.tbl.oriCol)
 End Function
 
 Public Function init(ByRef t As TblFRows) As Boolean
 Dim myRngTitle As Range
 Dim r As Range
 
 Set Me.tbl = t
 Set r = t.sht.Cells(t.oriRow, t.oriCol)
 Set myRngTitle = t.sht.Range(r, r.Offset(0, t.nCols - 1))
 
 With クラス変数.gCD_Rバグ票
 If Not .idInited Then
 .myColランク = myRngTitle.Find("ランク", , , xlWhole).Column - Me.tbl.oriCol
 .myCol管理番号 = myRngTitle.Find("管理番号", , , xlWhole).Column - Me.tbl.oriCol
 .idInited = True
 .notNullCol = Me.管理番号.Column - Me.tbl.oriCol
 Me.tbl.adjstRowDown (Me.管理番号.Column)
 End If
 End With
 
 init = True
 End Function
 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Rバグ票data
 Public idInited As Boolean
 Public notNullCol As Integer
 Public myCol管理番号 As Integer
 Public myColランク As Integer
 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ TblFRows
 Public sht As Worksheet
 Public nRows As Integer
 Public nCols As Integer
 Public oriRow As Integer
 Public oriCol As Integer
 Public notNullCol As Integer
 Public rowsPerRec
 
 Public Function init(ByRef ori As Range) As Boolean
 Set Me.sht = ori.Worksheet
 Me.oriRow = ori.Row
 Me.oriCol = ori.Column
 Me.adjstCol
 Me.notNullCol = 0
 End Function
 
 Public Function adjstCol() As Boolean
 Dim r As Range
 Set r = Me.sht.Cells(oriRow, oriCol)
 Me.nCols = r.End(xlToRight).Column - oriCol + 1
 adjstCol = True
 End Function
 
 Public Function getRecStartRow(ByVal idx As Integer) As Integer
 getRecStartRow = Me.oriRow + (idx * Me.rowsPerRec) + 1
 End Function
 
 Public Function adjstRowDown(ByVal col As Integer) As Boolean
 Dim r As Range
 Set r = Me.sht.Cells(oriRow, col)
 Me.nRows = r.End(xlDown).Row - oriRow
 adjstRowDown = True
 End Function
 
 Public Function adjstRowUp(ByVal col As Integer) As Boolean
 Dim r As Range
 Set r = Me.sht.Cells(65536, col)
 Me.nRows = r.End(xlUp).Row - oriRow
 adjstRowUp = True
 End Function
 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ クラス変数
 Public gCD_Rバグ票 As Rバグ票data
 Public Sub initClassData()
 Set gCD_Rバグ票 = New Rバグ票data
 End Sub
 
 |  |