VBA Excel error code

Alessandra Marchi Macedo 21 Reputation points
2022-02-22T10:54:05.193+00:00

Hi All
I have a code with error incompative values:
Sub Registro()

Dim x As Double
Dim sh As Worksheet
Dim V1 As Double
Dim V2 As Double


Set sh = ThisWorkbook.Sheets("Tabela Registros")

sh.Unprotect password:="Beto5651"
sh.Visible = xlSheetVisible
With sh

If Len(Userform1.txtValorArremat.Value) > 0 Then sh.Range("N2") = CDbl(Userform1.txtValorArremat)


If Len(Userform1.txtValorVenal.Value) > 0 Then sh.Range("O2") = CDbl(Userform1.txtValorVenal)



V1 = sh.Range("N3").Value
V2 = sh.Range("O3").Value

 End With


If Len(Userform1.txtValorArremat.Value) > 0 And Len(Userform1.txtValorVenal.Value) > 0 And CDbl(Userform1.txtValorVenal) > CDbl(Userform1.txtValorArremat) _
Then x = V1


If Len(Userform1.txtValorArremat.Value) > 0 And Len(Userform1.txtValorVenal.Value) > 0 And CDbl(Userform1.txtValorVenal) < CDbl(Userform1.txtValorArremat) _
Then x = V2

Userform1.txtRegistro = x


Userform1.txtRegistro.Text = Format(Userform1.txtRegistro.Value, "R$ #,###0.00")

end sub

Do you know what I did wrong?

{count} votes

Accepted answer
  1. Viorel 116.6K Reputation points
    2022-02-23T14:37:40.193+00:00

    Maybe you should write this:

    If Len(Userform1.txtValorArremat.Value) > 0 And Len(Userform1.txtValorVenal.Value) > 0 Then
       If CDbl(Userform1.txtValorVenal.Value) > CDbl(Userform1.txtValorArremat.Value) Then 
          x = V1
       End If
    End If
    
    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Alessandra Marchi Macedo 21 Reputation points
    2022-02-23T14:22:32.337+00:00

    Error in this line:

    If Len(Userform1.txtValorArremat.Value) > 0 And Len(Userform1.txtValorVenal.Value) > 0 And CDbl(Userform1.txtValorVenal) > CDbl(Userform1.txtValorArremat) _
    Then x = V1

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.