Excel VBA我正在尝试删除Excel中的记录,但代码不起作用

Excel VBA我正在尝试删除Excel中的记录,但代码不起作用,excel,vba,Excel,Vba,我试图在excel VBA中创建一个访客日志程序,但我很难完成最后一部分,因为我无法删除任何记录 我将感谢任何帮助,以及在编码,我不是一个程序员,我只是组装代码位在这里和那里从谷歌。提前谢谢 MyForm访客日志 Excel数据表 选项显式 私有子组件(更改) 端接头 私人子组合箱室_Change() 端接头 私有子组合关系_Change() 端接头 专用子框架1_单击() 端接头 私有子DTPickerSearch_CallbackKeyDown(ByVal KeyCode为整数,ByVa

我试图在excel VBA中创建一个访客日志程序,但我很难完成最后一部分,因为我无法删除任何记录

我将感谢任何帮助,以及在编码,我不是一个程序员,我只是组装代码位在这里和那里从谷歌。提前谢谢

MyForm访客日志

Excel数据表

选项显式
私有子组件(更改)
端接头
私人子组合箱室_Change()
端接头
私有子组合关系_Change()
端接头
专用子框架1_单击()
端接头
私有子DTPickerSearch_CallbackKeyDown(ByVal KeyCode为整数,ByVal Shift为整数,ByVal CallbackField为字符串,CallbackDate为日期)
端接头
私有子列表框打印输出\单击()
端接头
专用子搜索按钮\u单击()
调用刷新数据
ActiveWorkbook.RefreshAll
'范围(“K2”)。排序键1:=范围(“K2”),顺序1:=xl降序,标题:=xlYes
端接头
私有子文本框iqama_Change()
Dim NotNumeric作为布尔值
将值设置为字符串
如果不是数字(TextBoxIqama.Value),则
如果文本为“boxikama”,则
NotNumeric=TRUE
MsgBox“仅允许数字”
TempValue=Left(Me.TextBoxIqama.Value,Len(Me.TextBoxIqama.Value)-1)
而NotNumeric=TRUE和TempValue“”
如果不是数字(TempValue),则
TempValue=左(TempValue,Len(TempValue)-1)
其他的
NotNumeric=FALSE
如果结束
温德
Me.TextBoxIqama.Value=TempValue
如果结束
如果结束
端接头
专用子文本框VisitDate_Change()
TextBoxVisitDate=格式(TextBoxVisitDate.Value,“DD/MM/YYYY”)
端接头
私有子文本框vname_Change()
端接头
专用子时间单击按钮()
如果Me.TimeBoxOut.Value=”“,则
TimeBoxOut.Value=Now
TimeBoxOut=格式(TimeBoxOut.Value,“HH:MM:SS AM/PM”)
出口接头
如果结束
'=================================验证=========================
如果不是空的(Me.TimeBoxOut.Value),那么
调暗输出时间为整数
outtime=MsgBox(“是否继续?”,vbQuestion+vbYesNo)
如果outtime=vbYes,则
TimeBoxOut.Value=Now
TimeBoxOut=格式(TimeBoxOut.Value,“HH:MM:SS AM/PM”)
其他的
'如果单击“否”,则不执行任何操作
如果结束
出口接头
如果结束
端接头
'==================================添加按钮=========================
私有子命令按钮添加单击()
将sh设置为工作表
Set sh=ThisWorkbook.Sheets(“数据库”)
最后一排的颜色和前面一样长
最后一行=Application.WorksheetFunction.CountA(sh.Range(“A:A”))
'=================================验证=========================
如果Me.TextBoxVName.Value=”“,则
MsgBox“请输入患者姓名”,vbCritical
出口接头
如果结束
如果Me.ComboboxNational.Value=”“,则
MsgBox“请输入访客国籍”,请输入
出口接头
如果结束
如果Me.TextBoxPName.Value=”“,则
MsgBox“请输入地址”,VBC
出口接头
如果结束
如果Me.TextBoxIqama.Value=”“,则
MsgBox“请输入手机号码”,VBC
出口接头
如果结束
如果Me.ComboBoxRoom.Value=”“,则
MsgBox“请选择病房”,vbCritical
出口接头
如果结束
如果Me.ComboBoxRelationship.Value=”“,则
MsgBox“请选择访客与患者的关系”,vbCritical
出口接头
如果结束
'===================================================
sh.Range(“A”和最后一行+1).Value=“=IF(B)和最后一行+1&=”,“”,Row()-1)”
sh.Range(“B”和最后一行+1).Value=Me.TextBoxVName.Value
sh.Range(“C”和最后一行+1).Value=Me.comboboxNational.Value
sh.Range(“D”和最后一行+1).Value=Me.TextBoxPName.Value
sh.Range(“E”和最后一行+1).Value=Me.TextBoxIqama.Value
sh.Range(“F”和最后一行+1).Value=Me.ComboBoxRoom.Value
sh.Range(“G”和最后一行+1).Value=Me.TimeBoxIn.Value
sh.Range(“H”和最后一行+1).Value=Me.TimeBoxOut.Value
sh.Range(“I”和最后一行+1).Value=Me.TextBoxDuration.Value
sh.Range(“J”和最后一行+1).Value=Me.ComboBoxRelationship.Value
sh.Range(“K”和最后一行+1)。值=现在
'范围(“K2”)。排序键1:=范围(“K2”),顺序1:=xl降序,标题:=xlYes
Me.TextBoxVName.Value=“”
Me.comboboxnational.Value=“”
Me.TextBoxPName.Value=“”
Me.TextBoxIqama.Value=“”
Me.TextBox4.Value=“”
Me.TimeBoxIn.Value=“”
Me.TimeBoxOut.Value=“”
Me.ComboBoxRoom.Value=“”
Me.ComboBoxRelationship.Value=“”
调用刷新数据
端接头
'==================================更新记录=========================
私有子命令按钮更新_单击()
如果Me.TextBox4.Value=”“,则
MsgBox“请选择要更新的记录”,vbCritical
出口接头
如果结束
尺寸a为日期,b为日期
a=Me.TimeBoxIn.Value
如果Me.TimeBoxOut.Value=”“,则
'如果单击“否”,则不执行任何操作
其他的
b=Me.TimeBoxOut.Value
TextBoxDuration.Value=b-a
TextBoxDuration=格式(Me.TextBoxDuration.Value,“hh:mm:ss”)
如果结束
将sh设置为工作表
Set sh=ThisWorkbook.Sheets(“数据库”)
将选定的行变暗为长
选定的行=Application.WorksheetFunction.Match(Me.ListBox1.List(Me.ListBox1.ListIndex,0),sh.Range(“A:A”),0)
'=================================验证=
Option Explicit

