0

As the title explains a result from an input box is behaving strangely. It seems like when there is a consecutive double up in last integers in data coming from the balance in the second sub below, what ends up in the cell after the return is not the same as appears on the screen of the balance.

so: 2.56 on the balance screen gives 2.56 in the cell but 2.55 on the screen gives 2.50 ?

or: 2.565 on the balance screen gives 2.565 in the cell but 2.855 on the screen gives 2.850 ? Any ideas would be great. Thanks.

This is the code.

Private Sub Barcode_Keydown(ByVal KeyCode As MSForms.ReturnInteger, _
                            ByVal Shift As Integer)    
    Worksheets("Data").Activate    
    ActiveSheet.Unprotect "waterypanda"

    If KeyCode = vbKeyReturn Then    
        ActiveCell = Barcode.Value    
        ActiveCell.Offset(0, 1).Select    
        ActiveCell.Value = ""    
        If Len(Me.Barcode.Value) = 8 Then Me.weight.SetFocus    
    End If

    Worksheets("Data").Activate  
    ActiveSheet.Protect "waterypanda"    
End Sub


Private Sub weight_Keydown(ByVal KeyCode As MSForms.ReturnInteger, _
                            ByVal Shift As Integer)
    Worksheets("Data").Activate    
    ActiveSheet.Unprotect "waterypanda"

    If KeyCode = vbKeyReturn Then    
        ActiveCell = weight.Value    
        Barcode.Value = ""    
        weight.Value = ""    
        ActiveCell.Offset(1, -1).Select    
    End If

    Worksheets("Data").Activate   
    ActiveSheet.Protect "waterypanda"    
End Sub
Pᴇʜ
  • 56,719
  • 10
  • 49
  • 73
  • The code works fine for me. By the way, that's the first time I've seen code like this - why not use the Exit events of the textboxes to put the values on the sheet? Also, why unprotect/protect the sheet on each key stroke - that appears to cause some sort of lag. – norie Jul 06 '21 at 05:49
  • 1
    You might benefit from reading [How to avoid using Select in Excel VBA](https://stackoverflow.com/questions/10714251/how-to-avoid-using-select-in-excel-vba). Keys might come in faster than the sheet can unprotect/protect, so you might run into some real-time issues here. Make sure you [protect your sheet using UserInterfaceOnly](https://stackoverflow.com/questions/38353751/sheet-protection-userinterfaceonly-gone). This way VBA can make changes to the sheet but the users cannot. So you don't need to unprotect/protect everytime. – Pᴇʜ Jul 06 '21 at 06:08
  • Good advice thanks @PEH . I have added a Application.Wait (Now + TimeValue("0:00:01")) after the return and taken the password off. It seems to gives the slower hardware time to catch up. – CashDown Jul 07 '21 at 21:52
  • @CashDown Well not the clean way but will probably work for most systems. – Pᴇʜ Jul 08 '21 at 06:04

0 Answers0