Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,058
Default Excel Worksheet Select (Activate ?) in VBS

Usually The argument to WORKSHEETS() is either a number, or a string or
string variable that represents the sheet in question.

Excel VBA may not know that the function will return a string.Perhaps using:

Function funWorksheetInput() as string
..
..
..
..
Dim s as string
s=funWorksheetInput()
objWorkbooks.Worksheets(s).Activate
--
Gary's Student
gsnu200702


"tbader01" wrote:

Hello Everyone,

I have started the following code that will allow a user to drag and drop an
XLS file ontop of this vbs script, and it will display a list of all the
available worksheet names, which they can then choose the worksheet number
and it will select that worksheet (take you to that worksheet).
This is part of a bigger idea I have in my head, but so far I am stuck with
the following line:

objWorkbooks.Worksheets(funWorksheetInput).Select

Why can't I use my function as the input selection? I get "Script Out of
Range"

I can do the following hard coding:
objWorkbooks.Worksheets(2).Select
or
objWorkbooks.Worksheets("Sheet3").Select

Also, what would that line look like if I used (or can I even use that?):
objWorksheets(?)

Any help would be appreciated!

T

'vbs code start

Dim argsNamed, argsUnnamed
set argsNamed = WScript.Arguments.Named
set argsUnnamed = WScript.Arguments.Unnamed

Dim objExcelApp ' Excel application object
Set objExcelApp = WScript.CreateObject("Excel.Application")

objExcelApp.Visible = True

Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")

set objWorkbooks = objExcelApp.Workbooks.Open(argsUnnamed(file))
set objWorksheets = objWorkbooks.Worksheets

objWorkbooks.Worksheets(funWorksheetInput).Select

set objWorkbooks = Nothing
set objWorksheets = Nothing
set objRange = Nothing

Set objExcelApp = Nothing
Set argsNamed = Nothing
Set argsUnnamed = Nothing
Set fso = Nothing

WScript.Quit

Function funWorksheetInput
Dim arryWS, c, ws
arryWS = split( funGetWorksheetNames, ",")
For c = 0 to Ubound(arryWS) - 1
ws = ws & c+1 & "). " & arryWS(c)
ws = ws & vbCRLF
Next
strSourceMessage = "Select from one of the following worksheets:" & vbCrLf &
ws

funWorksheetInput = InputBox( strSourceMessage, "WorkSheet Selection", "1")
If funWorksheetInput = "" Then WScript.Quit(0)
End Function


Function funGetWorksheetNames
Dim s,ws
For s = 1 To objWorksheets.Count
ws = ws & objWorksheets(s).Name
ws = ws & ","
Next
funGetWorksheetNames = ws
End Function
'vbs code end

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7,247
Default Excel Worksheet Select (Activate ?) in VBS

It shouldn't matter that the function call is within the Worksheets()
property. For example, the following code works just fine.

Sub AAA()
Worksheets(GetSheetName()).Select
End Sub

Function GetSheetName() As String
GetSheetName = "Sheet3"
End Function

As long as GetSheetName returns the name of an existing worksheet, the
correct worksheet will be selected.

--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com
(email on the web site)



"Gary''s Student" wrote in message
...
Usually The argument to WORKSHEETS() is either a number, or a string or
string variable that represents the sheet in question.

Excel VBA may not know that the function will return a string.Perhaps
using:

Function funWorksheetInput() as string
.
.
.
.
Dim s as string
s=funWorksheetInput()
objWorkbooks.Worksheets(s).Activate
--
Gary's Student
gsnu200702


"tbader01" wrote:

Hello Everyone,

I have started the following code that will allow a user to drag and drop
an
XLS file ontop of this vbs script, and it will display a list of all the
available worksheet names, which they can then choose the worksheet
number
and it will select that worksheet (take you to that worksheet).
This is part of a bigger idea I have in my head, but so far I am stuck
with
the following line:

objWorkbooks.Worksheets(funWorksheetInput).Select

Why can't I use my function as the input selection? I get "Script Out of
Range"

I can do the following hard coding:
objWorkbooks.Worksheets(2).Select
or
objWorkbooks.Worksheets("Sheet3").Select

Also, what would that line look like if I used (or can I even use that?):
objWorksheets(?)

Any help would be appreciated!

T

'vbs code start

Dim argsNamed, argsUnnamed
set argsNamed = WScript.Arguments.Named
set argsUnnamed = WScript.Arguments.Unnamed