Private Sub ComboBoxNationality_Change()
    
End Sub

Private Sub ComboBoxRoom_Change()
    
End Sub

Private Sub ComboBoxRelationship_Change()
    
End Sub

Private Sub Frame1_Click()
    
End Sub

Private Sub DTPickerSearch_CallbackKeyDown(ByVal KeyCode As Integer, ByVal Shift As Integer, ByVal CallbackField As String, CallbackDate As Date)
    
End Sub

Private Sub ListBoxPrintout_Click()
    
End Sub

Private Sub SearchButton_Click()
    
    Call Refresh_Data
    ActiveWorkbook.RefreshAll
    'Range("K2").Sort Key1:=Range("K2"), Order1:=xlDescending, Header:=xlYes
    
End Sub

Private Sub TextBoxIqama_Change()
    
    Dim NotNumeric  As Boolean
    Dim TempValue   As String
    
    If Not IsNumeric(TextBoxIqama.Value) Then
        
        If TextBoxIqama <> "" Then
            
            NotNumeric = TRUE
            
            MsgBox "Only numbers allowed"
            
            TempValue = Left(Me.TextBoxIqama.Value, Len(Me.TextBoxIqama.Value) - 1)
            
            While NotNumeric = TRUE And TempValue <> ""
                
                If Not IsNumeric(TempValue) Then
                    
                    TempValue = Left(TempValue, Len(TempValue) - 1)
                    
                Else
                    
                    NotNumeric = FALSE
                    
                End If
                
            Wend
            
            Me.TextBoxIqama.Value = TempValue
            
        End If
        
    End If
    
End Sub

