data verification in VBA user form

VBAmalta89

Board Regular
Joined
Mar 25, 2011
Messages
116
I have the following lines of code but when I compile it an error is prompted saying that it couldnt find the specified object and highlights this line:

For Each Ctrl In Me.Controls("Frame" & i).Controls


The coding im using is as follows:


Private Sub FactoryNum_Change()

OnlyNumbers

Dim i As Long
Dim Ctrl As Control

If (Val(FactoryNum.Value)) > 2 Then

MsgBox "Sorry, maximum of 2 plants allowed for Calculator"

For i = 1 To Val(FactoryNum.Text)
For Each Ctrl In Me.Controls("Frame" & i).Controls
Ctrl.Enabled = True
Next Ctrl
Next i
For i = Val(FactoryNum.Text) + 1 To 2
For Each Ctrl In Me.Controls("Frame" & i).Controls
Ctrl.Enabled = False
Next Ctrl
Next i

End If

End Sub


What i want the program to do is check that the value of a text box isnt greater than 2. if it is then it prompts the user to re enter a value, if it is less then program execution can continue.

How can i achieve this?
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
Have you checked the names of all your controls, including the frames?
 
Upvote 0
Yes. In fact the program runs if i leave out the following:

If (Val(FactoryNum.Value)) > 2 Then

MsgBox "Sorry, maximum of 2 plants allowed for Calculator"

but if i include it then i get the error i stated in my first post for some reason.
 
Upvote 0
If you have the time, would you post a brief description of the solution?

That might assist people who experience similar errors in the future and find your post via a forum search or Google.
 
Upvote 0
yes of course:

What i did was include an else statement along with what i had. S in total i had an if then else statement rather than just an if then. the else statement would then allow the execution of teh remainder of the program.

Something basic which i forgot about when coding this section!
 
Upvote 0

Forum statistics

Threads
1,214,583
Messages
6,120,380
Members
448,955
Latest member
BatCoder

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