使用 SQL 语句作为我的 While 的 VBA While 循环

发布于 2024-09-30 01:16:52 字数 743 浏览 0 评论 0原文

我正在一些电子表格后面编写一些代码,并且需要循环执行一些代码,例如从数据库获取数据执行一些公式并将数据移动到新工作表。我从数据库获取数据的代码是获取多个列中的所有值,其中数据尚未报告并且具有相同的文件名(数据来自文件),我有一个字段说明是否它由一个简单的“Y”或“N”以及一个保存其来源的文件名的字段来报告。

所以我需要一段时间,“WHILE”有尚未报告的数据来完成我的其余代码(这包括我所说的第一个 SQL 语句,因为它获取尚未从每个单独的文件名报告的数据)。

我已经尝试让它工作好几天了,但一直不知道如何做,所以任何帮助将非常感激。

更新:

数据库有一个名为 datareported 的实体,可以是“N”或“Y”,还有 datadatfile,它是数据来源的名称。

因此,

WHILE datareported = 'N' THEN
"SELECT (the data rows I want)
FROM tbldata
WHERE datareported='N' and datadatfile =
(SELECT min(datadatfile)
    FROM tbldata WHERE datareported='N')"

这意味着我会循环执行代码的其余部分,同时存在尚未报告的数据,并且仅引入同名的数据(来自 datadatfile ),以便可以在该数据上运行代码。

这基本上就是我想做的,也是我尝试过的。我尝试了其他一些方法,通常会返回类型不匹配的结果。

干杯,

萨姆

I'm writing some code behind some spreadsheets and I need to loop through some code, like getting data from a database doing some formulas and moving the data to a new sheet. My code for getting the data from the database is getting all of the values in multiple columns where the data has not been reported and has the same file name ( the data is coming from a file ), I have a field which states whether or not it has be reported by a simple "Y" or "N", and a field which holds the filename it came from.

So I need a while that, "WHILE" there's data that hasn't been reported do the rest of my code ( this includes my first SQL statement I said as this get data that hasn't been reported from each individual filename ).

I've been trying to get this to work for days but just have not been able to figure out how, so any help would be very grateful.

Update:

Database has a entity called datareported, can either be "N" or "Y", and also datadatfile which is the name from which the data came from.

So,

WHILE datareported = 'N' THEN
"SELECT (the data rows I want)
FROM tbldata
WHERE datareported='N' and datadatfile =
(SELECT min(datadatfile)
    FROM tbldata WHERE datareported='N')"

This means that I loop through the rest of my code WHILE there is data that hasn't been reported and only bring in data of the same name ( from datadatfile ) so that the code can be run on that data.

That's basically what I want to do and that's pretty much what I have tried. I have tried a few other things and normally get a return of type mis-match.

Cheers,

Sam

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

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

发布评论

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

评论(1

瀟灑尐姊 2024-10-07 01:16:52

解决方案需要更多信息,但这可能有助于推动您走向正确的方向...

'ws1 = worksheet object...
bC = true
cnt1 = 2 ' starting row
Do While bC

If ws1.cells(cnt1, 1) = "N" Then
'Run your SQL here
Else
'Already reported...
End If
cnt1 = cnt1 + 1
If 'you hit the last row on your worksheet set bC = False to exit loop      
Loop

错过了更新...这不会有帮助,抱歉。

More information needed for a solution but this may be of some help to push you in the right direction...

'ws1 = worksheet object...
bC = true
cnt1 = 2 ' starting row
Do While bC

If ws1.cells(cnt1, 1) = "N" Then
'Run your SQL here
Else
'Already reported...
End If
cnt1 = cnt1 + 1
If 'you hit the last row on your worksheet set bC = False to exit loop      
Loop

Missed the update...this wont help, sorry.

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