Private Sub TextBoxVisitDate_Change()
    TextBoxVisitDate = Format(TextBoxVisitDate.Value, "DD/MM/YYYY")
End Sub

Private Sub TextBoxVName_Change()
    
End Sub

Private Sub TimeOutButton_Click()
    If Me.TimeBoxOut.Value = "" Then
        TimeBoxOut.Value = Now
        TimeBoxOut = Format(TimeBoxOut.Value, "HH:MM:SS AM/PM")
        Exit Sub
    End If
    
    '======================= Validation =========================
    
    If Not IsEmpty(Me.TimeBoxOut.Value) Then
        Dim outtime As Integer
        outtime = MsgBox("Do you want To Continue?", vbQuestion + vbYesNo)
        
        If outtime = vbYes Then
            TimeBoxOut.Value = Now
            TimeBoxOut = Format(TimeBoxOut.Value, "HH:MM:SS AM/PM")
        Else
            'if clicked No do nothing
        End If
        Exit Sub
    End If
    
End Sub

'======================= Add Button =========================
Private Sub CommandButtonAdd_Click()
    
    Dim sh          As Worksheet
    Set sh = ThisWorkbook.Sheets("Database")
    
    Dim Last_Row    As Long
    Last_Row = Application.WorksheetFunction.CountA(sh.Range("A:A"))
    
    '======================= Validation =========================
    
    If Me.TextBoxVName.Value = "" Then
        MsgBox "Please enter the patient name", vbCritical
        Exit Sub
    End If
    
    If Me.ComboBoxNationality.Value = "" Then
        MsgBox "Please enter the Visitor Nationality", vbCritical
        Exit Sub
    End If
    
    If Me.TextBoxPName.Value = "" Then
        MsgBox "Please enter the Address", vbCritical
        Exit Sub
    End If
    
    If Me.TextBoxIqama.Value = "" Then
        MsgBox "Please enter the Mobile Number", vbCritical
        Exit Sub
    End If
    
    If Me.ComboBoxRoom.Value = "" Then
        MsgBox "Please Select the Patient Room", vbCritical
        Exit Sub
    End If
    
    If Me.ComboBoxRelationship.Value = "" Then
        MsgBox "Please Select the Visitor relationship With Patient", vbCritical
        Exit Sub
    End If
    
    '===================================================
    
    sh.Range("A" & Last_Row + 1).Value = "=IF(B" & Last_Row + 1 & "="""","""",ROW()-1)"
    sh.Range("B" & Last_Row + 1).Value = Me.TextBoxVName.Value
    sh.Range("C" & Last_Row + 1).Value = Me.ComboBoxNationality.Value
    sh.Range("D" & Last_Row + 1).Value = Me.TextBoxPName.Value
    sh.Range("E" & Last_Row + 1).Value = Me.TextBoxIqama.Value
    sh.Range("F" & Last_Row + 1).Value = Me.ComboBoxRoom.Value
    sh.Range("G" & Last_Row + 1).Value = Me.TimeBoxIn.Value
    sh.Range("H" & Last_Row + 1).Value = Me.TimeBoxOut.Value
    sh.Range("I" & Last_Row + 1).Value = Me.TextBoxDuration.Value
    sh.Range("J" & Last_Row + 1).Value = Me.ComboBoxRelationship.Value
    sh.Range("K" & Last_Row + 1).Value = Now
    'Range("K2").Sort Key1:=Range("K2"), Order1:=xlDescending, Header:=xlYes
    
    Me.TextBoxVName.Value = ""
    Me.ComboBoxNationality.Value = ""
    Me.TextBoxPName.Value = ""
    Me.TextBoxIqama.Value = ""
    Me.TextBox4.Value = ""
    Me.TimeBoxIn.Value = ""
    Me.TimeBoxOut.Value = ""
    Me.ComboBoxRoom.Value = ""
    Me.ComboBoxRelationship.Value = ""
    
    Call Refresh_Data
    
End Sub
'======================= Update Record =========================

