Photo Gallery Member List Search Calendars FAQ Ticket List Log Out


VBScript & Excel

 
Logged in as: Guest
arrSession:exec spGetSession 2,2,32435
 Active Users: There are 0 members and 0 guests.
 Users viewing this topic: none
 

 

 
  
  Printable Version
All Forums >> [Scripting] >> WSH & Client Side VBScript >> VBScript & Excel
  Do you like VisualBasicScript.com? Link to us and help spread the word about our forum. Thanks!
Page: [1]
Login
Message << Older Topic   Newer Topic >>
 VBScript & Excel - 3/20/2006 8:31:59 AM   
  hamboy

 

Posts: 94
Score: 6
Joined: 7/11/2005
Status: offline
Hi I'm trying to play around with excel & vbscript.

I have an excel file (test.xls), with worksheet (test1).
How would I grab the data in cells B300-B350 from test1 worksheet and put it into an array?

Thanks
 
 
Post #: 1
 
 RE: VBScript & Excel - 3/20/2006 3:43:33 PM   
  Cybex


Posts: 412
Score: 0
Joined: 9/14/2005
From: Florida
Status: offline
You could do something like this...

      

Or use a dictionary object.  I don't have time to do both I gotta get some sleep.


Cybex

_____________________________

Common sense is not so common.

(in reply to hamboy)
 
 
Post #: 2
 
 RE: VBScript & Excel - 3/21/2006 4:37:54 AM   
  hamboy

 

Posts: 94
Score: 6
Joined: 7/11/2005
Status: offline
thanks...

you know how you declare an object Set objWB = objXL.WorkBooks.Open(...)
what if in the script, I want to use 2 different excel files... or two worksheets within an excel file?

say for example i want:
cell B2 from test1.xls>sheet1
cell C5 from test1.xls>sheet2
cell A2 from test2.xls>sheet1

how do i declare and activate whatever sheet I want?

(in reply to Cybex)
 
 
Post #: 3
 
 RE: VBScript & Excel - 3/21/2006 5:35:08 AM   
  ziminski

 

Posts: 79
Score: 2
Joined: 1/8/2006
Status: offline
set the worksheet ...
Set ws1 = objXL.Worksheets(1)
Set ws2 = objXL.Worksheets(2)

(in reply to hamboy)
 
 
Post #: 4
 
 RE: VBScript & Excel - 3/21/2006 6:10:32 AM   
  hamboy

 

Posts: 94
Score: 6
Joined: 7/11/2005
Status: offline
thanks... I know that
but how would you reference it?

say if I did:
Set objXL = CreateObjecct("Excel.Application")
Set objWB1=objXL.WorkBooks.Open(c:\script\test1.xls)
Set objWB2=objXL.WorkBooks.Open(c:\script\test2.xls)

ie. would you do something like this to get the values?
'cell B2 from test1.xls>sheet1
'cell C5 from test1.xls>sheet2
'cell A2 from test2.xls>sheet1

Set objWS1=objXL.objWB1.Worksheets(1)
Set objWS2=objXL.objWB1.Worksheets(2)
Set objWS3=objXL.objWB2.Worksheets(1)

CellValue1=objWS1.Cells( , ).Value
etc...


(in reply to ziminski)
 
 
Post #: 5
 
 RE: VBScript & Excel - 3/23/2006 6:54:00 AM   
  hamboy

 

Posts: 94
Score: 6
Joined: 7/11/2005
Status: offline
hi all, i've been searching through google and see if i can find an example with vbscript using more than 1 excel file.
unfortuantely no luck...

does this mean vbscirpt can't work with more than 1 instance of excel files?

(in reply to hamboy)
 
 
Post #: 6
 
 RE: VBScript & Excel - 3/23/2006 8:03:49 AM   
  ebgreen


Posts: 4613
Score: 31
Joined: 7/12/2005
Status: offline
VBScript can work with multiple files. You have a couple of options on how you want to do this. The first method is if you know how many files you will need to work with, just Dim and instantiate as many Excel objects as you need. If you don't know (for instance you need to do something to every excel file in a specific folder) then you can use a for next loop and just set one excel object to each file in turn. If you have some need to have an excel object for every excel file in a folder, then do the same thing, but add each file to a dictionary or array as you loop through them.

_____________________________

"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to hamboy)
 
 
Post #: 7
 
 RE: VBScript & Excel - 3/23/2006 9:37:35 AM   
  hamboy

 

Posts: 94
Score: 6
Joined: 7/11/2005
Status: offline
thanks ebgreen...

i'm working with 2 excel files, basically what i'm trying to do is get data from a range of cells in one ecxel file (from 2 worksheets) do calculations
with them in the script and display it in another existing excel file.

however, i'm not how to properly refer to specific excel file/worksheet in vbscript.
do i just dim a variable for each workbook and variables for each worksheet? and how would i tell vbscript to get data from this excel sheet and in the 3rd worksheet for example?

(in reply to ebgreen)
 
 
Post #: 8
 
 RE: VBScript & Excel - 3/23/2006 11:28:46 AM   
  ebgreen


Posts: 4613
Score: 31
Joined: 7/12/2005
Status: offline
Actually I think I've addressed this issue here before. Try searching the forum for something like "multiple excel".

_____________________________

"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to hamboy)
 
 
Post #: 9
 
 RE: VBScript & Excel - 3/24/2006 3:17:09 AM   
  hamboy

 

Posts: 94
Score: 6
Joined: 7/11/2005
Status: offline
thanks... i think i'm able to start scripting now that i have that cleared up


I have an error that says
Line 30 Char 2
Error: Subscript out of range

i guess it's sometihig to do with preserving array? how can i fix this?

< Message edited by hamboy -- 3/27/2006 1:54:29 AM >

(in reply to ebgreen)
 
 
Post #: 10
 
 RE: VBScript & Excel - 3/24/2006 3:28:43 AM   
  ebgreen


Posts: 4613
Score: 31
Joined: 7/12/2005
Status: offline
Lets walk through your code together. I won't use code tags so that I can make comments using colored text:

'This script collects data from Excel1(employee names and work hrs) -> WS1,WS2, sums the data
'and outputs the result to Excel2 within appropriate worksheets (each employee has a worksheet)
'The two excel files for data collection
strPathExcel1 = "C:\Work\script\excel1.xls"
strPathExcel2 = "C:\Work\script\collection\excel2.xls"
'Create excel instance, run in background
Set oXL = CreateObject("Excel.Application")
oXL.Visible = False
oXL.DisplayAlerts = 0
'Open WB1->worksheet 1 as read only
Set objWB1 = oXL.Workbooks.Open(strPathExcel1,False,True)
Set Worksheet = oXL.Sheets(1)
'WB1, WS1 currently activated
'collect all 19 employee names and monthly work hrs
Dim empArray()  Here you create an array with no elements
'employee names listed in column B from row 363 to 381
'|Name-|---------Monthly work hrs --------|
'Name1 Jan Feb Mar Apr May Jun Jul Aug Sep
'XXX X X X X X X X X X <--row 363
'...
'XXX X X X X X X X X X <--row 381
For i=363 To 381
   'store employee name in 0 element
   empArray(0) = oXL.Cells(i,2).Value Here you try to set the first element of the array to some value (remember that you explicitly told the interpreter that the array had no elements at all)
   WScript.Echo empArray(0) Here you try to retrieve the value of the first element in an array that has no elements
   'collect work hrs for employee i
   Acount = 1
   For j = 17 To 25 'Jan - Sep
        empArray(Acount) = oXL.Cells(i,j).value Here you try to set the value of the second element in the array. The array still has no elements at all
        ReDim Preserve empArray(Acount) Here you finally try to tell the interpreter that the array should have 2 elements
        WScript.Echo empArray(Acount) Here you try to retrieve the value of the second element in the array
        Acount = Acount +1
   Next
Next

Note that these comments are only applicable for the first pass through the loop but that is really a moot point since code execution will never get to the loop in the first place because you try toi assign a value to the first element in an array that you explicitly declared as having no elements. You need to rethink the logic in the way you handle the array.

_____________________________

"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to hamboy)
 
 
Post #: 11
 
 RE: VBScript & Excel - 3/27/2006 3:05:27 AM   
  hamboy

 

Posts: 94
Score: 6
Joined: 7/11/2005
Status: offline
thanks all...

i have one last question... how do i search for a text "Alan Kurstra" in excel for all the used range in column B. This text only appers once.
And from this, what command do I use to get the row number?


I have

      

but i get unknown runtime error

< Message edited by hamboy -- 3/27/2006 3:56:55 AM >

(in reply to ebgreen)
 
 
Post #: 12
 
 RE: VBScript & Excel - 4/7/2006 12:19:22 AM   
  hamboy

 

Posts: 94
Score: 6
Joined: 7/11/2005
Status: offline
in excel... how do you find the color of an orange cell...

i'm trying to loop through a specified number of rows until i reach a cell that is shaded orange..
how woudl i do that?

< Message edited by hamboy -- 4/7/2006 12:29:09 AM >

(in reply to hamboy)
 
 
Post #: 13
 
 RE: VBScript & Excel - 4/7/2006 12:50:13 AM   
  ebgreen


Posts: 4613
Score: 31
Joined: 7/12/2005
Status: offline
Ok, on your search question, what is the verbatim text of the error?

For the shading question, you will need to look at each cell's .ColorIndex property. You may also need to look at its .Pattern and/or .PatternColorIndex property(ies). Note that I had no idea how to do this before you asked the question. Here is what I did to find out:

1) Open a new Excel spreadsheet.
2) Click Tools->Macros->record (pick any name for the macro)
3) Click on any cell
4) Click Format->Cells->Patterns and select the cell shading that you want to look for.
5) Stop the macro recording
6)Click Tools->Macro->Macros and select the macro that you just recorded.
7) Click the Edit button
8) Voila...there is sample code on what you are interested in.


The macro recorder should always be one of your first three stops when you have office automation questions (the other two are the docs and google).

_____________________________

"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to hamboy)
 
 
Post #: 14
 
 RE: VBScript & Excel - 4/7/2006 1:01:36 AM   
  hamboy

 

Posts: 94
Score: 6
Joined: 7/11/2005
Status: offline
awesome thanks for the tips!


(in reply to ebgreen)
 
 
Post #: 15
 
 
 
  

If you found our site useful please link to us <a href="http://www.visualbasicscript.com">VisualBasicScript.com</a>.
All Forums >> [Scripting] >> WSH & Client Side VBScript >> VBScript & Excel Page: [1]
Jump to:





New Messages No New Messages
Hot Topic w/ New Messages Hot Topic w/o New Messages
Locked w/ New Messages Locked w/o New Messages
 Post New Thread
 Reply to Message
 Post New Poll
 Submit Vote
 Delete My Own Post
 Delete My Own Thread
 Rate Posts