gpt4 book ai didi

excel - 需要将用户选择的工作表名称分配给变量

转载 作者:行者123 更新时间:2023-12-04 20:47:17 31 4
gpt4 key购买 nike

我想提示用户在工作表中选择一个单元格来填充一个值。我正在尝试使用 Application.Input ,但它不会获取在其中选择单元格的工作表的名称......它继续使用运行宏时处于事件状态的工作表名称。

我到处找。有没有办法将工作表名称以及用户在提示时选择的单元格范围分配给变量?

varCellContent = Application.InputBox(prompt:="Choose a sheet by clicking on any cell in it.", Type:=8)
strDestinationSheetName = ActiveSheet.Name
MsgBox ("Sheet = " & strDestinationSheetName)

最佳答案

这是你正在尝试的吗?

Option Explicit

Sub Sample()
Dim varCellContent As Range
Dim strDestinationSheetName As String

On Error Resume Next
Set varCellContent = Application.InputBox(Prompt:="Choose a sheet by clicking on any cell in it.", Type:=8)
On Error GoTo 0

If Not varCellContent Is Nothing Then
strDestinationSheetName = varCellContent.Parent.Name

MsgBox ("Sheet = " & strDestinationSheetName)
End If
End Sub

关于excel - 需要将用户选择的工作表名称分配给变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10438935/

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