2013-03-07 54 views
1

我已經構建了一個簡單的UserFormCombobox刪除行的名稱。使用UserForm刪除行

  • 我已經使用宏來構建所謂的「Combo_List」一 命名的區域填充Combobox行源。我靜態插入 rowsource屬性(意思是說,我沒有用代碼做到這一點)。
  • 另外在UserFormCheckbox我想用確認的目的。
  • 一旦他們從列表中選擇了用戶名,他們需要在Checkbox中進行檢查,然後他們可以單擊刪除按鈕。

我不知道如何編寫驗證Checkbox的代碼,然後刪除選定的行。

如果有幫助,下面是打造命名的區域代碼,然後顯示UserForm

Sub RecordDelete() 

Dim LastRow As Long 
ActiveSheet.Select 

LastRow = ActiveSheet.UsedRange.SpecialCells(xlCellTypeLastCell).Row 
Range("B3:B" & LastRow).Name = "Combo_List" 
frmDeleteData.Show 

End Sub 

複選框剛剛命名爲「checkbox1」。任何幫助?要做到這一點

+0

看到你的文件(santised如果需要的話)將使這很容易...... – brettdj 2013-03-07 23:41:38

+0

我以前從未使用過的Dropbox,所以讓我知道,如果你沒有得到文件。這裏的鏈接:https://www.dropbox.com/s/1bs3qlszorecdoh/Delete_Demo.xlsm – 2013-03-08 14:31:32

回答

0

一種方法是

  1. 在設計模式設置Enabled默認屬性爲您刪除CommandButtonFalse
  2. 將代碼添加到CheckBox能夠使CommandButtonCheckbox打勾
  3. 添加代碼到刪除CommandButton刪除整行

代碼

Private Sub CheckBox1_Click() 
CommandButton1.Enabled = CheckBox1.Value 
End Sub 

Private Sub CommandButton1_Click() 
    Dim rng1 As Range 
    If Me.CheckBox1 Then 
     Set rng1 = Range("Combo_List").Find(Me.ComboBox1.Value, , xlValues, xlWhole) 
     If Not rng1 Is Nothing Then 
      rng1.EntireRow.Delete 
     Else 
      MsgBox Me.ComboBox1.Value & "not found" 
     End If 
    Else 
    `redundant if button is disabled 
     MsgBox "Checkbox not checked!", vbCritical 
    End If 
End Sub 
+1

工程太棒了!謝謝。 – 2013-03-11 14:31:58