Excel中的一组数据如下所示:
X1 <- X
#some operations on X
for(i in 1:n){
for(j in 1:p){
if(X[i,j]< 0){
X[i,j]<-X1[i,j]
}
}
}
最后一列是检查数据是否已加载到SQL Server。
我编写了代码来迭代范围并将值插入SQL表中。在这段代码中,它还会检查最后一列,如果有一个Y,它应该跳过迭代并转到下一列..
它给了我一个错误,说&#34;否则没有&#34;。
Test1 12345678 1906 John GY DFS H1C Y
Test2 12345678 1806 Jack GY GQ H1C Y
Test3 12345678 1706 Kate GY GQ H1C Y
Test4 12345678 1606 Sawyer GY GQ H1C
我真的很想弄明白我哪里出错了。代码完全正常,无需检查&#34; Y&#34;在那里......
感谢您的帮助。
答案 0 :(得分:1)
尝试以下
NextIteration
,您需要验证它现在位于正确的位置。避免调用子连接并使用不那么模糊的编译器
Public Sub My_Connection()
Dim Conn As ADODB.Connection
Dim Command As ADODB.Command
Set Conn = New ADODB.Connection
Set Command = New ADODB.Command
Dim i As Long
Dim rownumber As Long
rownumber = Worksheets("Sheet1").Range("A1048576").End(xlUp).Row
Conn.ConnectionString = "Provider=SQLOLEDB; Data Source=[Server];Initial Catalog=[DB];User ID=[user];Password=[Password]; Trusted_Connection=no"
Conn.Open
Command.ActiveConnection = Conn
For i = 1 To rownumber 'rows
If ActiveSheet.Cells(i, 8).Value = "Y" Then
GoTo NextIteration
Else
Command.CommandText = "INSERT INTO [Database] (" & _
"[Col1], [Col2], [Col3], [Col4], [Col5], [Col6], [Col7])" & _
"VALUES (" & _
"'" & ActiveSheet.Cells(i, 1).Value & "'," & _
"'" & ActiveSheet.Cells(i, 2).Value & "'," & _
"'" & ActiveSheet.Cells(i, 3).Value & "'," & _
"'" & ActiveSheet.Cells(i, 4).Value & "'," & _
"'" & ActiveSheet.Cells(i, 5).Value & "'," & _
"'" & ActiveSheet.Cells(i, 6).Value & "'," & _
"'" & ActiveSheet.Cells(i, 7).Value & "')"
Command.Execute
ActiveSheet.Cells(i, 8).Value = "Y"
End If
NextIteration:
Next i
Conn.Close
Set Conn = Nothing
End Sub
答案 1 :(得分:0)
在&#39;然后&#39;之后添加一个新行。在第一个if语句或elses vba implicity关闭if语句。