Dim objExcelApp ' Excel application object
Set objExcelApp = WScript.CreateObject("Excel.Application")

objExcelApp.Visible = True

Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")

set objWorkbooks = objExcelApp.Workbooks.Open(argsUnnamed(file))
set objWorksheets = objWorkbooks.Worksheets

objWorkbooks.Worksheets(funWorksheetInput).Select

set objWorkbooks = Nothing
set objWorksheets = Nothing
set objRange = Nothing

Set objExcelApp = Nothing
Set argsNamed = Nothing
Set argsUnnamed = Nothing
Set fso = Nothing

WScript.Quit

Function funWorksheetInput
Dim arryWS, c, ws
arryWS = split( funGetWorksheetNames, ",")
For c = 0 to Ubound(arryWS) - 1
ws = ws & c+1 & "). " & arryWS(c)
ws = ws & vbCRLF
Next
strSourceMessage = "Select from one of the following worksheets:" &
vbCrLf &
ws

funWorksheetInput = InputBox( strSourceMessage, "WorkSheet Selection",
"1")
If funWorksheetInput = "" Then WScript.Quit(0)
End Function


Function funGetWorksheetNames
Dim s,ws
For s = 1 To objWorksheets.Count
ws = ws & objWorksheets(s).Name
ws = ws & ","
Next
funGetWorksheetNames = ws
End Function
'vbs code end



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Excel Worksheet Select (Activate ?) in VBS

Hello,
Thanks for your input, but I don't think VBS likes or accepts the "As
String" statement, since it gives me an error there. I think that might be an
exclusive thing to VBA?
Were you able to copy and paste my code to see what I am seeing?
It just seems like it should work like I have it!
I guess I can use vartype to see just exactly what the function is
returning...
If you have any insights, please let me know!
Thanks!

"Gary''s Student" wrote:

Usually The argument to WORKSHEETS() is either a number, or a string or
string variable that represents the sheet in question.

Excel VBA may not know that the function will return a string.Perhaps using:

Function funWorksheetInput() as string
.
.
.
.
Dim s as string
s=funWorksheetInput()
objWorkbooks.Worksheets(s).Activate
--
Gary's Student
gsnu200702


"tbader01" wrote:

Hello Everyone,

I have started the following code that will allow a user to drag and drop an
XLS file ontop of this vbs script, and it will display a list of all the
available worksheet names, which they can then choose the worksheet number
and it will select that worksheet (take you to that worksheet).
This is part of a bigger idea I have in my head, but so far I am stuck with
the following line:

objWorkbooks.Worksheets(funWorksheetInput).Select

Why can't I use my function as the input selection? I get "Script Out of
Range"

I can do the following hard coding:
objWorkbooks.Worksheets(2).Select
or
objWorkbooks.Worksheets("Sheet3").Select

Also, what would that line look like if I used (or can I even use that?):
objWorksheets(?)

Any help would be appreciated!

T

'vbs code start

Dim argsNamed, argsUnnamed
set argsNamed = WScript.Arguments.Named
set argsUnnamed = WScript.Arguments.Unnamed

Dim objExcelApp ' Excel application object
Set objExcelApp = WScript.CreateObject("Excel.Application")

objExcelApp.Visible = True

Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")

set objWorkbooks = objExcelApp.Workbooks.Open(argsUnnamed(file))
set objWorksheets = objWorkbooks.Worksheets

objWorkbooks.Worksheets(funWorksheetInput).Select

set objWorkbooks = Nothing
set objWorksheets = Nothing
set objRange = Nothing

Set objExcelApp = Nothing
Set argsNamed = Nothing
Set argsUnnamed = Nothing
Set fso = Nothing

WScript.Quit

Function funWorksheetInput
Dim arryWS, c, ws
arryWS = split( funGetWorksheetNames, ",")
For c = 0 to Ubound(arryWS) - 1
ws = ws & c+1 & "). " & arryWS(c)
ws = ws & vbCRLF
Next
strSourceMessage = "Select from one of the following worksheets:" & vbCrLf &
ws

funWorksheetInput = InputBox( strSourceMessage, "WorkSheet Selection", "1")
If funWorksheetInput = "" Then WScript.Quit(0)
End Function


