在InputBox中使用变量作为title参数

时间:2013-10-03 20:13:47

标签: excel vba variables inputbox

我创建了一个InputBox,它要求用户输入包含条目的最后一列的字母。根据索引,我希望标题是工作表名称。这是我写的代码:

Dim LastEntryCol As String  
Dim ShtName As Variant  
Set ShtName = ThisWorkbook.Sheets(2) '****The number 2 will need to be changed to an index.  For now it's hard-entered.  
LastEntryCol = Application.InputBox("What is the letter of the last column containing entry labels?", ShtName)`

当我运行它时,我收到错误1004“对象'_Application'的方法'InputBox'失败”。我怀疑它与我如何设置ShtName有关,因为如果我用“hooray coding!”之类的东西替换title参数,代码就会正常运行。

任何人都可以向我解释出现了什么问题以及如何解决这个问题?谢谢!

2 个答案:

答案 0 :(得分:1)

您正在传递整个WorkSheet object,而不仅仅是其名称。试试这个:

Dim LastEntryCol As String
Dim ShtName As String
ShtName = ThisWorkbook.Sheets(2).Name '****The number 2 will need to be changed to an index.  For now it's hard-entered.
LastEntryCol = Application.InputBox("What is the letter of the last column containing entry labels?", ShtName)

答案 1 :(得分:0)

表格是一个集合,以便获得您需要使用的名称

ShtName = ThisWorkbook.Sheets.Item(2).Name

也很确定这将始终是一个字符串,因此您可以使用Dim ShtName As String并避免设置。