带 Excel 数据库的 VBA 计数器

发布于 2024-12-10 16:46:21 字数 552 浏览 0 评论 0原文

我正在为 Excel 工作表编写一些宏,将点添加到数据库中。这些条目的纬度和经度值必须是唯一的,因此我添加了几行代码,用于在用户尝试输入条目后检查条目的纬度/经度。它的目的是根据数据库中的所有条目检查待处理条目的纬度/经度。如果匹配,就会返回错误。代码如下:

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 是行计数器,lat 和 lon 是用户输入的纬度/经度,coorAbort 是包含错误消息的部分。一切工作正常,除了将循环推进到下一行的计数器似乎不起作用。它检查第二行条目,然后退出循环并继续执行代码的下一部分。我确信我有一些小事情搞砸了,但我似乎无法把它找出来。有人有什么想法吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

傾城如夢未必闌珊 2024-12-17 16:46:21

我猜 latlon 是双精度值?如果是这样,您应该替换

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

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

(并且对于 lon 执行相同的操作)。切勿直接测试 double 值是否相等。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文