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?
 

Some videos you may like

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,218
Office Version
  1. 365
Platform
  1. Windows
Have you checked the names of all your controls, including the frames?
 

VBAmalta89

Board Regular
Joined
Mar 25, 2011
Messages
116
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.
 

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,218
Office Version
  1. 365
Platform
  1. Windows

ADVERTISEMENT

So what was the problem?:)
 

Ruddles

Well-known Member
Joined
Aug 24, 2010
Messages
5,786
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.
 

VBAmalta89

Board Regular
Joined
Mar 25, 2011
Messages
116
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!
 

Watch MrExcel Video

Forum statistics

Threads
1,122,499
Messages
5,596,517
Members
414,074
Latest member
Matthew Kakde

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
Top