VBA code to check cell contents agains a list of valid values

Spartan300

Board Regular
Joined
Jul 1, 2008
Messages
71
Hi,

I have a report which automatically finds the month of the year and then uses various VLOOKUPS to pull the relevant data into the report.

I have a button within the workbook so that the user can change the month displayed in cell C6 via an input box.

What I am hoping to do by way of VBA code is check that the month entered is valid, to avoid a tbale full of #NA errors.
I have tried putting data validation on the cell, but when it is entered from the input box it seems to overwrite it?

I was thinking of a VLOOKUP but I think I had some terrible code as it just crashed excel!

Thanks very much.
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
Just to get the ball rolling on this, your description of the problem does not make sense. Data Validation would be a much better way to mandate which month name is selected for cell C6 instead of using an input box where you never know what the user will try to enter. Yes you can programatically validate the inputbox for a proper month name but why go through all that if you can just use DV in the cell and stop using the inputbox altogether.
 
Upvote 0
Thanks for the reply.

I wanted to have an input box as it is very clear for anyone using the report how to change the month, even if they are unsure of the basics of excel. I also wanted to build in a check so that a month that is yet to occur cannot be entered into the cell.

Thanks
 
Upvote 0
Hello,

I have managed to accomplish what I wanted. This is probably far from the most elegant way but here it is...

Code:
Private Sub EventProc2(ByVal Target As Range)
Dim Lookup As String
On Error GoTo BadTimes
Lookup = Application.WorksheetFunction.VLookup(Range("C6"), Range("K4:K23"), 1, False)
Exit Sub
BadTimes:
MsgBox "Invalid Month", vbOKOnly + vbCritical
If vbOK Then
Range("I1").Select
Selection.Copy
Range("C6").Select
ActiveSheet.Paste
Application.CutCopyMode = False
End If
End Sub

Thanks for the help.
 
Upvote 0

Forum statistics

Threads
1,214,614
Messages
6,120,525
Members
448,969
Latest member
mirek8991

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top