Private Sub CommandButtonUpdate_Click()
    
    If Me.TextBox4.Value = "" Then
        MsgBox "Please Select a record To update", vbCritical
        Exit Sub
    End If
    
    Dim a           As Date, b As Date
    a = Me.TimeBoxIn.Value
    If Me.TimeBoxOut.Value = "" Then
        'if clicked No do nothing
    Else
        b = Me.TimeBoxOut.Value
        TextBoxDuration.Value = b - a
        TextBoxDuration = Format(Me.TextBoxDuration.Value, "hh:mm:ss")
    End If
    
    Dim sh          As Worksheet
    Set sh = ThisWorkbook.Sheets("Database")
    
    Dim Selected_Row As Long
    Selected_Row = Application.WorksheetFunction.Match(Me.ListBox1.List(Me.ListBox1.ListIndex, 0), sh.Range("A:A"), 0)
    
    '======================= Validation =========================
    
    If Me.TextBoxVName.Value = "" Then
        MsgBox "Please enter the patient Name", vbCritical
        Exit Sub
    End If
    
    If Me.ComboBoxNationality.Value = "" Then
        MsgBox "Please enter the Visitor Nationality", vbCritical
        Exit Sub
    End If
    
    If Me.TextBoxPName.Value = "" Then
        MsgBox "Please enter the Address", vbCritical
        Exit Sub
    End If
    
    If Me.TextBoxIqama.Value = "" Then
        MsgBox "Please enter the Mobile Number", vbCritical
        Exit Sub
    End If
    
    If Me.ComboBoxRoom.Value = "" Then
        MsgBox "Please Select the Patient room", vbCritical
        Exit Sub
    End If
    
    If Me.ComboBoxRelationship.Value = "" Then
        MsgBox "Please Select the Visitor relationship With Patient", vbCritical
        Exit Sub
    End If
    
    '========================= updating the values in the Database sheet ==========================
    
    sh.Range("B" & Selected_Row).Value = Me.TextBoxVName.Value
    sh.Range("C" & Selected_Row).Value = Me.ComboBoxNationality.Value
    sh.Range("D" & Selected_Row).Value = Me.TextBoxPName.Value
    sh.Range("E" & Selected_Row).Value = Me.TextBoxIqama.Value
    sh.Range("F" & Selected_Row).Value = Me.ComboBoxRoom.Value
    sh.Range("G" & Selected_Row).Value = Me.TimeBoxIn.Value
    sh.Range("H" & Selected_Row).Value = Me.TimeBoxOut.Value
    sh.Range("I" & Selected_Row).Value = Me.TextBoxDuration.Value
    sh.Range("J" & Selected_Row).Value = Me.ComboBoxRelationship.Value
    sh.Range("K" & Selected_Row).Value = TextBoxVisitDate.Value
    
    myForm.TextBoxVName.Value = ""
    myForm.ComboBoxNationality.Value = ""
    myForm.TextBoxPName.Value = ""
    myForm.TextBoxIqama.Value = ""
    myForm.ComboBoxRoom.Value = ""
    myForm.ComboBoxRelationship.Value = ""
    myForm.TimeBoxOut.Value = ""
    myForm.TextBoxDuration.Value = ""
    TimeBoxIn.Value = Now
    TimeBoxIn = Format(TimeBoxIn.Value, "HH:MM:SS AM/PM")
    ResetButton.Enabled = FALSE
    TimeOutButton.Enabled = FALSE
    CommandButtonDelete.Enabled = TRUE
    CommandButtonUpdate.Enabled = FALSE
    
    Call Refresh_Data
    'Range("K2").Sort Key1:=Range("K2"), Order1:=xlDescending, Header:=xlYes
    
End Sub

Private Sub TextBox5_Change()
    
End Sub

Private Sub Label1_Click()
    
End Sub

Private Sub Label2_Click()
    
End Sub

Private Sub Label3_Click()
    
End Sub

Private Sub Label4_Click()
    
End Sub

Private Sub Label6_Click()
    
End Sub

Private Sub Label7_Click()
    
End Sub

Private Sub ResetButton_Click()
    If Me.TimeBoxIn.Value = "" Then
        TimeBoxOut.Value = Now
        TimeBoxOut = Format(TimeBoxIn.Value, "HH:MM:SS AM/PM")
        Exit Sub
    End If
    
    '======================= Validation =========================
    
    If Not IsEmpty(Me.TimeBoxIn.Value) Then
        Dim answer  As Integer
        answer = MsgBox("Do you want To Continue?", vbQuestion + vbYesNo)
        
        If answer = vbYes Then
            TimeBoxIn.Value = Now
            TimeBoxIn = Format(TimeBoxIn.Value, "HH:MM:SS AM/PM")
        Else
            'if clicked No do nothing
        End If
        Exit Sub
    End If
    
