The program does not return the correct value

KhallP

Board Regular
Joined
Mar 30, 2021
Messages
157
Office Version
  1. 2016
Platform
  1. Windows
I created a macro that detects if the user has entered invalid characters or if the "Textboxes" are empty, but the program is returning the wrong value, can someone help me?

Code:

VBA Code:
Select Case True
   
Case Trim(tb_morada) = "" And Trim(tb_cod_postal) = "" Or (Not IsNumeric(Len(tb_cod_postal) = 1) Or Not IsNumeric(Len(tb_cod_postal) = 2) Or Not IsNumeric(Len(tb_cod_postal) = 3) Or Not IsNumeric(Len(tb_cod_postal) = 4) Or Not IsNumeric(Len(tb_cod_postal) = 6) Or Not IsNumeric(Len(tb_cod_postal) = 7) Or Not IsNumeric(Len(tb_cod_postal) = 8)) And Trim(data_nasc) = "" Or Not IsNumeric(tb_data_nasc) And cb_sexo.ListIndex = -1
    If Trim(tb_cod_postal) = "" Then
        lb_erro_cod_postal.Visible = True
       
    ElseIf Not IsNumeric(Len(tb_cod_postal) = 1) Or Not IsNumeric(Len(tb_cod_postal) = 2) Or Not IsNumeric(Len(tb_cod_postal) = 3) Or Not IsNumeric(Len(tb_cod_postal) = 4) Or Not IsNumeric(Len(tb_cod_postal) = 6) Or Not IsNumeric(Len(tb_cod_postal) = 7) Or Not IsNumeric(Len(tb_cod_postal) = 8) Then
        lb_erro_cod_postal.Visible = True
        lb_erro_cod_postal.Caption = "*Caracteres Inválidos"
       
    End If
   
    If Trim(tb_data_nasc) = "" Then
        lb_erro_data_nasc.Visible = True
       
    ElseIf Not IsNumeric(tb_data_nasc) Then
        lb_erro_data_nasc.Visible = True
        lb_erro_data_nasc.Caption = "*Caracteres Inválidos"
       
    End If
   
    lb_erro_morada.Visible = True
    lb_erro_sexo.Visible = True
    MsgBox "1"


It should return the value "7"


Code:
Case Trim(cod_postal) = "" Or (Not IsNumeric(Len(tb_cod_postal) = 1) Or Not IsNumeric(Len(tb_cod_postal) = 2) Or Not IsNumeric(Len(tb_cod_postal) = 3) Or Not IsNumeric(Len(tb_cod_postal) = 4) Or Not IsNumeric(Len(tb_cod_postal) = 6) Or Not IsNumeric(Len(tb_cod_postal) = 7) Or Not IsNumeric(Len(tb_cod_postal) = 8)) And Trim(data_nasc) = "" Or Not IsNumeric(data_nasc) And cb_sexo.ListIndex = -1
    If Trim(cod_postal) = "" Then
        lb_erro_cod_postal.Visible = True
       
    ElseIf Not IsNumeric(Len(tb_cod_postal) = 1) Or Not IsNumeric(Len(tb_cod_postal) = 2) Or Not IsNumeric(Len(tb_cod_postal) = 3) Or Not IsNumeric(Len(tb_cod_postal) = 4) Or Not IsNumeric(Len(tb_cod_postal) = 6) Or Not IsNumeric(Len(tb_cod_postal) = 7) Or Not IsNumeric(Len(tb_cod_postal) = 8) Then
        lb_erro_cod_postal.Visible = True
        lb_erro_cod_postal.Caption = "*Caracteres Inválidos"
       
    End If
   
    If Not IsNumeric(data_nasc) Then
        lb_erro_data_nasc.Visible = True
        lb_erro_data_nasc.Caption = "*Caracteres Inválidos"
       
    ElseIf Trim(data_nasc) = "" Then
        lb_erro_data_nasc.Visible = True
       
    End If
       
    lb_erro_sexo.Visible = True
          MsgBox "7"


Capturar.JPG
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
Your code has some details.
For example:

Trim(data_nasc) = "" Or Not IsNumeric(tb_data_nasc)
If Not IsNumeric(data_nasc) Then

I guess it must be:
Trim(tb_data_nasc) = "" Or Not IsNumeric(tb_data_nasc)
If Not IsNumeric(tb_data_nasc) Then

_____________________________________________________________
Case Trim(cod_postal) ...
If Trim(cod_postal) = "" Then

I guess it must be:
Case Trim(tb_cod_postal) ...
If Trim(tb_cod_postal) = "" Then

_____________________________________________________________


This is your condition.
What do you need that must match?
Rich (BB code):
  Case Trim(tb_morada) = "" And _
       Trim(tb_cod_postal) = "" Or _
       (Not IsNumeric(Len(tb_cod_postal) = 1) Or _
       Not IsNumeric(Len(tb_cod_postal) = 2) Or _
       Not IsNumeric(Len(tb_cod_postal) = 3) Or _
       Not IsNumeric(Len(tb_cod_postal) = 4) Or _
       Not IsNumeric(Len(tb_cod_postal) = 6) Or _
       Not IsNumeric(Len(tb_cod_postal) = 7) Or _
       Not IsNumeric(Len(tb_cod_postal) = 8)) And _
       Trim(data_nasc) = "" Or _
       Not IsNumeric(tb_data_nasc) And _
       cb_sexo.ListIndex = -1

And in this other condition, what must match?
Rich (BB code):
  Case Trim(cod_postal) = "" Or _
    (Not IsNumeric(Len(tb_cod_postal) = 1) Or _
    Not IsNumeric(Len(tb_cod_postal) = 2) Or _
    Not IsNumeric(Len(tb_cod_postal) = 3) Or _
    Not IsNumeric(Len(tb_cod_postal) = 4) Or _
    Not IsNumeric(Len(tb_cod_postal) = 6) Or _
    Not IsNumeric(Len(tb_cod_postal) = 7) Or _
    Not IsNumeric(Len(tb_cod_postal) = 8)) And _
    Trim(data_nasc) = "" Or _
    Not IsNumeric(data_nasc) And _
    cb_sexo.ListIndex = -1

____________________________________________________
Maybe you only need to validate the length and if the content is numeric, for example:
VBA Code:
  If Len(cod_postal) <> 8 Then
    'longitud inválida
  Else
    If Not IsNumeric(cod_potal) Then
      '*Caracteres Inválidos
    End If
  End If
____________________________________________________________
Note: Use the instruction: Option Explicit at the beginning of your code to validate the variables and names of the controls.
 
Upvote 0

Forum statistics

Threads
1,214,666
Messages
6,120,806
Members
448,990
Latest member
rohitsomani

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