ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

搜索
EH技术汇-专业的职场技能充电站 妙哉!函数段子手趣味讲函数 Excel服务器-会Excel,做管理系统 效率神器,一键搞定繁琐工作
HR薪酬管理数字化实战 Excel 2021函数公式学习大典 Excel数据透视表实战秘技 打造核心竞争力的职场宝典
让更多数据处理,一键完成 数据工作者的案头书 免费直播课集锦 ExcelHome出品 - VBA代码宝免费下载
用ChatGPT与VBA一键搞定Excel WPS表格从入门到精通 Excel VBA经典代码实践指南
查看: 3684|回复: 12

[求助] 为什么next没有for?

[复制链接]

TA的精华主题

TA的得分主题

发表于 2018-8-6 11:35 | 显示全部楼层 |阅读模式
大神们,我想要if符合条件的时候做错next进行自增继续循环下去的操作,该怎么改呢?
  1. Dim cnn As New ADODB.Connection
  2. Dim myStr As String
  3. cnn.ConnectionString = "Provider=SQLOLEDB;User ID=sa;Password=12345678;Server=LAPTOP-4B0N1Q41\SQLEXPRESS;Database=contract"
  4. cnn.Open
  5. Set Rs_Cate = conn.Execute("SELECT conNum from list")
  6. Arr_Cate = Rs_Cate.GetRows
  7. Set Rs_Cate = Nothing
  8. Dim Arr_CateNumS, Arr_CateNumI
  9. Arr_CateNumS = UBound(Arr_Cate, 2) '得到数组中数据的下标
  10. For i = 3 To Range("A65536").End(xlUp).Row
  11. For Arr_CateNumI = 0 To Arr_CateNumS
  12. If Cells(i, 2).Value <> "&Arr_Cate(0,Arr_CateNumI)&" Then
  13. Arr_CateNumI = arr_cetenumi + 1
  14. ElseIf Cells(i, 2).Value = "&Arr_Cate(0,Arr_CateNumI)&" Then
  15. Next i
  16. End If
  17. Dim S As Integer
  18. For S = i To Range("A65536").End(xlUp).Row
  19. For l = 1 To Range("IV1").End(xlToLeft).Column
  20. If IsNull(Cells(S, l)) = True Then
  21. Cells(S, l) = Null
  22. l = l + 1
  23. End If
复制代码


TA的精华主题

TA的得分主题

 楼主| 发表于 2018-8-6 11:36 | 显示全部楼层
原来还试过这样的
  1. Dim cnn As New ADODB.Connection
  2. Dim myStr As String
  3. cnn.ConnectionString = "Provider=SQLOLEDB;User ID=sa;Password=12345678;Server=LAPTOP-4B0N1Q41\SQLEXPRESS;Database=contract"
  4. cnn.Open
  5. Set Rs_Cate = conn.Execute("SELECT conNum from list")
  6. Arr_Cate = Rs_Cate.GetRows
  7. Set Rs_Cate = Nothing
  8. Dim Arr_CateNumS, Arr_CateNumI
  9. Arr_CateNumS = UBound(Arr_Cate, 2) '得到数组中数据的下标
  10. For i = 3 To Range("A65536").End(xlUp).Row
  11. For Arr_CateNumI = 0 To Arr_CateNumS
  12. If Cells(i, 2).Value <> "&Arr_Cate(0,Arr_CateNumI)&" Then
  13. Next Arr_CateNumI
  14. ElseIf Cells(i, 2).Value = "&Arr_Cate(0,Arr_CateNumI)&" Then
  15. Next i
  16. End If
复制代码

TA的精华主题

TA的得分主题

发表于 2018-8-6 11:37 | 显示全部楼层

TA的精华主题

TA的得分主题

 楼主| 发表于 2018-8-6 11:38 | 显示全部楼层

TA的精华主题

TA的得分主题

发表于 2018-8-6 11:43 | 显示全部楼层

TA的精华主题

TA的得分主题

发表于 2018-8-6 11:45 | 显示全部楼层
for i=1 to 10
     if  i=5 then goto haha else msgbox 1/(i-5)