Function funGetWorksheetNames
Dim s,ws
For s = 1 To objWorksheets.Count
ws = ws & objWorksheets(s).Name
ws = ws & ","
Next
funGetWorksheetNames = ws
End Function
'vbs code end

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4,391
Default Excel Worksheet Select (Activate ?) in VBS

I understood VBS does not have datatypes, everything is a variant (Or Object
is also available ?).

Anyway, you problem is caused by the Inputbox function returning a string;
check the Help.
So you are trying to set the Worksheets("1"), not Worksheets(1).

One solution is:
funWorksheetInput = CLng(InputBox( strSourceMessage, "WorkSheet Selection",
"1"))
or better to use objExcelApp.Inputbox, so you can limit the values to
Numbers with "Type:=1".

NickHK

"tbader01" wrote in message
...
Hello,
Thanks for your input, but I don't think VBS likes or accepts the "As
String" statement, since it gives me an error there. I think that might be

an
exclusive thing to VBA?
Were you able to copy and paste my code to see what I am seeing?
It just seems like it should work like I have it!
I guess I can use vartype to see just exactly what the function is
returning...
If you have any insights, please let me know!
Thanks!

"Gary''s Student" wrote:

Usually The argument to WORKSHEETS() is either a number, or a string or
string variable that represents the sheet in question.

Excel VBA may not know that the function will return a string.Perhaps

using:

Function funWorksheetInput() as string
.
.
.
.
Dim s as string
s=funWorksheetInput()
objWorkbooks.Worksheets(s).Activate
--
Gary's Student
gsnu200702


"tbader01" wrote:

Hello Everyone,

I have started the following code that will allow a user to drag and

drop an
XLS file ontop of this vbs script, and it will display a list of all

the
available worksheet names, which they can then choose the worksheet

number
and it will select that worksheet (take you to that worksheet).
This is part of a bigger idea I have in my head, but so far I am stuck

with
the following line:

objWorkbooks.Worksheets(funWorksheetInput).Select

Why can't I use my function as the input selection? I get "Script Out

of
Range"

I can do the following hard coding:
objWorkbooks.Worksheets(2).Select
or
objWorkbooks.Worksheets("Sheet3").Select

Also, what would that line look like if I used (or can I even use

that?):
objWorksheets(?)

Any help would be appreciated!

T

'vbs code start

Dim argsNamed, argsUnnamed
set argsNamed = WScript.Arguments.Named
set argsUnnamed = WScript.Arguments.Unnamed

Dim objExcelApp ' Excel application object
Set objExcelApp = WScript.CreateObject("Excel.Application")

objExcelApp.Visible = True

Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")

set objWorkbooks = objExcelApp.Workbooks.Open(argsUnnamed(file))
set objWorksheets = objWorkbooks.Worksheets

objWorkbooks.Worksheets(funWorksheetInput).Select

set objWorkbooks = Nothing
set objWorksheets = Nothing
set objRange = Nothing

Set objExcelApp = Nothing
Set argsNamed = Nothing
Set argsUnnamed = Nothing
Set fso = Nothing

WScript.Quit

Function funWorksheetInput
Dim arryWS, c, ws
arryWS = split( funGetWorksheetNames, ",")
For c = 0 to Ubound(arryWS) - 1
ws = ws & c+1 & "). " & arryWS(c)
ws = ws & vbCRLF
Next
strSourceMessage = "Select from one of the following worksheets:" &

vbCrLf &
ws

funWorksheetInput = InputBox( strSourceMessage, "WorkSheet Selection",

"1")
If funWorksheetInput = "" Then WScript.Quit(0)
End Function


Function funGetWorksheetNames
Dim s,ws
For s = 1 To objWorksheets.Count
ws = ws & objWorksheets(s).Name
ws = ws & ","
Next
funGetWorksheetNames = ws
End Function
'vbs code end



Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
.Activate vs. .Select in VBA Dave F Excel Discussion (Misc queries) 1 January 24th 07 03:41 PM
Select OR Activate ? Which when and Why? Buffyslay Excel Programming 5 January 16th 07 03:42 PM
IF statement to activate worksheet & select Range MentalDrow Excel Programming 1 June 19th 06 08:20 PM
what's the difference between Activate and Select for Worksheet? lvcha.gouqizi Excel Programming 1 October 28th 05 05:53 PM
Smart use of .Activate; .Select; .Copy with Sheets(1) va Worksheet Dennis Excel Discussion (Misc queries) 5 July 24th 05 01:05 AM


All times are GMT +1. The time now is 02:19 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"