返回列表 上一主題 發帖

[發問] 如何刪除控制項

這是我的檔案,請大大幫我看一下,感謝:)
寶寶

TOP

回復 11# caichen3
你要刪除的是選取儲存格那一列的群組嗎?
For Each ob In ActiveSheet.OLEObjects
  If ob.Name Like "OptionButton*" Then
    If ob.TopLeftCell.Row = gyou Then ob.Delete
  End If
Next
Cells(gyou, 1).Resize(, 8).Delete xlShiftUp
學海無涯_不恥下問

TOP

本帖最後由 caichen3 於 2012-3-28 12:31 編輯

嗯,我要刪除的是當時選取儲存格的那列(gyou)
為什麼我選取編號4按下刪除會同時將編號4、5一起刪除呢?
只要是選取倒數第二列刪除,就會將最後二列的編號、需求陳述刪除,但還是會剩最後一列的選項按鈕
寶寶

TOP

本帖最後由 GBKEE 於 2012-3-28 15:53 編輯

回復 13# caichen3
為什麼我選取編號4按下刪除會同時將編號4、5一起刪除呢?幫你查看控制項的群組如下 GroupName=5 有10個OptionButton控制項
請你先用 Ex 的程式調整在同一列上的OptionButton  用列號設定為同一群組
用 Ex 的程式調整後再執行你的Private Sub CommandButton5_Click()
  1. OptionButton2  GroupName=1
  2. OptionButton1  GroupName=1
  3. OptionButton4  GroupName=1
  4. OptionButton3  GroupName=1
  5. OptionButton5  GroupName=1
  6. OptionButton11  GroupName=2
  7. OptionButton6  GroupName=2
  8. OptionButton7  GroupName=2
  9. OptionButton8  GroupName=2
  10. OptionButton9  GroupName=2
  11. OptionButton10  GroupName=3
  12. OptionButton13  GroupName=3
  13. OptionButton12  GroupName=3
  14. OptionButton17  GroupName=3
  15. OptionButton14  GroupName=3
  16. OptionButton15  GroupName=4
  17. OptionButton16  GroupName=4
  18. OptionButton18  GroupName=4
  19. OptionButton20  GroupName=4
  20. OptionButton19  GroupName=4
  21. OptionButton21  GroupName=5
  22. OptionButton22  GroupName=5
  23. OptionButton23  GroupName=5
  24. OptionButton26  GroupName=5
  25. OptionButton24  GroupName=5
  26. OptionButton25  GroupName=5
  27. OptionButton27  GroupName=5
  28. OptionButton28  GroupName=5
  29. OptionButton29  GroupName=5
  30. OptionButton30  GroupName=5
  31. OptionButton31  GroupName=6
  32. OptionButton32  GroupName=6
  33. OptionButton33  GroupName=6
  34. OptionButton34  GroupName=6
  35. OptionButton35  GroupName=6
  36. Sub Ex()  '在同一列上的OptionButton 以列號設定為同一群組
  37. Dim ob As OLEObject
  38. For Each ob In ActiveSheet.OLEObjects
  39.   If ob.Name Like "OptionButton*" Then   '
  40.     ob.Object.GroupName = ob.TopLeftCell.Row
  41.   End If
  42. Next
  43. End Sub
複製代碼
執行的Private Sub CommandButton5_Click() 須修改如下
  1. Private Sub CommandButton5_Click()
  2. Dim yyy As String
  3. Dim XXX As String
  4. Dim k As Integer
  5. Dim ob As Object
  6. gyou=Selection.Row
  7. ActiveSheet.Rows(gyou).ClearContents
  8. Range("I1").Value = Range("I1").Value - 1
  9. TMMPA = Range("I1").Value
  10. aaa = Chr(65)
  11. ccc = Chr(67)
  12. For I = 2 To 100
  13. XXX = I
  14. Range(aaa + XXX).Value = ("")
  15. Range(ccc + XXX).Value = ("")
  16. Next
  17. ActiveSheet.Range("A2:H200").Borders.LineStyle = XILinestyleNone
  18. ActiveSheet.Range("A2:H200").Interior.ColorIndex = xlColorIndexNone

  19. For R = 1 To TMMPA
  20. yyy = R + 1
  21. Range(aaa + yyy).Value = R
  22. Range(ccc + yyy).Value = R
  23. Next
  24. ActiveSheet.Range("A2:H" + yyy).Borders.LineStyle = xlContinuous
  25. ActiveSheet.Range("A2:H" + yyy).Borders(xlEdgeBottom).Weight = xlThick
  26. ActiveSheet.Range("A2:H" + yyy).Borders(xlEdgeRight).Weight = xlThick
  27. ActiveSheet.Range("A2:H" + yyy).Borders(xlEdgeLeft).Weight = xlThick
  28. ActiveSheet.Range("A2:A" + yyy).Interior.ColorIndex = 15
  29. For Each ob In ActiveSheet.OLEObjects
  30.   If ob.Name Like "OptionButton*" Then
  31.     If ob.Object.GroupName = Selection.Row Then ob.Delete
  32.   End If
  33. Next
  34. Selection.EntireRow.Delete   '刪除整列
  35. Ex        '***  再度的 將 在同一列上的OptionButton  用 [列號]設定為同一群組
  36. End Sub
複製代碼

TOP

本帖最後由 caichen3 於 2012-3-28 22:04 編輯

請問大大:
用 Ex 的程式調整後再執行你的Private Sub CommandButton5_Click()   EX( )這段程式會該放在哪裡呢?是Private Sub CommandButton5_Click()裡面一開始的地方嗎?

接下來在Private Sub CommandButton5_Click()程式最後面的地方在執行一次EX( ) 這段程式嗎??

我嘗試放在CommandButton5_Click()程式碼前與後面,還是無法可以幫我看一下嗎,感謝:)
Private Sub CommandButton5_Click()
Dim yyy As String
Dim xxx As String
Dim k As Integer
Dim ob As OLEObject
gyou = Selection.Row
ActiveSheet.Rows(gyou).ClearContents
Range("I1").Value = Range("I1").Value - 1
TMMPA = Range("I1").Value
aaa = Chr(65)
ccc = Chr(67)

For I = 2 To 100
xxx = I
Range(aaa + xxx).Value = ("")
Range(ccc + xxx).Value = ("")
Next
ActiveSheet.Range("A2:H200").Borders.LineStyle = XILinestyleNone
ActiveSheet.Range("A2:H200").Interior.ColorIndex = xlColorIndexNone

For R = 1 To TMMPA
yyy = R + 1
Range(aaa + yyy).Value = R
Range(ccc + yyy).Value = R
Next
ActiveSheet.Range("A2:H" + yyy).Borders.LineStyle = xlContinuous
ActiveSheet.Range("A2:H" + yyy).Borders(xlEdgeBottom).Weight = xlThick
ActiveSheet.Range("A2:H" + yyy).Borders(xlEdgeRight).Weight = xlThick
ActiveSheet.Range("A2:H" + yyy).Borders(xlEdgeLeft).Weight = xlThick
ActiveSheet.Range("A2:A" + yyy).Interior.ColorIndex = 15

k = gyou - 1
For Each ob In ActiveSheet.OLEObjects
If ob.Name Like "OptionButton*" Then
    If ob.Object.GroupName = k Then ob.Delete
  End If
Next
Cells(gyou, 1).Resize(, 8).Delete xlShiftUp

For Each ob In ActiveSheet.OLEObjects
    If ob.Name Like "OptionButton*" Then
    ob.Object.GroupName = ob.TopLeftCell.Row
    End If
Next

End Sub
寶寶

TOP

回復 13# caichen3

play.gif
學海無涯_不恥下問

TOP

本帖最後由 caichen3 於 2012-3-29 12:12 編輯

感謝大大的回覆:victory:
gyou = Selection.Row    ' gyou表示所在列
If ob.TopLeftCell.Row = gyou Then ob.Delete   ' 這行程式是說明若ob左上儲存格的行值等於gyou即刪除嗎??
topLeftCell.Row 是左上單元格的行值,該如何辦識topleftcell.row的所在位置呢??
寶寶

TOP

回復 17# caichen3

是的,以選項按紐位置做判斷
所以你說會有重複刪除的狀況,我就不知道你是怎麼做的
學海無涯_不恥下問

TOP

若我Selection.Row在第3列,請問topleftcell.row在哪個位置呢??

123.JPG (7.97 KB)

123.JPG

寶寶

TOP

回復 19# caichen3
看動畫
按鈕放在第4列就是4
第5列就是5
play.gif
學海無涯_不恥下問

TOP

        靜思自在 : 有多少力量就做多少事,不要心存等待,等待才會落空。
返回列表 上一主題