#1   Report Post  
mully
 
Posts: n/a
Default VBA Error

Hi

On a UserForm everything works OK all the boxes fill with the correct
information -- however on hitting enter the following error message appears.

Division By Zero

in the VBA the bottom line of the following code is highlighted

Private Sub txtHrsTime_Change()
Me.txtHrs2.Value = (Val(Trim(txtHrsTime.Text))) * 60
Me.txtVel2.Value = (Val(Trim(txtDis2.Text))) / (Val(Trim(txtHrs2.Text)))
End Sub

On viewing the sheet everything has been entered in the correct Cells --
this bug is really bugging me.

Also how do you restrict the numbers after the decimal point in a Text Box.

Any help much appreciated

Cheers ---- Mully
  #2   Report Post  
Dave O
 
Posts: n/a
Default

When you get the error and the line is highlighted, you can hover the
mouse cursor over a variable and Excel will show you the value of that
variable as of the time the error occurred. What values do you see for
txtHrs2.Text and txtHrs2.Value ?

  #3   Report Post  
Duke Carey
 
Posts: n/a
Default

Since the error is divide by zero, and the only place in that line where
there's division taking place uses txtHrs2.Text as the divisor, then it
appears that txtHrs2.Text is blank or otherwise equal to 0

When you get the error you can hover the mouse over txtHrs2.Text to see its
value to confirm that is the issue. If so you'll have to figure out why it's
zero when you don't think that's the case.


"mully" wrote:

Hi

On a UserForm everything works OK all the boxes fill with the correct
information -- however on hitting enter the following error message appears.

Division By Zero

in the VBA the bottom line of the following code is highlighted

Private Sub txtHrsTime_Change()
Me.txtHrs2.Value = (Val(Trim(txtHrsTime.Text))) * 60
Me.txtVel2.Value = (Val(Trim(txtDis2.Text))) / (Val(Trim(txtHrs2.Text)))
End Sub

On viewing the sheet everything has been entered in the correct Cells --
this bug is really bugging me.

Also how do you restrict the numbers after the decimal point in a Text Box.

Any help much appreciated

Cheers ---- Mully

  #4   Report Post  
George Nicholson
 
Posts: n/a
Default

I avoid using the Text property whenever possible. I can't find it in Excel
VB Help, but I remember some sort of restriction that the Text property of a
TextBox is only accessible when that control has the focus. Your code uses
the Text property of 3 controls. Obviously, only 1 control can have the
focus at any given time. It could be I'm confusing the TextBoxes of
UserForms, Access and VB6, all of which are slightly different animals, but
I still would eliminate it as the potential problem first.

Setting Text also sets Value. Value is the default property of TextBoxes.
Why not just use that?

Try changing
Me.txtVel2.Value = (Val(Trim(txtDis2.Text))) / (Val(Trim(txtHrs2.Text)))
to
Me.txtVel2 = (Val(Trim(txtDis2))) / (Val(Trim(txtHrs2)))
and see if you get better results.

HTH,
--
George Nicholson

Remove 'Junk' from return address.


"mully" wrote in message
...
Hi

On a UserForm everything works OK all the boxes fill with the correct
information -- however on hitting enter the following error message
appears.

Division By Zero

in the VBA the bottom line of the following code is highlighted

Private Sub txtHrsTime_Change()
Me.txtHrs2.Value = (Val(Trim(txtHrsTime.Text))) * 60
Me.txtVel2.Value = (Val(Trim(txtDis2.Text))) / (Val(Trim(txtHrs2.Text)))
End Sub

On viewing the sheet everything has been entered in the correct Cells --
this bug is really bugging me.

Also how do you restrict the numbers after the decimal point in a Text
Box.

Any help much appreciated

Cheers ---- Mully



  #5   Report Post  
mully
 
Posts: n/a
Default

Hi Everybody

Thanks for the advice-- hovering over txtHrs2 shows a zero as everyone
suggested-- also got rid of text and value -- hit enter again still the same
line highlighted.
the text box -- txtHrs2 shows on the Userform the correct entry of figures
as do all the other boxes txtVel2 text box which I'm trying to divide also
shows the correct figure --- so everything is in place until hitting enter --
then everything goes pear shaped. If I change the top line of code to the
following

Private Sub txtHrsTime_Enter()

Then click on txtVel2 the figures show and it all works OK .

Thanks for the help will keep trying to get it to work correctly

Cheers --- Mully


"George Nicholson" wrote:

I avoid using the Text property whenever possible. I can't find it in Excel
VB Help, but I remember some sort of restriction that the Text property of a
TextBox is only accessible when that control has the focus. Your code uses
the Text property of 3 controls. Obviously, only 1 control can have the
focus at any given time. It could be I'm confusing the TextBoxes of
UserForms, Access and VB6, all of which are slightly different animals, but
I still would eliminate it as the potential problem first.

Setting Text also sets Value. Value is the default property of TextBoxes.
Why not just use that?

Try changing
Me.txtVel2.Value = (Val(Trim(txtDis2.Text))) / (Val(Trim(txtHrs2.Text)))
to
Me.txtVel2 = (Val(Trim(txtDis2))) / (Val(Trim(txtHrs2)))
and see if you get better results.

HTH,
--
George Nicholson

Remove 'Junk' from return address.


"mully" wrote in message
...
Hi

On a UserForm everything works OK all the boxes fill with the correct
information -- however on hitting enter the following error message
appears.

Division By Zero

in the VBA the bottom line of the following code is highlighted

Private Sub txtHrsTime_Change()
Me.txtHrs2.Value = (Val(Trim(txtHrsTime.Text))) * 60
Me.txtVel2.Value = (Val(Trim(txtDis2.Text))) / (Val(Trim(txtHrs2.Text)))
End Sub

On viewing the sheet everything has been entered in the correct Cells --
this bug is really bugging me.

Also how do you restrict the numbers after the decimal point in a Text
Box.

Any help much appreciated

Cheers ---- Mully




Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Ignore error msgs in formula references gharden Excel Discussion (Misc queries) 4 June 17th 05 12:14 AM
Error trapped only while stepping through the code - Not triggered when run Jeff Excel Discussion (Misc queries) 7 March 7th 05 06:29 PM
Error trapped only while stepping through the code - Not triggered when run Jeff Excel Discussion (Misc queries) 0 February 28th 05 06:26 PM
Problem with VBA returning the contents of a long formula. [email protected] Excel Discussion (Misc queries) 2 February 23rd 05 12:14 AM
ERROR Pinto1uk Excel Discussion (Misc queries) 1 February 8th 05 03:15 AM


All times are GMT +1. The time now is 10:31 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"