vba中条件满足后不运行下一句(Excel VBA 单元格满足一定条件时 停止运行并计数)

Excel,VBA,如果该条件发生错误,则运行下一条件Worksheets(3).Name,=,Worksheets(3).Cells(1,,9......

vba中条件满足后不运行下一句(Excel VBA 单元格满足一定条件时 停止运行并计数)

Excel VBA 如果该条件发生错误,则运行下一条件

Worksheets(3).Name = Worksheets(3).Cells(1, 9).Value & "(4)" On Error Resume Next Worksheets(3).Name = Worksheets(3).Cells(1, 9).Value & "(3)" On Error Resume Next Worksheets(3).Name = Worksheets(3).Cells(1, 9).Value & "(2)" On Error Resume Next Worksheets(3).Name = Worksheets(3).Cells(1, 9).Value 类似于上面这种方式,用if的方式去写

如果CELLS(1,9)里面有非法字符,你这些语句全部都要失败。如果是为了解决重名,可以先检测是否有这样名字的表,使用下面的自定义函数检查:

Function SheetExists(nm) as Boolean
    Dim st as WorkSheet
    set st=nothing
    on error resume next
    set st=sheets(nm)
    on error goto 0
    SheetExists = not st is nothing
End Function

主程序可以使用while一直循环,例如:

Dim i, nm
i=0
do
    nm = Worksheets(3).Cells(1, 9).Value
    if i>0 then nm = nm & "("& i &")"
    i=i+1
loop until not SheetExists(nm)
Worksheets(3).Name = nm

Excel VBA 单元格满足一定条件时,停止运行并计数!

请问,A1单元格输入=randbetween(0,10), A2单元格输入 =randbetween(0,10), A3 单元格输入 =and(A1>A2), A3的结果为TRUE或者是False。通常只能通过手工操作,比如删除一列,输入数据才能触发A1和A2的数据发生变化。现在想达到以下效果,能过VBA 自动使A1,A2单元格发生变化,且要求A3单元格在出现第N次TRUE时,A1,A2停止变化,并且记录下当A3单元格出现N次TRUE时,A1,A2同时运行了多少次,希望大侠帮忙!
Sub 判断符合条件的随机次数()
Dim i&, j&
i = 0: j = 0
Do
    [a1].Value = "=randbetween(0,10)"
    [a2].Value = "=randbetween(0,10)"
    j = j + 1
    If [a1].Value > [a2].Value Then
        i = i + 1
    End If
Loop Until i = 10 '在此设定符合判断条件的次数,这里暂时设定为10次
[A3].Value = "随机" & j & "次," & "A1>A2的情况出现" & i & "次"
End Sub

VBA 满足IF 但Then 不执行 每分拉 搜趣网只能说声谢谢!

Sub jewr()Dim i As IntegerDim j As IntegerDim y As StringDim x As StringDim a As Stringx = InputBox("ÇëÊäÈ&搜趣网#235;£Ð޺źóËÄλ:", "µ¼Èë")y = "JCWC000" & xActiveSheet.Cells([E65536].End(xlUp).Row + 1, 1).Selecti = Selection.Row()For j = 10 To 1 Step -1If y = Application.Workbooks("Database.xlsm").Worksheets("Role B").Cells(j, 1).Value Then MsgBox y a = Application.Workbooks("C:\Warranty Database\0Database.xlsm").Worksheets("Role B").Cells(j, 1).Value ThisWorkbook.Sheets(1).Cells(i, 1).Value = a a = Application.Workbooks("C:\Warranty Database\0Database.xlsm").Worksheets("Role B").Cells(j, 2).Value ThisWorkbook.Sheets(1).Cells(i, 2).Value = a a = Application.Workbooks("Database.xlsm").Worksheets("Role B").Cells(j, 3).Value ThisWorkbook.Sheets(1).Cells(i, 3).Value = a a = Application.Workbooks("Database.xlsm").Worksheets("Role B").Cells(j, 4).Value ThisWorkbook.Sheets(1).Cells(i, 4).Value = a a = Application.Workbooks("Database.xlsm").Worksheets("Role B").Cells(j, 4).Value ThisWorkbook.Sheets(1).Cells(i, 4).Value = a a = Application.Workbooks("Databawww.souquanme.comse.xlsm").Worksheets("Role B").Cells(j, 5).Value ThisWorkbook.Sheets(1).Cells(i, 5).Value = a a = Application.Workbooks("Database.xlsm").Worksheets("Role B").Cells(j, 6).Value ThisWorkbook.Sheets(1).Cells(i, 6).Value = a a = Application.Workbooks("Database.xlsm").Worksheets("Role B").Cells(j, 7).Value ThisWorkbook.Sheets(1).Cells(i, 7).Value = a a = Application.Workbooks("Database.xlsm").Worksheets("Role B").Cells(j, 8).Value ThisWorkbook.Sheets(1).Cells(i, 8).Value = a a = Application.Workbooks("Database.xlsm").Worksheets("Role B").Cells(j, 9).Value ThisWorkbook.Sheets(1).Cells(i, 9).Value = a a = Application.Workbooks("Database.xlsm").Worksheets("Role B").Cells(j, 10).Value ThisWorkbook.Sheets(1).Cells(i, 10).Value = aExit ForEnd IfNextEnd Sub
y = Application.Workbooks("Database.xlsm").Worksheets("Role B").Cells(j, 1).Value中的y和后面的数据类型不对,要满足相等必须大小写匹配,比如"a"<>"A"的,同时要注意是否有空格 ,excel中的单元格值得类型一定要能确定

VBA有没有mjgBk代码是可以直接退出不往下继续执行的语句??

想在调用过程判断的时候,出错就不往下继续执行了。

你好:

可以用下面的代码:

on error resume next
    if(www.souquanme.comiserror(condition)) then'condition中是你判断是否出错的条件
        exit sub'这是退出整个程序
    endif
on error goto 0

祝你成功!

相关推荐文章