Comparing values

pauloalex

New Member
Joined
Jan 16, 2021
Messages
11
Office Version
  1. 365
Platform
  1. Windows
Hello, everybody.

I'm trying to do a comparison between what the user inserted in this form:

1659550079195.png


And the values present in the last line of this table:

1659550189253.png


If all of them are equals, so the Excel will alert "this line was registered already", but if one or more value is different, than a new line is create with the values from the form.

I tried to do it using the folloing code:

VBA Code:
ThisWorkbook.Sheets("cabos").Cells(1, 7).Select

    Do Until IsEmpty(ActiveCell) = True
        ActiveCell.Offset(1, 0).Select
        r = r + 1
    Loop

If Not ((ThisWorkbook.Sheets("cabos").Range("B" & r).Value = form_cabos.campo_regiao.Value) Or _
(ThisWorkbook.Sheets("cabos").Range("C" & r).Value = form_cabos.campo_endereco.Value) Or _
(ThisWorkbook.Sheets("cabos").Range("D" & r).Value = form_cabos.campo_relatorio.Value) Or _
(ThisWorkbook.Sheets("cabos").Range("E" & r).Value = form_cabos.campo_tipo.Value) Or _
(ThisWorkbook.Sheets("cabos").Range("F" & r).Value = form_cabos.campo_metragem.Value) Or _
(ThisWorkbook.Sheets("cabos").Range("G" & r).Value = form_cabos.campo_mesano.Value)) Then

ThisWorkbook.Sheets("cabos").Cells(r + 1, 2) = form_cabos.campo_regiao.Value
ThisWorkbook.Sheets("cabos").Cells(r + 1, 3) = form_cabos.campo_endereco.Value
ThisWorkbook.Sheets("cabos").Cells(r + 1, 4) = form_cabos.campo_relatorio.Value
ThisWorkbook.Sheets("cabos").Cells(r + 1, 5) = form_cabos.campo_tipo.Value
ThisWorkbook.Sheets("cabos").Cells(r + 1, 6) = form_cabos.campo_metragem.Value
ThisWorkbook.Sheets("cabos").Cells(r + 1, 7) = form_cabos.campo_mesano.Value
ID = ThisWorkbook.Sheets("cabos").Range("A" & r).Value + 1
ThisWorkbook.Sheets("cabos").Cells(r + 1, 1) = ID

Else

MsgBox "Item já cadastrado."
Exit Sub

End If

But it isn't working properly. It is inserting new lines without following the condition.. I don't know how to fix it.

PS.: The text and images are in Portuguese.. I hope that it won't bother anyone..

Thank you all for the attention, in advanced.
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
Hello, everybody.

I solved my problem. Was quite simple. I just wrote:

VBA Code:
If (StrComp(CStr(ThisWorkbook.Sheets("cabos").Range("B" & r).Value), CStr(form_cabos.campo_regiao.Value), vbTextCompare) Or _
StrComp(CStr(ThisWorkbook.Sheets("cabos").Range("C" & r).Value), CStr(form_cabos.campo_endereco.Value), vbTextCompare) Or _
StrComp(CStr(ThisWorkbook.Sheets("cabos").Range("D" & r).Value), CStr(form_cabos.campo_relatorio.Value), vbTextCompare) Or _
StrComp(CStr(ThisWorkbook.Sheets("cabos").Range("E" & r).Value), CStr(form_cabos.campo_tipo.Value), vbTextCompare) Or _
StrComp(CStr(ThisWorkbook.Sheets("cabos").Range("F" & r).Value), CStr(form_cabos.campo_metragem.Value), vbTextCompare)) Then

ThisWorkbook.Sheets("cabos").Cells(r + 1, 2) = form_cabos.campo_regiao.Value
ThisWorkbook.Sheets("cabos").Cells(r + 1, 3) = form_cabos.campo_endereco.Value
ThisWorkbook.Sheets("cabos").Cells(r + 1, 4) = form_cabos.campo_relatorio.Value
ThisWorkbook.Sheets("cabos").Cells(r + 1, 5) = form_cabos.campo_tipo.Value
ThisWorkbook.Sheets("cabos").Cells(r + 1, 6) = form_cabos.campo_metragem.Value
ThisWorkbook.Sheets("cabos").Cells(r + 1, 7) = form_cabos.campo_mesano.Value
ID_cabos = ThisWorkbook.Sheets("cabos").Range("A" & r).Value + 1
ThisWorkbook.Sheets("cabos").Cells(r + 1, 1) = ID_cabos

MsgBox ThisWorkbook.Sheets("cabos").Range("C" & r).Value
MsgBox form_cabos.campo_endereco.Value


Else

MsgBox "Item já cadastrado."
Exit Sub

End If

End If
 
Upvote 0
Solution

Forum statistics

Threads
1,214,865
Messages
6,121,988
Members
449,060
Latest member
mtsheetz

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