返回列表 上一主題 發帖

[發問] 請教如何簡化指令及在如何一般模組中使用

[發問] 請教如何簡化指令及在如何一般模組中使用

請教各為先進, 小弟程式碼如下, 原本小弟想使用For.....Next迴圈來判斷C欄是否有資料並控制 CommandButton顯示與否
但是卡在 控制項物件的部份, 小弟不會使用For...Next來寫...再請各位先進指導一下.

另外因所有的sheet都會用到該程式, 因此小弟新增於一般模組, 可是每當執行後, 會出現"此處需要物件"的錯誤訊息
Excel的說明說我少了指定的定位項, 小弟不太瞭解定位項的意思以及該放在那邊...有勞各位先進開導, 謝謝!!
  1. Sub RemoveButton()

  2. If [C3] <> "" Then
  3.     CommandButton2.Visible = True
  4.     Else
  5.     CommandButton2.Visible = False
  6. End If

  7. If [C4] <> "" Then
  8.     CommandButton3.Visible = True
  9.     Else
  10.     CommandButton3.Visible = False
  11. End If

  12. If [C5] <> "" Then
  13.     CommandButton4.Visible = True
  14.     Else
  15.     CommandButton4.Visible = False
  16. End If

  17. If [C6] <> "" Then
  18.     CommandButton5.Visible = True
  19.     Else
  20.     CommandButton5.Visible = False
  21. End If

  22. If [C7] <> "" Then
  23.     CommandButton6.Visible = True
  24.     Else
  25.     CommandButton6.Visible = False
  26. End If

  27. If [C8] <> "" Then
  28.     CommandButton7.Visible = True
  29.     Else
  30.     CommandButton7.Visible = False
  31. End If

  32. If [C10] <> "" Then
  33.     CommandButton8.Visible = True
  34.     Else
  35.     CommandButton8.Visible = False
  36. End If

  37. If [C11] <> "" Then
  38.     CommandButton9.Visible = True
  39.     Else
  40.     CommandButton9.Visible = False
  41. End If

  42. If [C12] <> "" Then
  43.     CommandButton10.Visible = True
  44.     Else
  45.     CommandButton10.Visible = False
  46. End If

  47. If [C13] <> "" Then
  48.     CommandButton11.Visible = True
  49.     Else
  50.     CommandButton11.Visible = False
  51. End If

  52. If [C14] <> "" Then
  53.     CommandButton12.Visible = True
  54.     Else
  55.     CommandButton12.Visible = False
  56. End If

  57. If [C15] <> "" Then
  58.     CommandButton13.Visible = True
  59.     Else
  60.     CommandButton13.Visible = False
  61. End If

  62. If [C16] <> "" Then
  63.     CommandButton14.Visible = True
  64.     Else
  65.     CommandButton14.Visible = False
  66. End If

  67. If [C17] <> "" Then
  68.     CommandButton15.Visible = True
  69.     Else
  70.     CommandButton15.Visible = False
  71. End If

  72. End Sub
複製代碼

回復 11# GBKEE

GBKEE版大,
真的太感謝您了, 您真的是神人
謝謝!!

TOP

回復 10# dino1978
OLEObjects("CheckBox" & i).Object.Value = False

TOP

回復 9# GBKEE

GBKEE 版大,
   我的意思是, 我想在Workbook開啟後, 自動將所有的CheckBox控制項取消核取,
所以我才會把程式改成 .Value=False,  不知這樣可不可行嗎?

TOP

回復 8# dino1978
.OLEObjects("CheckBox" & i).Value = False
改成
.OLEObjects("CheckBox" & i).Visible = False

TOP

回復 7# GBKEE

GBKEE 版大
   不好意思, 我太豬頭了, 我忘了將舊的模組移除造成衝突, 現在問題已經解決了
太感謝您了~整個看起來就清爽多了!!

