ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   InputBox code (https://www.excelbanter.com/excel-programming/306806-re-inputbox-code.html)

baehrjf

InputBox code
 
Hi Frank. Thanks.
By valid, I mean that the user enters in the name of a worksheet that exists
in the active workbook (the workbook has a number of sheets named
chronologically by month and year). What I'm concerned with is how to handle
an error if/when the user doesn't enter anything and there's an empty string.
This code is working for me. Now how should I code the sub to end the entire
macro when the user clicks the Cancel button?
John

"Frank Kabel" wrote:

Hi
how do you define a 'valid' string?

For your second question you could use something like

Dim wksname
Dim wks as worksheet
wksname=inputbox ("Enter a name")

on error resume next
set wks = activeworkbook.worksheets(wksname)
on error goto 0
if wks is nothing then
msgbox "not a valid sheetname"
else
msgbox "You have selected sheet: " & wks.name
end if


--
Regards
Frank Kabel
Frankfurt, Germany


baehrjf wrote:
New to programming Excel VB, so bear with me....
I want to have a InputBox which will activate a worksheet in a
workbook that was just opened with Workbooks.Open. I am able to
build the InputBox with Application.InputBox and activate the sheet
with Sheets.Activate, but what I need help with is to write the
syntax to 1. requires the user to either enter a valid string or end
the macro with a cancel commandbutton; and 2. handle the error if the
user enters an invalid worksheet name (invalid because it's not in
the workbook, nothing more complex than that). Thanks in advance for
any help.

John





All times are GMT +1. The time now is 05:21 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com