简体   繁体   English

VBA VLookup If Match MsgBox Else Continue

[英]VBA VLookup If Match MsgBox Else Continue

I have a template that is being used to look up a list of accounts.我有一个用于查找帐户列表的模板。 Its kind of like a workflow tool, where tab 1 shows a list of active accounts.它有点像工作流工具,其中选项卡 1 显示活动帐户列表。 Tab 2 is where data formatting takes place and tab 3 is where the completed accounts are listed.选项卡 2 是进行数据格式化的位置,选项卡 3 是列出已完成帐户的位置。 This is being used by multiple people.这被多人使用。 So as they work through the list I wanted to apply some error handling that applies a vlookup to the account the user has selected just to see if its already in the completed tab by another user.因此,当他们通过列表工作时,我想应用一些错误处理,将 vlookup 应用于用户选择的帐户,以查看它是否已经在另一个用户的已完成选项卡中。

So essentially I really want it to Vlookup against the completed list.所以本质上我真的希望它对完整的列表进行 Vlookup。 If a match then msgbox to say "This account has already been completed", else carry on formatting the data.如果匹配则 msgbox 说“此帐户已完成”,否则继续格式化数据。

The code I was trying to use was:我试图使用的代码是:

Set WsInput = Sheets("Account Search")
Set PolLookup = WsInput.Range("U3")
Set PolRange = Sheets("Completed Accounts").Range("B5:B15000")

With WsInput
    .Range("U3").Value = Application.VLookup(PolLookup, PolRange, 1, False)
        If IsError(.Range("U3").Value) Then

*Format Data

Else
    MsgBox "This account has already been completed", vbExclamation
    Sheets("Completed Accounts").Select
    Exit Sub

End If

End With

However I can't seem to get it to work.但是我似乎无法让它工作。 If I put a matching account number in it goes to the msgbox, which is what I want, but if I put a new number in, it also goes to the msgbox?如果我在其中输入匹配的帐号会进入 msgbox,这就是我想要的,但如果我输入一个新号码,它也会进入 msgbox?

Any help or advice would be much appreciated.任何帮助或建议将不胜感激。

Thanks谢谢

Sub Check()

    Dim sPol As String, rngPol As Range
    sPol = Sheets("Account Search").Range("U3").Value
    Set rngPol = Sheets("Completed Accounts").Range("B5:B15000")

    If IsError(Application.VLookup(sPol, rngPol, 1, False)) Then
   
        ' do something
        MsgBox sPol & " not completed"

    Else
        MsgBox "Account " & sPol & " has already been completed", vbExclamation
        Sheets("Completed Accounts").Select
        Exit Sub
   End If

End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM