简体   繁体   English

带有Excel数据库的VBA计数器

[英]VBA counter with excel database

I'm writing some macros for an excel worksheet that will add points to a database. 我正在为Excel工作表编写一些宏,这些宏会将点添加到数据库中。 The entries need to be unique with respect to their latitude and longitude values, so I've added aa 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. x是行计数器,lat和lon是用户输入的经/纬度,而coorAbort是带有错误消息的部分。 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? 有人有想法么?

I guess lat and lon are double values? 我猜latlon是双值吗? 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). (对于lon也要这样做)。 Never test double values directly for equality. 切勿直接测试双精度值是否相等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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