Brendan
Brendan

Reputation: 164

VBA counter with excel database

I'm writing some macros for an excel worksheet that will add points to a database. The entries need to be unique with respect to their latitude and longitude values, so I've added a a few lines of code that will check the lat/long of an entry after a user has tried to enter it. It is intended to check the lat/long of a pending entry against all the ones in the database. If it matches one, it will return an error. Here is the code:

x = 2
Do Until (Worksheets("DBase").Cells(x, 3).Value = "")
     If Worksheets("DBase").Cells(x, 3).Value = lat And Worksheets("DBase").Cells(x,
4).Value = lon Then
          GoTo coorAbort
     Else
          x = x + 1
     End If
Loop

x is the row counter, lat and lon are the lat/long's inputted by the user, and coorAbort is the section with the error message. Everything works fine except the counter to progress the loop to the next row doesn't seem to be working. It checks the second row entry and then exits the loop and moves on to the next part of the code. I'm sure I have something small messed up, I just can't seem to pick it out. Anyone have any ideas?

Upvotes: 0

Views: 643

Answers (1)

Doc Brown
Doc Brown

Reputation: 20054

I guess lat and lon are double values? If so, you should replace

If Worksheets("DBase").Cells(x, 3).Value = lat

by

If Abs(Worksheets("DBase").Cells(x, 3).Value - lat)<1e-6

(and for lon do the same). Never test double values directly for equality.

Upvotes: 3

Related Questions