gpt4 book ai didi

vba - Excel VBA取消输入框不返回false/退出子

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

我的 Excel 工作表上有一个命令按钮,用于打开一个预加载当前选定范围的 application.inputbox,并将该范围内的单元格内容附加到这些单元格的注释中。

我正在使用 if/else 语句来检查取消按钮是否被单击,但它没有退出子程序;无论我单击“确定”还是“取消”,代码都会运行。我认为要么取消按钮没有返回“false”,要么我的 if 语句被破坏。

这是代码:

Private Sub CommentLogButton_Click()
'This Sub Appends Cell Contents to Cell Comment
Dim rng As Range
Dim WorkRng As Range
On Error Resume Next
xTitleId = "Range to Log"
Set WorkRng = Application.Selection
Set WorkRng = Application.InputBox("Range", xTitleId, WorkRng.Address, Type:=8)
If WorkRng = False Then
Exit Sub
Else
For Each rng In WorkRng
rng.NoteText Text:=rng.NoteText & rng.Value & ", "
rng.Value = ""
Next
End If
End Sub

最佳答案

WorkRng 已声明为范围。

改变

If WorkRng = False Then

If WorkRng is nothing Then

将代码更改为

Private Sub CommentLogButton_Click()
'This Sub Appends Cell Contents to Cell Comment
Dim rng As Range, WorkRng As Range
Dim rngAddress As String

xTitleId = "Range to Log"

'~~> Check if what the user selected is a valid range
If TypeName(Selection) <> "Range" Then
MsgBox "Select a range first."
Exit Sub
End If

rngAddress = Application.Selection.Address

On Error Resume Next
Set WorkRng = Application.InputBox("Range", xTitleId, rngAddress, Type:=8)
On Error GoTo 0

If WorkRng Is Nothing Then
Exit Sub
Else
For Each rng In WorkRng
rng.NoteText Text:=rng.NoteText & rng.Value & ", "
rng.Value = ""
Next
End If
End Sub

关于vba - Excel VBA取消输入框不返回false/退出子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31794189/

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