excel里的,求問各位大神為什么這樣會next沒有for呢
Private Sub addBtn_Click()
Dim cnn As New ADODB.Connection
Dim myStr As String
cnn.ConnectionString = "Provider=SQLOLEDB;User ID=sa;Password=12345678;Server=LAPTOP-4B0N1Q41\SQLEXPRESS;Database=contract"
cnn.Open
Set Rs_Cate = conn.Execute("SELECT conNum from list")
Arr_Cate = Rs_Cate.GetRows
Set Rs_Cate = Nothing
Dim Arr_CateNumS, Arr_CateNumI
Arr_CateNumS = UBound(Arr_Cate, 2) '得到陣列中資料的下標
For i = 3 To Range("A65536").End(xlUp).Row
For Arr_CateNumI = 0 To Arr_CateNumS
If Cells(i, 2).Value <> "&Arr_Cate(0,Arr_CateNumI)&" Then
Next Arr_CateNumI
ElseIf Cells(i, 2).Value = "&Arr_Cate(0,Arr_CateNumI)&" Then
Next i
End If
uj5u.com熱心網友回復:
流程結構錯誤!你這樣寫的代碼,For陳述句塊與If陳述句塊的范圍形成“交叉”了!
各種陳述句塊的“作用范圍”,只能是“嵌套關系”(或說:包含關系),不能交叉的。
uj5u.com熱心網友回復:
那 如果我要實作符合字串不相等條件就繼續回圈下去,得怎么辦呢
uj5u.com熱心網友回復:
你這兩個For回圈,什么有用的事也沒做,就只是在那兒“回圈”了……有意義嗎!!!你不如直接說明白你的目的是什么,讓別人給你個合適的建議。
或者:你別自以為是的把“流程”抽象出來、把“自認為無關”的代碼洗掉了,
反而讓別人覺得你的這段代碼就是莫明其妙的。