End Sub

Private Sub TextBox4_Change()
    
End Sub

'======================= Time In Box =========================
Private Sub UserForm_Initialize()
    TimeBoxIn.Value = Now
    TimeBoxIn = Format(TimeBoxIn.Value, "HH:MM:SS AM/PM")
End Sub

'======================= Delete button =========================

Private Sub CommandButtonDelete_Click()
    
    If Me.ListBox1.ListIndex < 0 Then
        MsgBox "Please Select a record To update", vbCritical
        Exit Sub
    End If
    
    Dim Selected_Row As Long
    Dim sh          As Worksheet
    Set sh = ThisWorkbook.Sheets("Database")
    
    If Selected_Row < 1 Then
        MsgBox "Please Select a record To Delete", vbCritical
    Else
        
        Selected_Row = Application.WorksheetFunction.Match(Me.ListBox1.List(Me.ListBox1.ListIndex, 0), sh.Range("A:A"), 0)
        
        sh.Range("A" & Selected_Row).EntireRow.Delete
    End If
    'Range("K2").Sort Key1:=Range("K2"), Order1:=xlDescending, Header:=xlYes
    myForm.TextBoxVName.Value = ""
    myForm.ComboBoxNationality.Value = ""
    myForm.TextBoxPName.Value = ""
    myForm.TextBoxIqama.Value = ""
    myForm.ComboBoxRoom.Value = ""
    myForm.ComboBoxRelationship.Value = ""
    myForm.TimeBoxOut.Value = ""
    myForm.TextBoxDuration.Value = ""
    TimeBoxIn.Value = Now
    TimeBoxIn = Format(TimeBoxIn.Value, "HH:MM:SS AM/PM")
    ResetButton.Enabled = FALSE
    TimeOutButton.Enabled = FALSE
    CommandButtonAdd.Enabled = TRUE
    CommandButtonDelete.Enabled = FALSE
    CommandButtonUpdate.Enabled = FALSE
    'Range("K2").Sort Key1:=Range("K2"), Order1:=xlDescending, Header:=xlYes
    Call Refresh_Data
    ActiveWorkbook.RefreshAll
    
End Sub

'======================= Clear All Button =========================

Private Sub CommandButtonClear_Click()
    
    myForm.TextBoxVName.Value = ""
    myForm.ComboBoxNationality.Value = ""
    myForm.TextBoxPName.Value = ""
    myForm.TextBoxIqama.Value = ""
    myForm.ComboBoxRoom.Value = ""
    myForm.ComboBoxRelationship.Value = ""
    myForm.TimeBoxOut.Value = ""
    myForm.TextBoxDuration.Value = ""
    TimeBoxIn.Value = Now
    TimeBoxIn = Format(TimeBoxIn.Value, "HH:MM:SS AM/PM")
    ResetButton.Enabled = FALSE
    TimeOutButton.Enabled = FALSE
    CommandButtonAdd.Enabled = TRUE
    CommandButtonDelete.Enabled = FALSE
    CommandButtonUpdate.Enabled = FALSE
    Me.TextBoxVisitDate.Value = Now
    TextBoxVisitDate = Format(TextBoxVisitDate.Value, "DD/MM/YYYY")
    
    'Range("K2").Sort Key1:=Range("K2"), Order1:=xlDescending, Header:=xlYes
    Call Refresh_Data
    ActiveWorkbook.RefreshAll
End Sub

