I've created an InputBox which asks the user for the letter of the last column containing entries. I'd like the title to be sheet name, according to an index. Here's the code I've written:

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)`

When I run it, I get error 1004 "Method 'InputBox' of object '_Application' failed". I suspect it has something to do with how I set ShtName, as the code runs properly if I replace the title argument with something like "hooray coding!"

Can anyone explain to me what's going wrong and how I can fix it? Thanks!

有帮助吗?

解决方案

You are passing the whole WorkSheet object, instead of just its name. Try this:

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)

其他提示

Sheets is a Collection so to get the name you need to use

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

Also pretty sure this will always be a string so you could use Dim ShtName As String and avoid Set.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top