haha:
next

TA的精华主题

TA的得分主题

 楼主| 发表于 2018-8-6 11:55 | 显示全部楼层
zopey 发表于 2018-8-6 11:45
for i=1 to 10
     if  i=5 then goto haha else msgbox 1/(i-5)
haha:

好的 我试一下!谢谢!
头像被屏蔽

TA的精华主题

TA的得分主题

发表于 2018-8-6 12:32 | 显示全部楼层

TA的精华主题

TA的得分主题

 楼主| 发表于 2018-8-6 12:45 | 显示全部楼层
mikezhan 发表于 2018-8-6 12:32
IF 语句 和 FOR 循环语句,不能交叉嵌套!!!!!!!!!!!!

if...  then ...

嗯嗯,我现在明白是不能交叉嵌套了,但是我想要在if条件符合后面的语句后让i值或arr那个值自增,继续循环,请问有什么实现方法吗?我按照楼上的那位老师改了个goto语句,弹出错误提示是要求对象
  1. Private Sub addBtn_Click()
  2. Dim cnn As New ADODB.Connection
  3. Dim myStr As String
  4. cnn.ConnectionString = "Provider=SQLOLEDB;User ID=sa;Password=12345678;Server=LAPTOP-4B0N1Q41\SQLEXPRESS;Database=contract"
  5. cnn.Open
  6. Dim rs_cate As Variant, arr_cate As Variant
  7. Set rs_cate = conn.Execute("SELECT conNum from list")
  8. arr_cate = rs_cate.GetRows
  9. Set rs_cate = Nothing
  10. Dim Arr_CateNumS, Arr_CateNumI, i As Integer
  11. Arr_CateNumS = UBound(arr_cate, 2) '得到数组中数据的下标
  12. For i = 3 To Range("A65536").End(xlUp).Row
  13. For Arr_CateNumI = 0 To Arr_CateNumS
  14. If Cells(i, 2).Value <> "&Arr_Cate(0,Arr_CateNumI)&" Then
  15. GoTo next1
  16. ElseIf Cells(i, 2).Value = "&Arr_Cate(0,Arr_CateNumI)&" Then
  17. GoTo next2
  18. End If
  19. next1: Next Arr_CateNumI
  20. next2: Next i
  21. Dim S As Integer, l As Integer
  22. For S = i To Range("A65536").End(xlUp).Row
  23. For l = 1 To Range("IV1").End(xlToLeft).Column
  24. If IsNull(Cells(S, l)) = True Then
  25. Cells(S, l) = Null
  26. End If
  27. Next l
  28. Sql = "insert into list values('" & Cells(S, 2) & "','" & Cells(S, 3) _
  29.                 & "','" & 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) _
  30.                 & "','" & 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) _
  31.                 & "," & Cells(S, 45) & "," & Cells(S, 46) & "," & Cells(S, 47) & ",'" & Cells(S, 48) & "','" & Cells(S, 49) & "')"
  32. cnn.Execute Sql
  33. Next S
  34. MsgBox "数据添加成功!", vbExclamation
  35. Dim xRs As New ADODB.Recordset
  36.     Sql = " Select * from list"
  37.     xRs.Open Sql, cnn, 1, 3
  38.      Sheet2.Range(Sheet2.Range("A3"), Sheet2.Range("A2").End(xlDown)).EntireRow.Clear
  39.     Sheet2.Range("A3").CopyFromRecordset xRs
  40.     xRs.Close
  41. cnn.Close
  42. Set cnn = Nothing
  43. End Sub
复制代码

TA的精华主题

TA的得分主题

发表于 2018-8-6 14:15 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
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

评分

1

查看全部评分

您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

手机版|关于我们|联系我们|ExcelHome

GMT+8, 2025-1-11 17:12 , Processed in 0.025620 second(s), 12 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

沪公网安备 31011702000001号 沪ICP备11019229号-2

本论坛言论纯属发表者个人意见,任何违反国家相关法律的言论,本站将协助国家相关部门追究发言者责任!     本站特聘法律顾问:李志群律师

快速回复 返回顶部 返回列表