gpt4 book ai didi

excel - 使用 InputBox 复制粘贴整个工作表

转载 作者:行者123 更新时间:2023-12-02 17:22:20 25 4
gpt4 key购买 nike

我有这段代码,我正在尝试执行一项简单的任务,显然这对我来说并不那么简单。我想说vba要复制哪个工作表(这里是InputBox的功能,我在其中插入工作表名称),然后,如果存在(即如果名称正确),则在sheet20中执行复制粘贴,如果不存在存在,转到 exitmysub。

现在,我有两个问题:

1) 它不复制粘贴。或者至少,并非总是如此。有时是,有时不是。我真的不明白为什么(我总是输入正确的工作表名称)

2) 即使名称正确,代码也会运行 msgbox(“无效的工作表名称”)。虽然我希望它仅在我输入的工作表名称不存在时才会触发。

感谢您的帮助!

Option Explicit

Dim text As String, ws As Worksheet


Sub copyentiresheet()

text = InputBox("Write here the Local Deposit Sheet you want to update", "Update Local Deposit Monitoring")

On Error GoTo exitmysub
Sheet20.Cells.Clear
Set ws = Sheets(text)
ws.Cells.Copy

Sheets20.Paste


exitmysub:
MsgBox ("Invalid Sheet Name")

End Sub

最佳答案

试试这个...

Option Explicit

Dim text As String, ws As Worksheet

Sub copyentiresheet()

text = InputBox("Write here the Local Deposit Sheet you want to update", "Update Local Deposit Monitoring")

On Error GoTo ErrorMySub
Sheet20.Cells.Clear
Set ws = Sheets(text)
ws.Cells.Copy Sheet20.Range("A1")

ExitMySub:
Exit Sub

ErrorMySub:
MsgBox ("Invalid Sheet Name")

End Sub

关于excel - 使用 InputBox 复制粘贴整个工作表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53619625/

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