gpt4 book ai didi

VBA:跳出for循环

转载 作者:行者123 更新时间:2023-12-04 21:26:29 25 4
gpt4 key购买 nike

我如何实现以下目标?

Sub Macro1()
'
' Macro1 Macro
'

'
Worksheets("Drop-down").Select
n = Cells(1, 1).End(xlDown).Row
For i = 1 To n
ActiveSheet.Cells(i, 2).Select
*******************************************************
If Worksheets("Misc").Cells(2, i).Value = "" Then
continue i
End If
*******************************************************
If Worksheets("Misc").Cells(3, i).Value <> "" Then
Set validationRange = Range(Worksheets("Misc").Cells(2, i), Worksheets("Misc").Cells(2, i).End(xlDown))
Else
Set validationRange = Worksheets("Misc").Cells(2, i)
End If
With Selection.Validation
.Delete
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
xlBetween, Formula1:=validationRange.Address
.IgnoreBlank = True
.InCellDropdown = True
.InputTitle = ""
.ErrorTitle = ""
.InputMessage = ""
.ErrorMessage = ""
.ShowInput = True
.ShowError = True
End With
Next i
End Sub

最佳答案

这不是一个简单的流控案例吗?我不明白需要特殊关键字来解决它:

For i = 0 To 10
If Not condition Then
some other code
Next

编辑:或者,在您的代码中:
Sub Macro1()
'
' Macro1 Macro
'

'
Worksheets("Drop-down").Select
n = Cells(1, 1).End(xlDown).Row
For i = 1 To n
ActiveSheet.Cells(i, 2).Select
*******************************************************
If Not Worksheets("Misc").Cells(2, i).Value = "" Then
*******************************************************
If Worksheets("Misc").Cells(3, i).Value <> "" Then
Set validationRange = Range(Worksheets("Misc").Cells(2, i), Worksheets("Misc").Cells(2, i).End(xlDown))
Else
Set validationRange = Worksheets("Misc").Cells(2, i)
End If
With Selection.Validation
.Delete
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
xlBetween, Formula1:=validationRange.Address
.IgnoreBlank = True
.InCellDropdown = True
.InputTitle = ""
.ErrorTitle = ""
.InputMessage = ""
.ErrorMessage = ""
.ShowInput = True
.ShowError = True
End With
********
End If
********
Next
End Sub

关于VBA:跳出for循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2808424/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com