Private Sub ListBox1_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
    CommandButtonAdd.Enabled = FALSE
    ResetButton.Enabled = TRUE
    TimeOutButton.Enabled = TRUE
    If Me.ListBox1.List(Me.ListBox1.ListIndex, 0) <> "" Then
        Me.TextBox4.Value = Me.ListBox1.List(Me.ListBox1.ListIndex, 0)
        Me.TextBoxVName.Value = Me.ListBox1.List(Me.ListBox1.ListIndex, 1)
        Me.ComboBoxNationality.Value = Me.ListBox1.List(Me.ListBox1.ListIndex, 2)
        Me.TextBoxPName.Value = Me.ListBox1.List(Me.ListBox1.ListIndex, 3)
        Me.TextBoxIqama.Value = Me.ListBox1.List(Me.ListBox1.ListIndex, 4)
        Me.ComboBoxRoom.Value = Me.ListBox1.List(Me.ListBox1.ListIndex, 5)
        Me.TimeBoxOut.Value = Format(Me.ListBox1.List(Me.ListBox1.ListIndex, 7), "HH:MM:SS AM/PM")
        Me.TimeBoxIn.Value = Format(Me.ListBox1.List(Me.ListBox1.ListIndex, 6), "HH:MM:SS AM/PM")
        Me.TextBoxDuration.Value = Format(Me.ListBox1.List(Me.ListBox1.ListIndex, 8), "HH:MM:SS")
        Me.ComboBoxRelationship.Value = Me.ListBox1.List(Me.ListBox1.ListIndex, 9)
        Me.TextBoxVisitDate.Value = Me.ListBox1.List(Me.ListBox1.ListIndex, 10)
        
    End If
    CommandButtonUpdate.Enabled = TRUE
    CommandButtonDelete.Enabled = TRUE
End Sub

Private Sub UserForm_Activate()
    CommandButtonDelete.Enabled = FALSE
    CommandButtonUpdate.Enabled = FALSE
    ResetButton.Enabled = FALSE
    TimeOutButton.Enabled = FALSE
    Me.TextBoxVisitDate.Value = Now
    TextBoxVisitDate = Format(TextBoxVisitDate.Value, "DD-MM-YYYY")
    
    'Range("K2").Sort Key1:=Range("K2"), Order1:=xlDescending, Header:=xlYes
    
    With Me.ComboBoxRoom
        .Clear
        .AddItem "NA"
        .AddItem "3301"
        .AddItem "3302"
        .AddItem "3303"
        .AddItem "3304"
        .AddItem "3305"
        .AddItem "3306"
        .AddItem "3307"
        .AddItem "3308"
        .AddItem "3309"
        .AddItem "3310"
        .AddItem "3311"
        .AddItem "3312"
    End With
    With Me.ComboBoxRelationship
        .Clear
        .AddItem "Relative"
        .AddItem "Mother"
        .AddItem "Father"
        .AddItem "Brother"
        .AddItem "Sister"
        .AddItem "Son"
        .AddItem "Daughter"
        .AddItem "Friend"
        .AddItem "Others"
    End With
    
    With Me.ComboBoxNationality
        .Clear
        .AddItem "Saudi"
        .AddItem "Non Saudi"
        .AddItem "Pakistan"
        .AddItem "Indian"
        .AddItem "Sudan"
        .AddItem "Egypt"
        .AddItem "UAE"
        .AddItem "Bahrain"
        .AddItem "Kuwait"
        .AddItem "Oman"
        .AddItem "Qatar"
        .AddItem "Iran"
        .AddItem "Iraq"
        .AddItem "Jordan"
        .AddItem "Palestine"
        .AddItem "Yemen"
        .AddItem "Bangladeshi"
    End With
    
    Call Refresh_Data
    
End Sub

Sub Refresh_Data()
    
    Dim sh          As Worksheet
    Set sh = ThisWorkbook.Sheets("Database")
    
    Dim Last_Row    As Long
    Last_Row = Application.WorksheetFunction.CountA(sh.Range("A:A"))
    
    sh.Range("K:K").NumberFormat = "DD/MM/YYYY"
    sh.Range("G:G").NumberFormat = "HH:MM:SS AM/PM"
    sh.Range("H:H").NumberFormat = "HH:MM:SS AM/PM"
    sh.Range("I:I").NumberFormat = "HH:MM:SS"
    
    With Me.ListBox1
        
        .ColumnHeads = TRUE
        .ColumnCount = 11
        .ColumnWidths = "20,100,60,100,60,60,60,80,50,60,50"
        
        If Last_Row = 1 Then
            .RowSource = "Database!A2:K2"
        Else
            .RowSource = "Database!A2:K2" & Last_Row
        End If
    End With
    
End Sub