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

时间:2018-09-04 02:18:16

标签: excel vba excel-vba

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

  • 1)按添加按钮
  • 2)检查数据是否存在3)如果存在,则显示msg“现有数据。确定要添加记录”
  • 3.1)如果用户单击“是”,则输入记录
  • 3.2)如果单击否,则清除文本框中的数据并允许用户重新过滤
  • 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

1 个答案:

答案 0 :(得分:0)

您可以尝试一下。我没有时间去处理所有内容,但是现在它已经适当地缩进了,您应该可以轻松地遵循它,甚至可以自己调试它。

我还以正确的方式(var tips = []; var bills = [124, 48, 264]; function tipCalc(bills, tips){ switch(bills){ case bills < 50: tips[0] = bills[1] * 0.2; break; case bills > 50: tips[1] = bills[0] * 0.5; break; default: console.log('no tips left.'); } return tips; } tips = tipCalc(bills[0]); console.log(tips); enter code here )用工作表对您的Cells实例进行了限定。希望这可以解决问题。我可能在最后弄乱了逻辑-代码变得混乱而不清楚。


ws