在从用户表单将数据添加到Excel工作表之前弹出警告消息

问题描述 投票:0回答:1

这是我的代码。我想在添加之前检查excel工作表中是否存在数据。如果确实存在,则弹出一个msgbox继续添加数据。

  • 1)按添加按钮
  • 2)检查数据是否存在3)如果存在,请显示“现有数据。确定要添加记录”
  • 3.1)如果用户单击是,请输入记录
  • 3.2)如果单击否,则清除文本框中的数据并允许用户refilL
  • 4.如果数据不存在,请输入最后一行

任何人都可以提供帮助并纠正我在代码中的错误吗?

Private Sub Addbutton_Click()

Sheets("MASTER").Activate
Dim lastrow
Dim answer As String
Dim newRecordRow As Integer
Dim isFound As Boolean
lastrow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row

lastrow = lastrow + 1
newRecordRow = 1


If Me.Entity.Text = Empty Then 'Entity
MsgBox "Please enter the entity.", vbExclamation
Me.Entity.SetFocus 'position cursor to try again
Exit Sub 'terminate here


 End If

If Me.Branch.Text = Empty Then 'Branch
MsgBox "Please enter the Branch.", vbExclamation
Me.Branch.SetFocus 'position cursor to try again
Exit Sub 'terminate here
End If

If Me.Emailname.Text = Empty Then 'Emailname
MsgBox "Please enter the email name.", vbExclamation
Me.Emailname.SetFocus 'position cursor to try again
Exit Sub 'terminate here
End If

If Me.Attention.Text = Empty Then 'Attention
MsgBox "Enter the attention names.", vbExclamation
Me.Attention.SetFocus 'position cursor to try again
Exit Sub 'terminate here
End If

If Me.emailcc.Text = Empty Then 'Emailcc
MsgBox "Enter the cc names.", vbExclamation
Me.emailcc.SetFocus 'position cursor to try again
Exit Sub 'terminate here
End If

Do While (IsEmpty(Worksheets("MASTER").Cells(newRecordRow, 3).Value) = False And isFound = False)


If (UCase(Worksheets("MASTER").Cells(newRecordRow, 3).Value) = UCase(Branch)) Then
Branch.Text = (Branch)
isFound = True
End If
Loop
If isFound = True Then
answer = MsgBox("Existing data.Are you sure to add the record", vbYesNo + vbQuestion, "Add Record")
Else
newRecordRow = newRecordRow + 1
Cells(lastrow, 2) = Entity.Text
Cells(lastrow, 3) = Branch.Text
Cells(lastrow, 4) = Product.Value
Cells(lastrow, 5) = Emailname.Value
Cells(lastrow, 6) = Attention.Value
Cells(lastrow, 7) = Emailadd.Value
Cells(lastrow, 8) = emailcc.Value
Cells(lastrow, 9) = ccadd.Value

If answer = vbYes Then
Cells(lastrow, 2) = Entity.Text
Cells(lastrow, 3) = Branch.Text
Cells(lastrow, 4) = Product.Value
Cells(lastrow, 5) = Emailname.Value
Cells(lastrow, 6) = Attention.Value
Cells(lastrow, 7) = Emailadd.Value
Cells(lastrow, 8) = emailcc.Value
Cells(lastrow, 9) = ccadd.Value
Unload Me
Else

answer = MsgBox("Do you want to add a new record?", vbYesNo + vbQuestion, "Add Record")

'Clear data first
With Me
.Entity.Text = ""
.Branch.Text = ""
.Product.Text = ""
.Emailname.Text = ""
.Attention.Text = ""
.Emailadd.Text = ""
.emailcc.Text = ""
.ccadd.Text = ""
End With

Entity.Value = Cells(lastrow, 2)
Branch.Value = Cells(lastrow, 3)
Product.Value = Cells(lastrow, 4)
Emailname.Value = Cells(lastrow, 5)
Attention.Value = Cells(lastrow, 6)
Emailadd.Value = Cells(lastrow, 7)
emailcc.Value = Cells(lastrow, 8)
ccadd.Value = Cells(lastrow, 9)



End If
End If


