Go Back  Xtreme Visual Basic Talk > Legacy Visual Basic (VB 4/5/6) > VBA / Office Integration > Excel > selecting a sheet


Reply
 
Thread Tools Display Modes
  #1  
Old 09-21-2003, 09:01 PM
Dangleberry Dangleberry is offline
Contributor
 
Join Date: Sep 2003
Location: Australia
Posts: 413
Default selecting a sheet


Hi guys,

Having a bit of trouble getting some code to work. I'm new to this caper, so any tips on neatening up my code
and the like would be greatly appreciated.

I'm using Visual Basic for excel in office 2000 and what I'm trying to do is to activate a sheet in an open workbook.
The sheetname is entered via an input box and if the sheet name is entered incorrectly I want an error message to
come up letting the user know and give them the option to re-enter the sheet name. Here is the code I have for it:

Code:
8 freightsheet = InputBox("Please Enter the name of the sheet that the freight information_ is located on in the file " & freightfile, "Sheet Name", "Sheetname") If freightsheet = "" Then End On Error GoTo 16 Sheets(freightsheet).Activate On Error GoTo 0 16 response = MsgBox("The sheet name you selected could not be found in the workbook "_ & freightname & ". Click 'Yes' to re-enter the sheet name or click 'No' to exit.",_ vbYesNo, "Incorrect Sheet Name, File Name Combination") If response = vbYes Then GoTo 8 Else End

It works for the first loop, but if it is entered incorrectly second time around an error message comes up:

"Run time error '9':

Subscript out of range."
Reply With Quote
  #2  
Old 09-21-2003, 10:30 PM
crabby's Avatar
crabby crabby is offline
mostly gone

Retired Moderator
* Expert *
 
Join Date: Aug 2002
Location: London / UK
Posts: 2,721
Default

i cant see that you clear freightsheet after the first loop. so it will still hold the old value, not certain but a
Code:
freightsheet = ""
before requereing could be usefull
__________________
there are always two sides of a story and 12 ways to sing a song or write some code. so whats wrong ?

Avatar by lebb
Reply With Quote
  #3  
Old 09-21-2003, 10:48 PM
Dangleberry Dangleberry is offline
Contributor
 
Join Date: Sep 2003
Location: Australia
Posts: 413
Default

Hmm... tried it, still didn't work

If on the second loop I put in a valid sheet name the code works. When the error came up i checked the
value of freightsheet, and it was the value I entered in the input box. I'm stumped anyway. I'm sure there's
a simple solution, I just can't see it! Thanks for the help though!
Reply With Quote
  #4  
Old 09-21-2003, 10:55 PM
mathiasb's Avatar
mathiasb mathiasb is offline
Centurion
 
Join Date: Jul 2003
Location: San Francisco, CA
Posts: 183
Default

Instead of using your code, I rewrote it quite a bit - mostly because I avoid using GoTo... Hope that helps,

Mathias

Code:
Sub activateSheet() Dim mySheet As Worksheet Dim sheetFound As Boolean Dim mySheetName As String mySheetName = InputBox("Enter the name of the sheet") If mySheetName = "" Then Exit Sub sheetFound = False For Each Worksheet In Worksheets If Worksheet.Name = mySheetName Then Set mySheet = Worksheet sheetFound = True End If Next If sheetFound = False Then activateSheet Else mySheet.Activate End If End Sub
Reply With Quote
  #5  
Old 09-21-2003, 10:57 PM
mathiasb's Avatar
mathiasb mathiasb is offline
Centurion
 
Join Date: Jul 2003
Location: San Francisco, CA
Posts: 183
Default

NB It's case sensitive, by the way
Reply With Quote
  #6  
Old 09-21-2003, 11:21 PM
Dangleberry Dangleberry is offline
Contributor
 
Join Date: Sep 2003
Location: Australia
Posts: 413
Cool

Its like you're talking half in english and half in Dutch!!!
I think if i have a bit of a play it should
start to make sense. Picked up a few handy tips, thanks. I'm guessing 'Exit Sub' stops the particular
sub, but not the subs that are running it. (hmmm.... think I'm talking in circles!)

If I want to hold onto the value of mysheet in other subs how do I do that?

Thanks Again

That works perfectly!!! It all makes sense too! I've learnt a fair bit from that, Ta.

Alex

Last edited by Dangleberry; 09-21-2003 at 11:53 PM.
Reply With Quote
  #7  
Old 09-22-2003, 12:00 AM
mathiasb's Avatar
mathiasb mathiasb is offline
Centurion
 
Join Date: Jul 2003
Location: San Francisco, CA
Posts: 183
Post

If your questions are still questions...

1. What you could do to hold the value is rewrite this into a function that just returns true if it is a valid sheet name, false else, without selecting/activating it, or sth along these lines.

2. Correct, exit sub, as the name suggest, exits the current sub; so it returns in the sub that called it.

Cheers,

Mathias

PS And I am largely French and partly German which does not exactly explain how I talk half English and half Dutch!
Reply With Quote
  #8  
Old 09-22-2003, 12:30 AM
Dangleberry Dangleberry is offline
Contributor
 
Join Date: Sep 2003
Location: Australia
Posts: 413
Default

All is good! you've helped me get it working like a well oiled machine! Thanks a million!

Ahh.. French and German is it, not surprising really, I never was any good at languages!
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
selecting from record sheet where value is an autonumber andrew_c_2000 Database and Reporting 4 09-17-2003 11:04 AM
Selecting call from specific sheet dynamically ryber Excel 2 07-14-2003 08:42 AM
Excel--maimum # of rows in a sheet bigeyedfish81 Excel 5 07-08-2003 09:20 AM
macro to look in another sheet HUBERT Excel 0 06-02-2003 05:51 PM
Automation Error...Problem of the DAY! jhermiz Excel 1 04-09-2003 03:29 AM

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->