uj5u.com熱心網友回復:
哪個回圈要“繼續”? 說清楚啊……
uj5u.com熱心網友回復:
要不然,你看看這樣的流程,是否能達到你的目的:For i = 3 To Range("A65536").End(xlUp).Row
For Arr_CateNumI = 0 To Arr_CateNumS
If Cells(i, 2).Value = "&Arr_Cate(0,Arr_CateNumI)&" Then Exit For
' 其它操作…………
Next Arr_CateNumI
' 其它操作…………
Next i
uj5u.com熱心網友回復:
流程結構錯誤!
你這樣寫的代碼,For陳述句塊與If陳述句塊的范圍形成“交叉”了!
各種陳述句塊的“作用范圍”,只能是“嵌套關系”(或說:包含關系),不能交叉的。
那 如果我要實作符合字串不相等條件就繼續回圈下去,得怎么辦呢
哪個回圈要“繼續”? 說清楚啊……
抱歉抱歉,我是想回圈找到excel表里資料庫沒有的資料,因為他們是直接在excel表下空白處開始添加的,所以找到了那一行資料庫里不存在的就從那一行開始一行一行添加下去
Private Sub addBtn_Click()
Dim cnn As New ADODB.Connection
Dim myStr As String
cnn.ConnectionString = "Provider=SQLOLEDB;User ID=sa;Password=12345678;Server=LAPTOP-4B0N1Q41\SQLEXPRESS;Database=contract"
cnn.Open
Set Rs_Cate = conn.Execute("SELECT conNum from list")
Arr_Cate = Rs_Cate.GetRows
Set Rs_Cate = Nothing
Dim Arr_CateNumS, Arr_CateNumI
Arr_CateNumS = UBound(Arr_Cate, 2) '得到陣列中資料的下標
For i = 3 To Range("A65536").End(xlUp).Row
For Arr_CateNumI = 0 To Arr_CateNumS
If Cells(i, 2).Value <> "&Arr_Cate(0,Arr_CateNumI)&" Then
Next Arr_CateNumI
ElseIf Cells(i, 2).Value = "&Arr_Cate(0,Arr_CateNumI)&" Then
Next i
End If
Dim S As Integer
For S = i To Range("A65536").End(xlUp).Row
For l = 1 To Range("IV1").End(xlToLeft).Column
If IsNull(Cells(S, l)) = True Then
Cells(S, l) = Null
Next l
End If
Sql = "insert into list values('" & Cells(S, 2) & "','" & Cells(S, 3) _
& "','" & Cells(S, 4) & "','" & Cells(S, 5) & "','" & Cells(S, 6) & "'," & Cells(S, 7) & "," & Cells(S, 8) & ",'" & Cells(S, 9) & "','" & Cells(S, 10) & "'," & Cells(S, 11) & "," & Cells(S, 12) & ",'" & Cells(S, 13) & "', '" & Cells(S, 14) & "', '" & Cells(S, 15) & "','" & Cells(S, 16) & "'," & Cells(S, 17) & ",'" & Cells(S, 18) & "'," & Cells(S, 19) & "," & Cells(S, 20) & "," & Cells(S, 21) & "," & Cells(S, 22) & "," & Cells(S, 23) & ",'" & Cells(S, 24) & "','" & Cells(S, 25) & "','" & Cells(S, 26) _
& "','" & Cells(S, 27) & "','" & Cells(S, 28) & "','" & Cells(S, 29) & "','" & Cells(S, 30) & "'," & Cells(S, 31) & "," & Cells(S, 32) & "," & Cells(S, 33) & "," & Cells(S, 34) & "," & Cells(S, 35) & "," & Cells(S, 36) & "," & Cells(S, 37) & "," & Cells(S, 38) & "," & Cells(S, 39) & "," & Cells(S, 40) & "," & Cells(S, 41) & "," & Cells(S, 42) & "," & Cells(S, 43) & "," & Cells(S, 44) _
& "," & Cells(S, 45) & "," & Cells(S, 46) & "," & Cells(S, 47) & ",'" & Cells(S, 48) & "','" & Cells(S, 49) & "')"
cnn.Execute Sql
Next S
MsgBox "資料添加成功!", vbExclamation
Dim xRs As New ADODB.Recordset
Sql = " Select * from list"
xRs.Open Sql, cnn, 1, 3
Sheet2.Range(Sheet2.Range("A3"), Sheet2.Range("A2").End(xlDown)).EntireRow.Clear
Sheet2.Range("A3").CopyFromRecordset xRs
xRs.Close
cnn.Close
Set cnn = Nothing
End Sub
uj5u.com熱心網友回復:
你的“條件”就根本不對。"&Arr_Cate(0,Arr_CateNumI)&" 只是表示“&Arr_Cate(0,Arr_CateNumI)&”這個字串,
跟你的“資料庫查詢結果”沒有半毛錢關系。

