vba 如果单元格内容已经通过,则下一次迭代

vba next iteration if cell content already passed

所以按照我的代码循环 table,连接到 sql 服务器并发送 table 的数据,我还有一个小限制: 我逐行、逐个单元地发送类似 5400 行和 13 列的数据(实际上并没有那么慢!)但我需要在某些单元格上设置条件:如果我已经发送了单元格的内容(例如:客户端号码已经发送到数据库中)我不想插入它两次,所以转到下一次迭代。 这是我的循环的简化版本:

nrow = 1

While My_range.Offset(nrow).Value <> ""  'continue if cell's not empty

'connect to the db and create a command that send the data

Set mobjCmd = New ADODB.Command
        With mobjCmd
        
            .ActiveConnection = mobjConn
            .CommandText = "INSERT_PRODUCT"
            .CommandType = adCmdStoredProc
            .CommandTimeout = 0

            'append the cell of the second row, first column in the db in the Client_id column  in my SQL table
 
            .Parameters.Append .CreateParameter("@Client_id", adChar, adParamInput, 255, Worksheets("My_Sheet").Range("A1").Offset(nrow, 0).Value)

            .Execute

Wend

基本上我想到了这样的事情:

If ... then GoTo NextIteration

End if

NextIteration: my_next_iteration

但是要设置什么 i/o“...”,这样如果它看到已经发送的 client_id,它就会进入下一次迭代。

在此先感谢您的帮助!

我已经遇到过几次这个问题,并且倾向于在每个循环中使用 Worksheetfunction.countif 来检查是否有任何相同图形的实例出现 above/below。

在你的情况下,这看起来像;

If WorksheetFunction.Countif(Range(Cells(1, MyRange.Column), Cells(nrow, MyRange.Column)), MyRange.Offset(nrow).Value) > 0 Then GoTo NextIteration

感谢@Spencer Barnes 的帮助。 Countif 函数是个好主意。另一种方式也是直接将存储过程修改为SQL。如果您的数据是干净的,您甚至可以使用 UPDATE 语句(如果存在 ... UPDATE)