VBA VLookup If Match MsgBox Else Continue - excel

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. Tab 2 is where data formatting takes place and tab 3 is where the completed accounts are listed. 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.
So essentially I really want it to Vlookup against the completed list. If a match then msgbox to say "This account has already been completed", else carry on formatting the data.
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?
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

Related

Mandatory Row of cells in an Excel Table

I am trying to make a row in my Excel table mandatory before users close the document, then display a pop-up message stating "cells require input".
I am running into an issue where users are still getting the pop-up message even if they have filled out all the mandatory cells.
This is a screenshot of what all I typed out. I have this in the workbook area
I am typing what's in the screenshot, in the workbook area, and have it to run beforeclose.
This is the what I used below. My required fields is the row A3-O3
Private Sub Workbook_BeforeClose(Cancel As Boolean)
If Cells(3, 1)(3, 2)(3, 3)(3, 4)(3, 5)(3, 6)(3, 7)(3, 8)(3, 9)(3, 10)(3, 11)(3, 12)(3, 13)(3, 14)(3, 15).Value = "" Then
MsgBox "Cell(s) require input", vbInformation, "Kutools for Excel"
Cancel = True
End If
End Sub
view of my spreadsheet
A plus would be a pop-up message letting the user know which cells are empty & for it to highlight the cells that are empty also
Use WorksheetFunction.CountBlank:
If Worksheetfunction.CountBlank(ActiveSheet.Range("A3:O3")) > 0 Then
MsgBox "Cell(s) require input", vbInformation
End If
Or SpecialCells(xlCellTypeBlanks):
On Error Resume Next
Dim rng As Range
Set rng = ActiveSheet.Range("A3:O3").SpecialCells(xlCellTypeBlanks)
On Error GoTo 0
If Not rng Is Nothing Then
MsgBox "Cell(s) " & rng.Address(False, False) & " require input", vbInformation
End If
Note that Cells(3, 1)(3, 2)(3, 3)(3, 4)(3, 5)(3, 6)(3, 7)(3, 8)(3, 9)(3, 10)(3, 11)(3, 12)(3, 13)(3, 14)(3, 15) does not refer to A3:O3.
In the Immediate Window, put:
? Cells(3, 1)(3, 2)(3, 3)(3, 4)(3, 5)(3, 6)(3, 7)(3, 8)(3, 9)(3, 10)(3, 11)(3, 12)(3, 13)(3, 14)(3, 15).Address
The result is
$DB$31

How to prevent saving when cells are blank but to allow me to save the blank template

I have a template that I need to send to various people in my company to use. Normally, half of them leave specific information I need blank when they return the form (despite multiple warnings from myself), so I set up a VBA macro to prevent them from saving the document until those specific cells are filled in. However, now I cannot simply save the template myself, and they wouldn't be able to save the template to their desktops either. Is there a way to do this or am I doomed?
If Application.Sheets("Sheet1").Range("D34, E34, F34").Value = "" Then
Cancel = True
MsgBox "Please fill in the dimensions in cells D34, E34 and F34"
End If
If Application.Sheets("Sheet1").Range("G34").Value = "" Then
Cancel = True
MsgBox "Please fill in the commodity weight in cell G34 "
End If
If Application.Sheets("Sheet1").Range("J33").Value = "" Then
Cancel = True
MsgBox "Please fill in the commodity percentage in cell J38"
End If
If Application.Sheets("Sheet1").Range("c22").Value = "" Then
Cancel = True
MsgBox "Please fill in the monthly bill commitment in cell C22"
End If
End Sub```
You can put the macro on the event Workbook.BeforeSave
well described on MS

If and Then basic

I am new to VBA and I want to test a basic If and Then Statement. I want to create a message to say Yes or No based on the data in cell A1 in excel named score. I had tried many things and sometimes just all the messages pop up so wondering how to write this properly?
Here is the actual statement:
Sub IfTest()
Dim Score As Integer
Dim Msgbox As String
Score = Cells(1, 1).Value
If Score = 1 Then
Msgbox = "Yes"
Else
Msgbox = "no"
End If
End Sub
Welcome to VBA!
MsgBox "This is my message", , "This is my Title"
Between the commas, you insert button styles.
Button styles and Title are optional.
So for your example, remove the "="'s and instead do:
If Score = 1 Then
Msgbox "Yes"
Else
Msgbox "no"
End If
Check out the MS link: MsgBox Function
* **EDIT per comintern's comment - remove this line !!!!!!
Dim Msgbox As String

Showing COUNTIF results in a Message Box

I am currently using this code to display the number of "RCA Pending" found in a column. The message box does show the correct number of times it is found in the column, however, it creates a box for each instance (i.e. if there are 2 instances in the column, when the workbook is open it will display "Found 2 RCA Pending(s)", then when the user clicks OK, a second popup saying the same thing appears. If there are 5, you will get 5 consecutive popups).
Sub Auto_Open()
Dim row As Range
For Each row In Worksheets("Swivel").UsedRange.Rows
If row.Cells(1, "AB").Value = "RCA Pending" Then
MsgBox "Found " & WorksheetFunction.CountIf(Columns("AB"), "RCA Pending") & " RCA Pending(s)", vbInformation, "RCA Pending Found"
End If
Next row
End Sub
How can this be altered to show the total number of instances and not get multiple popups?
As a side note, I am using UsedRange because the range is always growing. The module that this code resides in has Option Explicit at the top.
Is this what you are trying?
Sub Auto_Open()
Dim instances As Long
instances = WorksheetFunction.CountIf(Columns("AB"), "RCA Pending")
If instances <> 0 Then _
MsgBox "Found " & instances & " RCA Pending(s)", vbInformation, "RCA Pending Found"
End Sub
OR
Sub Auto_Open()
Dim instances As Long
instances = WorksheetFunction.CountIf(Columns("AB"), "RCA Pending")
MsgBox "We Found " & instances & " instances of RCA Pending(s)", _
vbInformation, "RCA Pending Found"
End Sub

Looping a VBA statement to check for empty cells before continuing sub

I'm new to VBA. How would I change the below code to check cells Q1:Q5 rather than just Q1. Also, is it possible to temporarily highlight the cell that's empty?
If ActiveSheet.Range("Q1").Value = "" Then
Answer = MsgBox("You have not entered all of the required details.", vbCritical, "Error")
Exit Sub
End If
For highlighting cells that are empty use Conditional Formatting (can be applied without code)
See Debra Dalgleish's site
Code Question
Sub CellCheck()
Dim rng1 As Range
Set rng1 = Range("Q1:Q5")
If Application.CountA(rng1) <> rng1.Cells.Count Then _
Answer = MsgBox("You have not entered all of the required details.", vbCritical, "Error")
End Sub

Resources