另外想請教的是 CheckBox 控制項是否也能夠像 CommandButton一樣用 OLEobjects的方式來做Loop呢?
我有自己小試了一下, 會出現 "物件不支援此屬性或方法"
是我的語法錯誤還是CheckBox控制項不能這樣使用?
  1. Private Sub Worksheet_SelectionChange(ByVal Target As Range)

  2. Dim i As Integer

  3. i = 1

  4. With Sheet1
  5. .OLEObjects("CheckBox" & i).Value = False
  6. End With

  7. End Sub
複製代碼

TOP

回復 6# dino1978
請上傳你修改後的檔案看看

TOP

本帖最後由 dino1978 於 2012-1-28 06:04 編輯

回復 5# GBKEE


    GBKEE版大,
   原來還有這招可以用, 真的是學到了
   不過我將該程式碼應用在上面的時候, 發生如下圖的錯誤訊息,
   看了老半天還是不知道問題出在哪, 我並沒有動到其他的欄位
   但執行後就出現了這個錯誤訊息, 再請您幫我確認一下, 謝謝您
   (若改回原來的方式, 就不會出現這個問題了, 不太理解.....)
擷取1.JPG

TOP

回復 4# dino1978
還有可簡化
  1. Private Sub CommandButton10_Click()
  2.     ASlotRemove [C12]   '傳送參數MyText : [C12]
  3. End Sub
  4. Private Sub CommandButton11_Click()
  5.     ASlotRemove [C13]   '傳送參數MyText : [C13]
  6. End Sub
  7. Private Sub CommandButton12_Click()
  8.     ASlotRemove [C14]    '傳送參數MyText : [C14]
  9. End Sub
複製代碼
  1. Sub SlotRemove(ByVal MyText As Range)   '接收參數 MyText
  2.     Application.ScreenUpdating = False
  3.     Dim Rng As Range, F_Address As String, TheFind As String, a As Range
  4.     'MyText = [C12] ' Trim(InputBox("請輸入要尋找的值:"))
  5.     If MyText <> "" Then
  6.         Set Rng = Sheet3.Range("A1").CurrentRegion.EntireColumn.Find(MyText, lookat:=xlWhole)
  7.         If Not Rng Is Nothing Then
  8.             F_Address = Rng.Address
  9.             Do
  10.                 TheFind = IIf(TheFind = "", Rng.Address(0, 0), TheFind & "   " & Rng.Address(0, 0))
  11.                 Set Rng = Sheet3.Range("A1").CurrentRegion.EntireColumn.FindNext(Rng)
  12.             Loop Until F_Address = Rng.Address
  13.         End If
  14.         Else: GoTo err2
  15.     End If
  16.     If TheFind <> "" Then
  17.         If Rng Is Nothing Then
  18.             GoTo err
  19.         Else
  20.           Sheet2.Range("A65536").End(xlUp).Offset(1) = Rng
  21.             With Rng.Resize(1, 3)
  22.                 .Delete xlShiftUp
  23.             End With
  24.             With Range("B12")
  25.                 .Resize(1, 2).ClearContents
  26.                 .ClearContents
  27.                 .Offset(, 1).Validation.Delete
  28.             End With
  29.         End If
  30.     Else
  31. err:
  32.         MsgBox "沒有找到  " & MyText
  33. err2:
  34.     End If
  35.     TheFind = ""
  36.     Application.ScreenUpdating = True
  37. End Sub
複製代碼

TOP

TR6850 configure.zip (310.03 KB)
回復 2# Hsieh
超版大大, 很抱歉這麼晚才回覆您, 我的附檔如附件, 已參照GBKEE大大的指令稍作修改完成, 謝謝您的協助

回復 3# GBKEE
GBKEE 版大, 謝謝您的幫忙, 程式照您的方法稍作修改已經可以使用  謝謝

並祝你們新春愉快!!

TOP

        靜思自在 : 能幹不幹,不如苦幹實幹。
返回列表 上一主題