excel vba程序代码判断单元格值跳出循环
请大家帮忙看看如下代码,想要实现判断第一列从第五个单元格如果有数据就执行接下来的计算公式,如果第一列中的单元格没有数据就自动停止计算跳出所有循环。但我写的代码中不会判断第...
请大家帮忙看看如下代码,想要实现判断第一列从第五个单元格如果有数据就执行接下来的计算公式,如果第一列中的单元格没有数据就自动停止计算跳出所有循环。但我写的代码中不会判断第一列是否为空值,一直循环到循环结束。请问我写的有什么错误应该如何修改?
Sub 计算折扣()
Dim sh As Worksheet
Set sh = Worksheets("sheet1")
Dim i As Double
Dim n As Integer
Dim m As Integer
Dim o As Integer
i = Sheets("1").[T2]
For o = 5 To 45
For m = 5 To 45
For n = 5 To 45
If sh.Cells(o, 1) <> "" Then
If sh.Cells(m, 12) <> "" Then
sh.Cells(n, 11) = sh.Cells(n, 5) * sh.Cells(n, 10)
sh.Cells(m, 12) = sh.Cells(m, 11)
sh.Cells(n, 13) = sh.Cells(n, 12) * i
Else
sh.Cells(m, 12) = ""
End If
Else
Exit For
End If
Next
Next
Next
End Sub
我想要判断从第一列第五个单元格到45个单元格是否有空数值,如果有空数值就停止计算跳出循环,如果有数据就进行计算,所以我设置了三个变量 展开
Sub 计算折扣()
Dim sh As Worksheet
Set sh = Worksheets("sheet1")
Dim i As Double
Dim n As Integer
Dim m As Integer
Dim o As Integer
i = Sheets("1").[T2]
For o = 5 To 45
For m = 5 To 45
For n = 5 To 45
If sh.Cells(o, 1) <> "" Then
If sh.Cells(m, 12) <> "" Then
sh.Cells(n, 11) = sh.Cells(n, 5) * sh.Cells(n, 10)
sh.Cells(m, 12) = sh.Cells(m, 11)
sh.Cells(n, 13) = sh.Cells(n, 12) * i
Else
sh.Cells(m, 12) = ""
End If
Else
Exit For
End If
Next
Next
Next
End Sub
我想要判断从第一列第五个单元格到45个单元格是否有空数值,如果有空数值就停止计算跳出循环,如果有数据就进行计算,所以我设置了三个变量 展开
3个回答
展开全部
这样,把Exit For改成Exit Sub试试。
Sub 计算折扣()
Dim sh As Worksheet
Set sh = Worksheets("sheet1")
Dim i As Double
Dim n As Integer
Dim m As Integer
Dim o As Integer
i = Sheets("1").[T2]
For o = 5 To 45
For m = 5 To 45
For n = 5 To 45
If sh.Cells(o, 1) <> "" Then
If sh.Cells(m, 12) <> "" Then
sh.Cells(n, 11) = sh.Cells(n, 5) * sh.Cells(n, 10)
sh.Cells(m, 12) = sh.Cells(m, 11)
sh.Cells(n, 13) = sh.Cells(n, 12) * i
Else
sh.Cells(m, 12) = ""
End If
Else
Exit Sub
End If
Next
Next
Next
End Sub
追问
试过了,还是不停的循环。
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2017-06-14
展开全部
由于没有数据,只能大致写了下,你测试运行下吧
代码如下:
Sub 计算折扣()
Dim sh As Worksheet
Set sh = Worksheets("sheet1")
Dim i As Double
Dim n As Integer
Dim m As Integer
Dim o As Integer
i = Sheets("1").[T2]
If Range("A5") <> "" Then
For o = 5 To 45
For m = 5 To 45
For n = 5 To 45
If sh.Cells(m, 12) <> "" Then
sh.Cells(n, 11) = sh.Cells(n, 5) * sh.Cells(n, 10)
sh.Cells(m, 12) = sh.Cells(m, 11)
sh.Cells(n, 13) = sh.Cells(n, 12) * i
Else
sh.Cells(m, 12) = ""
End If
Next
Next
Next
Else
MsgBox "A5没有数据"
Exit Sub
End If
End Sub
更多追问追答
追答
Sub 计算折扣()
Dim sh As Worksheet
Set sh = Worksheets("sheet1")
Dim i As Double
Dim n As Integer
Dim m As Integer
Dim o As Integer
i = Sheets("1").[T2]
For o = 5 To 45
If sh.Cells(o, 1) <> "" Then
For m = 5 To 45
For n = 5 To 45
If sh.Cells(m, 12) <> "" Then
sh.Cells(n, 11) = sh.Cells(n, 5) * sh.Cells(n, 10)
sh.Cells(m, 12) = sh.Cells(m, 11)
sh.Cells(n, 13) = sh.Cells(n, 12) * i
Else
sh.Cells(m, 12) = ""
End If
Next
Next
Else
Exit For
End If
Next
End Sub
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你这个代码简化一下,就行了,用一个for循环就可以了。如果要退出多个循环,你最好把多个循环用不同的格式,比如for循环 或 do while 循环交替应用,退出是exit for 或 exit do 就可以了。
Sub 计算折扣()
Dim sh As Worksheet
Set sh = Worksheets("sheet1")
Dim i As Double
Dim o As Integer
i = Sheets("1").[T2]
For o = 5 To 45
If sh.Cells(o, 1) <> "" Then
If sh.Cells(o, 12) <> "" Then
sh.Cells(o, 11) = sh.Cells(o, 5) * sh.Cells(o, 10)
sh.Cells(o, 12) = sh.Cells(o, 11)
sh.Cells(o, 13) = sh.Cells(o, 12) * i
End If
Else
Exit for
End If
Next
End Sub
Sub 计算折扣()
Dim sh As Worksheet
Set sh = Worksheets("sheet1")
Dim i As Double
Dim o As Integer
i = Sheets("1").[T2]
For o = 5 To 45
If sh.Cells(o, 1) <> "" Then
If sh.Cells(o, 12) <> "" Then
sh.Cells(o, 11) = sh.Cells(o, 5) * sh.Cells(o, 10)
sh.Cells(o, 12) = sh.Cells(o, 11)
sh.Cells(o, 13) = sh.Cells(o, 12) * i
End If
Else
Exit for
End If
Next
End Sub
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |