VBA Data Validation

kgkev

Well-known Member
Joined
Jun 24, 2008
Messages
1,291
Office Version
  1. 365
Platform
  1. Windows
When a user enters a value in A6:A22 VBA checks if it contains a number.

If it does it makes the whole value upcase and thats it.

However if a number is not present it checks the value against a list of valid option.

If the value matches an item in the list it makes it upcase and thats it.

However if it does not match any item in the list it displays a userform with a combobox with the list of allowed values.

I am having trouble returning this value to excel.


The macro call happens on worksheet change

Code:
Private Sub Worksheet_Change(ByVal Target As Range)

However I cannot refer to "Target" from the user form.
Writing to activecell doesn't work as its just changed.

How can I record the address of target and use it again from the user form>?


Code:
  ca = True
            If Target.Cells.Count = 1 Then
                If Target.Value <> "" Then
                    If Not HasNumber(Target.Value) Then
                        If blnExists(Target.Value, Sheets("timesheet").Range("F30:F37")) = False Then
                            For Each item In Sheets("Timesheet").Range("F30:F37")
                            UserForm1.ComboBox1.AddItem item.Value
                            
                            Next item
                            UserForm1.Show
                            
                            
                        End If
                    End If
                End If
             End If

Code:
Private Sub CommandButton1_Click()
                    Application.EnableEvents = False
                    
Target.Value = UCase(UserForm1.ComboBox1.Value)
           
                    Application.EnableEvents = True

UserForm1.Hide

End Sub
Private Sub CommandButton2_Click()
Application.EnableEvents = False
                    
Target.Value = ""
           
                    Application.EnableEvents = True

UserForm1.Hide
End Sub
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
Rather than let the user tear his / her hair out figuring out what should be right and wrong in a cell, why not just give them a drop down list of things they can choose from using data validation?

Google "Excel Data Validation Lists"

Don't over complicate it... :)
 
Upvote 0
This is a time sheet.

They have to enter a Works Order Number in various/infinte forms, but always containing a number. ie

TBW015
R0245WC
BBCS0125....


Or a list of 8 other options,

Holiday
Doctors
Sick
Office....

There are thousands of works order numbers available so no way to validate them. So if it has a number it is just accepted.
 
Upvote 0
It's not a techy issue - your ID numbers are all over the place.

Accepting it just has a number isn't good enough - what happens if I randomly bang the keyboard?! Its a major flaw in the design.

As with pretty much everything in technology and especially programming, you have to have consistency otherwise you'll never win.
 
Upvote 0
If you bang the keyboard and manage to hit a number it will be looked up in a later process that checks it against live works orders and throws it out then.

This is just a pre-check to reduce time correcting errors later on.

Anyway solved it by decalring the variable above the Modules.
 
Upvote 0

Forum statistics

Threads
1,224,583
Messages
6,179,678
Members
452,937
Latest member
Bhg1984

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