若是你的“查詢結果”無誤的話,應該用這樣的流程:
For i = 3 To Range("A65536").End(xlUp).Row
For Arr_CateNumI = 0 To Arr_CateNumS
If Cells(i, 2).Value = Arr_Cate(0, Arr_CateNumI) Then Exit For
Next
If (Arr_CateNumI > Arr_CateNumS) Then
' 沒有找到,執行“添加操作”……
' 添加記錄的代碼自己寫
'
End If
Next
uj5u.com熱心網友回復:
你的“條件”就根本不對。
"&Arr_Cate(0,Arr_CateNumI)&" 只是表示“&Arr_Cate(0,Arr_CateNumI)&”這個字串,
跟你的“資料庫查詢結果”沒有半毛錢關系。
嗯嗯,我改了代碼,覺得我原來做的很繁雜很亂,真的謝謝你幫我看下去了qwq,不過我改了之后,彈出提示是要求物件,這是為什么呢?
Private Sub addBtn_Click()
Dim cnn As New ADODB.Connection
Dim myStr As String
cnn.ConnectionString = "Provider=SQLOLEDB;User ID=sa;Password=12345678;Server=LAPTOP-4B0N1Q41\SQLEXPRESS;Database=contract"
cnn.Open
Dim rs_cate As Variant, arr_cate As Variant
Set rs_cate = conn.Execute("SELECT conNum from list")
arr_cate = rs_cate.GetRows
Set rs_cate = Nothing
Dim Arr_CateNumS, Arr_CateNumI, i As Integer
Arr_CateNumS = UBound(arr_cate, 2) '得到陣列中資料的下標
For i = 3 To Range("A65536").End(xlUp).Row
For Arr_CateNumI = 0 To Arr_CateNumS
If Cells(i, 2).Value = arr_cate(0, Arr_CateNumI) Then Exit For
Next
If (Arr_CateNumI > Arr_CateNumS) Then
Sql = "insert into list values('" & Cells(i, 2) & "','" & Cells(i, 3) _
& "','" & Cells(i, 4) & "','" & Cells(i, 5) & "','" & Cells(i, 6) & "'," & Cells(i, 7) & "," & Cells(i, 8) & ",'" & Cells(i, 9) & "','" & Cells(i, 10) & "'," & Cells(i, 11) & "," & Cells(i, 12) & ",'" & Cells(i, 13) & "', '" & Cells(i, 14) & "', '" & Cells(i, 15) & "','" & Cells(i, 16) & "'," & Cells(i, 17) & ",'" & Cells(i, 18) & "'," & Cells(i, 19) & "," & Cells(i, 20) & "," & Cells(i, 21) & "," & Cells(i, 22) & "," & Cells(i, 23) & ",'" & Cells(i, 24) & "','" & Cells(i, 25) & "','" & Cells(i, 26) _
& "','" & Cells(i, 27) & "','" & Cells(i, 28) & "','" & Cells(i, 29) & "','" & Cells(i, 30) & "'," & Cells(i, 31) & "," & Cells(i, 32) & "," & Cells(i, 33) & "," & Cells(i, 34) & "," & Cells(i, 35) & "," & Cells(S, 36) & "," & Cells(S, 37) & "," & Cells(S, 38) & "," & Cells(S, 39) & "," & Cells(S, 40) & "," & Cells(S, 41) & "," & Cells(S, 42) & "," & Cells(S, 43) & "," & Cells(S, 44) _
& "," & Cells(i, 45) & "," & Cells(i, 46) & "," & Cells(i, 47) & ",'" & Cells(i, 48) & "','" & Cells(i, 49) & "')"
cnn.Execute Sql
End If
Next
MsgBox "資料添加成功!", vbExclamation
Dim xRs As New ADODB.Recordset
Sql = " Select * from list"
xRs.Open Sql, cnn, 1, 3
Sheet2.Range(Sheet2.Range("A3"), Sheet2.Range("A2").End(xlDown)).EntireRow.Clear
Sheet2.Range("A3").CopyFromRecordset xRs
xRs.Close
cnn.Close
Set cnn = Nothing
End Subuj5u.com熱心網友回復:
在執行哪句時提示錯誤?
你說明白點不行嗎!!!!!
![]()
uj5u.com熱心網友回復:
在執行哪句時提示錯誤?
你說明白點不行嗎!!!!!
執行整段代碼的時候,彈出個對話框,運行時錯誤424 要求物件,也沒有標示是那句出了錯了的uj5u.com熱心網友回復:
出現提示的時候,你點“除錯”,它會定位到出錯的陳述句上啊!
![]()
uj5u.com熱心網友回復:
出現提示的時候,你點“除錯”,它會定位到出錯的陳述句上啊!
arr_cate = rs_cate.GetRows
是這個錯了,我后來改了代碼,把原本的Dim rs_cate As Variant刪了,改成了Dim rs_cate As New ADODB.Recordset除錯
![]()
uj5u.com熱心網友回復:
我這里是想要把查詢出來的conNum存入一個陣列中的uj5u.com熱心網友回復:
這時候就發現python強制縮進的好處了。一個一個代碼塊你寫好縮進自己捋都能捋得出哪里出錯,甚至寫的時候自己就發現不對勁了轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/43618.html
標籤:VBA
上一篇:抓取查快遞資料

