嗯嗯,我现在明白是不能交叉嵌套了,但是我想要在if条件符合后面的语句后让i值或arr那个值自增,继续循环,请问有什么实现方法吗?我按照楼上的那位老师改了个goto语句,弹出错误提示是要求对象
- 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
- GoTo next1
- ElseIf Cells(i, 2).Value = "&Arr_Cate(0,Arr_CateNumI)&" Then
- GoTo next2
- End If
- next1: Next Arr_CateNumI
- next2: Next i
- Dim S As Integer, l 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
- End If
- Next l
- 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
复制代码 |