vba seminar banner 
    Home   >>   Sample   >>   Design Table
"テーブルデザイン変更"
"出力例"
サンプル01 : 1.Range("A1")からデータが、入力されている範囲まで表のデザインを変更する

サンプル02,03,04 : .選択した範囲の表のデザインを変更する [02 : 青 ,03 :  緑, 04 : 赤] (Office2003対応)
designtable01
"サンプルマクロ01"
"ヒント・解説01"
Public Sub designtable01() '青Ver.

Dim f As Integer, k As Integer, mrow As Integer
Dim CP As Variant, r As Variant, g As Variant, b As Variant
Dim ul As Range

CP = Array(52, 12, 43, 6, 36, 19, 27)
r = Array(219, 184, 149, 54, 36, 255, 255)
g = Array(229, 204, 179, 95, 63, 192, 255)
b = Array(241, 228, 215, 145, 96, 0, 255)

Set ul = Range("A1")

mrow = ul.CurrentRegion.rows.Count

For f = 0 To UBound(CP)

    ActiveWorkbook.Colors(CP(f)) = RGB(r(f), g(f), b(f)) 'カラーパレット変更

Next f

With Range("A1").CurrentRegion
    
    .Font.Color = RGB(0, 0, 0)
    .Font.Bold = True
    .Interior.Color = RGB(r(1), g(1), b(1))
    .HorizontalAlignment = xlCenter
    
    With ul.CurrentRegion.rows(1)
    
        .Font.Color = RGB(255, 255, 255)
        .Interior.Color = RGB(r(3), g(3), b(3))
                
    End With
                
End With

For k = 2 To mrow Step 2

    ul.CurrentRegion.rows(k).Interior.Color = RGB(255, 255, 255)

Next k

Set ul = Nothing

'ActiveWorkbook.ResetColors 'カラーパレットのリセット

End Sub
	
  ・指定したセルから連続して入力されている範囲のテーブルデザインを変更する。(青Ver.)

  1. CurrentRegionプロパティを用いて、入力されているセルの範囲を調べ、最終行を取得する。

  2. Withステートメントを用いてセルのプロパティを変更する。

  3. WorkbookのColorsプロパティを変更して、カラーパレットを変更する。

 
"サンプルマクロ02"
"ヒント・解説02"
Public Sub designtable02() '青Ver.

Dim f As Integer, k As Integer, mrow As Integer
Dim CP As Variant, r As Variant, g As Variant, b As Variant

mrow = Selection.rows.Count

CP = Array(52, 12, 43, 6, 36, 19, 27)
r = Array(219, 184, 149, 54, 36, 255, 255)
g = Array(229, 204, 179, 95, 63, 192, 255)
b = Array(241, 228, 215, 145, 96, 0, 255)

For f = 0 To UBound(CP)

    ActiveWorkbook.Colors(CP(f)) = RGB(r(f), g(f), b(f)) 'カラーパレット変更

Next f

With Selection
    
    .Font.Color = RGB(0, 0, 0)
    .Font.Bold = True
    .Interior.Color = RGB(r(1), g(1), b(1))
    .HorizontalAlignment = xlCenter
    
    With Selection.rows(1)
    
        .Font.Color = RGB(255, 255, 255)
        .Interior.Color = RGB(r(3), g(3), b(3))
                
    End With
                
End With

For k = 2 To mrow Step 2

    Selection.rows(k).Interior.Color = RGB(255, 255, 255)

Next k

'ActiveWorkbook.ResetColors 'カラーパレットのリセット

End Sub
	
  ・選択している範囲のデザインを変更する。(青Ver.)

  1. Selectionプロパティを用いて選択範囲を取得する。

  2. Withステートメントを用いてセルのプロパティを変更する。

  3. WorkbookのColorsプロパティを変更して、カラーパレットを変更する。
"サンプルマクロ03"
"ヒント・解説03"
Public Sub designtable03() '緑Ver.

Dim f As Integer, k As Integer, mrow As Integer
Dim CP As Variant, r As Variant, g As Variant, b As Variant

mrow = Selection.rows.Count

CP = Array(49, 14, 42, 8, 34, 21, 29)
r = Array(234, 214, 194, 118, 78, 146, 165)
g = Array(241, 227, 214, 146, 97, 208, 165)
b = Array(221, 188, 155, 60, 40, 80, 165)

For f = 0 To UBound(CP)

    ActiveWorkbook.Colors(CP(f)) = RGB(r(f), g(f), b(f)) 'カラーパレット変更

Next f

With Selection
    
    .Font.Color = RGB(0, 0, 0)
    .Font.Bold = True
    .Interior.Color = RGB(r(1), g(1), b(1))
    .HorizontalAlignment = xlCenter
    
    With Selection.rows(1)
    
        .Font.Color = RGB(255, 255, 255)
        .Interior.Color = RGB(r(3), g(3), b(3))
                
    End With
                
End With

For k = 2 To mrow Step 2

    Selection.rows(k).Interior.Color = RGB(255, 255, 255)

Next k

'ActiveWorkbook.ResetColors 'カラーパレットのリセット

End Sub
	
  ・選択している範囲のデザインを変更する。(緑Ver.)

  1. Selectionプロパティを用いて選択範囲を取得する。

  2. Withステートメントを用いてセルのプロパティを変更する。

  3. WorkbookのColorsプロパティを変更して、カラーパレットを変更する。
"サンプルマクロ04"
"ヒント・解説04"
Public Sub designtable04() '赤Ver.

Dim f As Integer, k As Integer, mrow As Integer
Dim CP As Variant, r As Variant, g As Variant, b As Variant

mrow = Selection.rows.Count

CP = Array(51, 10, 50, 4, 35, 20, 28)
r = Array(242, 229, 217, 148, 98, 255, 216)
g = Array(219, 184, 149, 54, 36, 255, 216)
b = Array(219, 183, 148, 52, 35, 0, 216)

For f = 0 To UBound(CP)

    ActiveWorkbook.Colors(CP(f)) = RGB(r(f), g(f), b(f)) 'カラーパレット変更

Next f

With Selection
    
    .Font.Color = RGB(0, 0, 0)
    .Font.Bold = True
    .Interior.Color = RGB(r(1), g(1), b(1))
    .HorizontalAlignment = xlCenter
    
    With Selection.rows(1)
    
        .Font.Color = RGB(255, 255, 255)
        .Interior.Color = RGB(r(3), g(3), b(3))
                
    End With
                
End With

For k = 2 To mrow Step 2

    Selection.rows(k).Interior.Color = RGB(255, 255, 255)

Next k

'ActiveWorkbook.ResetColors 'カラーパレットのリセット

End Sub
	
  ・選択している範囲のデザインを変更する。(赤Ver.)

  1. Selectionプロパティを用いて選択範囲を取得する。

  2. Withステートメントを用いてセルのプロパティを変更する。

  3. WorkbookのColorsプロパティを変更して、カラーパレットを変更する。