简体   繁体   中英

If statement partially not executed with UserForm

I use simple if statements to make some checks before data from user form will be executed. If my statement is right code will proceed with changes, if not it will give me msgbox

It's part that doesn't work:

If Me.AusAmmTubesAuslagern.Value <= Me.AusAvailTubes Or _
      Me.AusAvailTubes <> "N/A" Then

First part If Me.AusAmmTubesAuslagern.Value <= Me.AusAvailTubes works perfectly. msgbox appeared.

If I reproduce that in my statement: that my Me.AusAvailTubes = N/A, it doesn't work. It make those changes. It should only happen, if that field has other txt than N/A.

I tried to get that data directly from a list not from UserForm, that same problem...

All those UserForm textboxes are filled from a list with help from other macro.

Each material is stored in 2 ways. Small pcs. or big boxes. From few of them u can take those small pcs. from other only whole boxes. Thats why i wrote 2 macros, that are working with UserForm.

1st Macro remove those small pcs. 2nd only whole boxes.

I need 2 checkpoints:

Checkpoint 1: If Me.AusAmmTubesAuslagern.Value <= Me.AusAvailTubes it checks that for ex. there are 40 Tubes in one box in warehouse and user try to remove more tan 40 it will stop him with msgbox.

Checkpoint 2: That material is not in small pcs. available, you need to take whole box with you. In that case there is column in my list that has "N/A" and it will be written with other macro in that textbox Me.AusAvailTubes before user will be able to try to withdraw it from warehouse with macro. That textbox need to be checked and if in that textbox is "N/A" the code will give too msgbox and will stop the code

Whole code:

Sub Tubes()
  Dim totRows As Long, i As Long
  Dim wsTubes As Worksheet
  Dim oldVal As Integer
  Dim newVal As Integer
  Dim AusWS As Worksheet
  Dim newValAus As String
  Dim oldValAus As String

 Select Case Me.AusListe.Value

  Case Is = "E1G"
  Set wsTubes = E1G
  Set AusWS = AusE1G

  totRows = wsTubes.Range("A1").CurrentRegion.Rows.Count

'Check to see if value exists

 If WorksheetFunction.CountIf(wsTubes.Range("A:A"), Me.AusCharge.Value) = 0 Then
    MsgBox "no right Charge chosen"
   Me.AusCharge.Value = ""
 Exit Sub
End If

With wsTubes
 For i = 2 To totRows
    If .Cells(i, 1) Like Me.AusCharge Then
      If Me.AusAmmTubesAuslagern.Value <= Me.AusAvailTubes Or _
      "N/A" <> Me.AusAvailTubes Then
         oldVal = .Cells(i, 14).Value
         newVal = oldVal + Me.AusAmmTubesAuslagern.Value
         wsTubes.Cells(i, 14) = newVal
       Else
        MsgBox "So viele Stücke dieser sind nich auf Lager vorhanden!"
        Exit Sub
      End If
    End If
Next
End With

'Rest of the code not important to that part above

End Select
End Sub

I guess your problem is mixing numbers and strings. Or, you are confusing OR and AND ?

I assume than Me.AusAmmTubesAuslagern and Me.AusAvailTubes are Controls on a Userform and you want to check the input. If you carefully check your if -statement, you will see that is always results in True - except if you enter a string starting with something like "X" into AusAmmTubesAuslagern

For simplicity, lets assume:

val1: Me.AusAmmTubesAuslagern.Value
val2: Me.AusAvailTubes
cond1: v1 <= v2
cond2: v2 <> "N/A"

Then, your IF -statement results in (added the condition also with AND , maybe that's what you need)

val1    val2    cond1   cond2   cond1 OR cond2  cond1 AND cond2  
-----------------------------------------------------------------
3       2       False   True    True            False
2       3       True    True    True            True
1       "N/A"   True    False   True            False
"AA"    "N/A"   True    False   True            False
"XY"    "N/A"   False   False   False           False

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM