gpt4 book ai didi

excel - 将文本附加到单元格但卡在循环中

转载 作者:行者123 更新时间:2023-12-04 22:24:45 24 4
gpt4 key购买 nike

我的问题是我的代码不能按照我认为的方式工作。我的 worksheet_change 宏中有 Loop,并且在该循环中,我希望 if 语句正确(MsgBox 按钮按下是),该单元格中写入的内容将在该文本的末尾附加值。

但是,如果我运行这个宏并按下是 - 单元格值最后的值,但 MsgBox 又来了,我陷入了那个循环......我是 VBA 编程和语法的新手。

有人可以帮助我并解释我的错误吗?

代码:

Private Sub Worksheet_Change(ByVal Target As Range)

Dim rLockable As Range
Dim cl As Range

Set rLockable = Range("C13:J1000")
Set cl = Range("C13:J1000")
Set cele = Range("K13:K1000")
vardas = ActiveWorkbook.Sheets("Login").Range("O8").Value

Select Case True
Case Not Intersect(rLockable, Target) Is Nothing
'If target is within the range then do nothing
If Intersect(rLockable, Target) Is Nothing Then Exit Sub
ActiveSheet.Unprotect Password:="1234"
For Each cl In Target
If cl.Value <> "" Then
check = MsgBox("Ar áraðyti áraðà? Koreguoti áraðo nebegalësite.", vbYesNo, "Áraðo iðsaugojimas")
If check = vbYes Then
Target.Worksheet.Unprotect Password:="1234"
cl.MergeArea.Locked = True
cl.Value = cl.Value & " " + vardas
Else
cl.Value = ""
ActiveSheet.Protect Password:="1234"
End If
End If
Exit For
Next cl

Case Not Intersect(Range("K13:K1000"), Target) Is Nothing
ActiveSheet.Unprotect Password:="1234"
For Each cele In Target
If cele.Value <> "" Then
cele.Offset(0, 2).MergeArea.Value = vardas
End If
Exit For
Next cele
End Select

ActiveSheet.Protect Password:="1234"

End Sub

最佳答案

在对工作表进行更改之前,您需要将 enableEvent 设置为 false,否则 worksheet_change 事件将再次启动。

application.enableEvents=false
'change the worksheet
application.enableEvents=true
'resets the worksheet_change event

关于excel - 将文本附加到单元格但卡在循环中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58953573/

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