Unload Me
End Sub
excel vba excel-vba
1个回答
0
投票

你可以试试这个。我没有时间仔细阅读所有内容,但它现在已正确缩进,您应该能够更轻松地关注它并自行调试。

我还用一个正确的工作表(Cells)对你的ws实例进行了限定。希望这将解决问题。我可能在最后弄乱了逻辑 - 代码类型变得复杂和不清楚..


Option Explicit


Private Sub Addbutton_Click()

Dim ws As Worksheet: Set ws = ThisWorkbook.Sheets("MASTER")
Dim lastrow As Long, answer As String, newRecordRow As Integer, isFound As Boolean

lastrow = ws.Cells(ws.Rows.Count, 1).End(xlUp).Offset(1).Row
newRecordRow = 1


If Me.Entity.Text = Empty Then
    MsgBox "Please enter the entity.", vbExclamation
    Me.Entity.SetFocus
    Exit Sub
ElseIf Me.Branch.Text = Empty Then
    MsgBox "Please enter the Branch.", vbExclamation
    Me.Branch.SetFocus
    Exit Sub
ElseIf Me.Emailname.Text = Empty Then
    MsgBox "Please enter the email name.", vbExclamation
    Me.Emailname.SetFocus
    Exit Sub
ElseIf Me.Attention.Text = Empty Then
    MsgBox "Enter the attention names.", vbExclamation
    Me.Attention.SetFocus
    Exit Sub
ElseIf Me.emailcc.Text = Empty Then
    MsgBox "Enter the cc names.", vbExclamation
    Me.emailcc.SetFocus
    Exit Sub
End If


Do While (IsEmpty(.ws.Cells(newRecordRow, 3).Value) = False And isFound = False)
    If (UCase(ws.Cells(newRecordRow, 3).Value) = UCase(Branch)) Then
        Branch.Text = (Branch)
        isFound = True
    End If
Loop


If Not isFound Then 'Double Negative (This means isFound is False)
    newRecordRow = newRecordRow + 1
    ws.Cells(lastrow, 2) = Entity.Text
    ws.Cells(lastrow, 3) = Branch.Text
    ws.Cells(lastrow, 4) = Product.Value
    ws.Cells(lastrow, 5) = Emailname.Value
    ws.Cells(lastrow, 6) = Attention.Value
    ws.Cells(lastrow, 7) = Emailadd.Value
    ws.Cells(lastrow, 8) = emailcc.Value
    ws.Cells(lastrow, 9) = ccadd.Value
Else 'If isFound is True
    answer = MsgBox("Existing data.Are you sure to add the record", vbYesNo + vbQuestion, "Add Record")
        If answer = vbYes Then
            ws.Cells(lastrow, 2) = Entity.Text
            ws.Cells(lastrow, 3) = Branch.Text
            ws.Cells(lastrow, 4) = Product.Value
            ws.Cells(lastrow, 5) = Emailname.Value
            ws.Cells(lastrow, 6) = Attention.Value
            ws.Cells(lastrow, 7) = Emailadd.Value
            ws.Cells(lastrow, 8) = emailcc.Value
            ws.Cells(lastrow, 9) = ccadd.Value
        End If
End If

answer = MsgBox("Do you want to add a new record?", vbYesNo + vbQuestion, "Add Record")

If answer = vbYes Then
    With Me
        .Entity.Text = ""
        .Branch.Text = ""
        .Product.Text = ""
        .Emailname.Text = ""
        .Attention.Text = ""
        .Emailadd.Text = ""
        .emailcc.Text = ""
        .ccadd.Text = ""
    End With

    Entity.Value = ws.ws.Cells(lastrow, 2)
    Branch.Value = ws.Cells(lastrow, 3)
    Product.Value = ws.Cells(lastrow, 4)
    Emailname.Value = ws.Cells(lastrow, 5)
    Attention.Value = ws.Cells(lastrow, 6)
    Emailadd.Value = ws.Cells(lastrow, 7)
    emailcc.Value = ws.Cells(lastrow, 8)
    ccadd.Value = ws.Cells(lastrow, 9)
End If

Unload Me

End Sub
© www.soinside.com 2019 - 2024. All rights reserved.