2017-10-11 58 views
-2

我认为我需要尝试使这个问题更容易。所以在这里;如果发现重复,VBA用户表单会发出警告

我在Excel中创建一个用户表单,它将充当数据捕获表单。

在这种形式中我有一个名为PolBX在此一被放置,并在提交数据在文本框PolBX被拷贝到使用此代码

Cells(emptyRow, 7).Value = PolBX.Value的“G”列中。这很好。

我发现可能有用户可能意外地使用相同的唯一ID号两次的情况。所以我试图找出如何编码它,在用户输入唯一ID号后,它会检查该字符串(由字母和数字组成)。如果已经发现在第7列(G)的字符串时,它必须这样说

"Policy number already Used, please try again"

我想我需要使用下面的子程序

Private Sub PolBX_AfterUpdate() 

End Sub 

可一些请协助创建此代码... 也可以请你解释你在做什么,因为我大约一个星期前开始VBA

回答

0

玩耍我发现了一个更简单的方法!我随附了一个带有以下代码的按钮

Private Sub CommandButton8_Click() 
Search = PolBX.Text 
Set FoundCell = Worksheets("sheet1").Columns(7).Find(Search,LookIn:=xlValues, lookat:=xlWhole) 
If FoundCell Is Nothing Then 
MsgBox "No duplicates found" 
Else 
MsgBox "This policy has already been Assessed" & "Please assess a different case" 
PolBX.Value = "" 
End If 
0

你可以添加以下代码来搜索你的保单号,如果没有发现,然后PolLookup = Nothing

Option Explicit 

Sub Test() 

    On Error GoTo ErrHandler 

    Dim ws As Worksheet, PolLookup As Range, LookupRng As Range 
    Set ws = ThisWorkbook.Worksheets(1) 

    'This is the range you want to search, it can be a long range 
    'or it can be a single cell. 
    Set LookupRng = ws.Range("A:A") 

    'Range.Find is looking for your value in the range you specified above 
    Set PolLookup = LookupRng.Find("YourLookupValue") 

    'PolLookup = Nothing if it didn't find a match, so we want to use 
    'If <NOT> Nothing, because this suggests .Find found your value 
    If Not PolLookup Is Nothing Then 
     Err.Raise vbObjectError + 0 'Whatever error you want to throw for finding a match 
    End If 

    'Exit before you reach the ErrHandler 
    Exit Sub 
ErrHandler: 
    If Err.Number = vbObjectError + 0 Then 
     'Handle your error. Do you want to stop completely? Or have the 
     'User enter a new value? 
    End If 

End Sub 

基本上,你的用户输入他们在您的用户窗体值后,只需打个电话本分做一个快速查找。

+0

我在哪里可以找到有关如何执行此操作的教程? – Flaniganga

+0

[使用Range.Find方法](https://msdn.microsoft.com/en-us/vba/excel-vba/articles/range-find-method-excel)。 –

+0

在VBA中使用[Err.Raise方法](https://msdn.microsoft.com/en-us/library/w4t2e92e(v = vs.90).aspx) –

相关问题