返回列表 上一主題 發帖

自動填滿選項按鈕

本帖最後由 GBKEE 於 2012-4-9 16:15 編輯

回復 24# caichen3
試試看
  1. Option Explicit
  2. Private Sub CommandButton4_Click()
  3.     Dim xR As Integer, Ar(1 To 5), xi As Integer, OB As OLEObject
  4.     With ActiveSheet
  5.         .CommandButton4.Placement = xlFreeFloating
  6.         xR = .Cells(Rows.Count, "A").End(xlUp).Row         'A欄最後有資料的列號
  7.         Ar(1) = xR & "非常不重要" & "(" & xR & ")"
  8.         Ar(2) = xR & "不重要" & "(" & xR & ")"
  9.         Ar(3) = xR & "普通" & "(" & xR & ")"
  10.         Ar(4) = xR & "重要" & "(" & xR & ")"
  11.         Ar(5) = xR & "非常重要" & "(" & xR & ")"
  12.         For xi = 1 To 5
  13.             With .Cells(xR + 1, "A").Offset(, xi + 2)      '以A欄為主   最後有資料的列號 + 1列的位置
  14.                 Set OB = ActiveSheet.OLEObjects.Add(ClassType:="Forms.OptionButton.1", Left:=.Left, Top:=.Top, Width:=.Width, Height:=.Height)
  15.                 OB.Object.Caption = Ar(xi)
  16.                 OB.Object.GroupName = "Row" & xR + 1   '對應列號
  17.             End With
  18.          Next
  19.         With .Range(.Cells(2, "A"), .Cells(xR + 1, "A")).Resize(, 8) 'A2:H & xR + 1
  20.             .Columns(1) = "=row()-1"                                 'A欄公式 依列號-1
  21.             .Columns(1) = .Columns(1).Value                           '將公式 轉成 值
  22.             .Columns(1).Interior.ColorIndex = 15
  23.             .Borders.LineStyle = xlContinuous
  24.             .Borders(xlEdgeBottom).Weight = xlThick
  25.             .Borders(xlEdgeRight).Weight = xlThick
  26.             .Borders(xlEdgeLeft).Weight = xlThick
  27.         End With
  28.     End With
  29. End Sub
  30. Private Sub CommandButton5_Click()
  31.     Dim xR As Integer, OB As OLEObject, Sp As Variant, MyStr As String
  32.     With ActiveSheet
  33.         .CommandButton5.Placement = xlFreeFloating
  34.          If ActiveCell.Row > .Cells(Rows.Count, "A").End(xlUp).Row Then Exit Sub  '不是範圍中
  35.         xR = ActiveCell.Row                                          '取得 作用儲存格的列號
  36.         For Each OB In ActiveSheet.OLEObjects
  37.             If OB.Name Like "OptionButton*" Then
  38.                 If OB.Object.GroupName = "Row" & xR Then OB.Delete  '刪除作用儲存格的列號 群組
  39.             End If
  40.         Next
  41.         .Cells(xR, "A").Resize(, 8).Delete xlUp                      '刪除作用儲存格 A欄到H欄
  42.         xR = .Cells(Rows.Count, "A").End(xlUp).Row
  43.         With .Range(.Cells(2, "A"), .Cells(xR, "A")).Resize(, 8)     'A2:H & xR :範圍中
  44.             .Columns(1) = "=row()-1"
  45.             .Columns(1) = .Columns(1).Value
  46.             .Columns(1).Interior.ColorIndex = 15
  47.             .Borders.LineStyle = xlContinuous
  48.             .Borders(xlEdgeBottom).Weight = xlThick
  49.             .Borders(xlEdgeRight).Weight = xlThick
  50.             .Borders(xlEdgeLeft).Weight = xlThick
  51.         End With
  52.         For Each OB In .OLEObjects                      '重新配置 OptionButton的文字 及 GroupName
  53.             If OB.Name Like "OptionButton*" Then
  54.                 Sp = Split(OB.TopLeftCell.Address(), "$")  '拆解 OptionButton 所在絕對位 置例: $D$5
  55.                 Select Case Sp(1)
  56.                     Case "D"
  57.                         MyStr = "非常不重要"
  58.                     Case "E"
  59.                         MyStr = "不重要"
  60.                     Case "F"
  61.                         MyStr = "普通"
  62.                     Case "G"
  63.                         MyStr = "重要"
  64.                     Case "H"
  65.                         MyStr = "非常重要"
  66.                 End Select
  67.                 OB.Object.Caption = Sp(2) - 1 & MyStr & "(" & Sp(2) - 1 & ")"
  68.                 OB.Object.GroupName = "Row" & Sp(2)    '對應列號
  69.             End If
  70.         Next
  71.     End With
  72. End Sub
複製代碼

TOP

        靜思自在 : 要用心,不要操心、煩心。
返回列表 上一主題