Photo Gallery Member List Search Calendars FAQ Ticket List Log Out


excel to array, perform functions to convert units of measure then save xls

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

 

 
  
  Printable Version
All Forums >> [Scripting] >> WSH & Client Side VBScript >> excel to array, perform functions to convert units of measure then save xls
  Do you like VisualBasicScript.com? Link to us and help spread the word about our forum. Thanks!
Page: [1] 2 3 4   next >   >>
Login
Message << Older Topic   Newer Topic >>
 excel to array, perform functions to convert units of m... - 6/24/2008 10:42:33 PM   
  chriswebb18

 

Posts: 71
Score: 0
Joined: 4/7/2008
Status: offline
This is a massive pain for me.  I was told yesterday morning that I had until this morning to get this done, obviously not gonna happen.  We have data that we're moving from one DB to a new one in a different format.  I don't know all the details, but I have the information in an xls file from the old DB.  I am taking it row by row and putting into an array, then converting the rows that contain the measurement info from ton to lb or gallons to litres, there are about 20 different conversions that will be in here... I think I have put 2 in, simply because I didn't want to write it all and have to go back through it all if I screwed it up.  I am getting a "Type Mismatch" error on line 56 char 2(this .line)
   If arrExcelData(9) = "EA" And arrExcelData(14) = "E" Then
not sure what would make an If get a type mismatch error, but heres the whole code and a section of my in file.  I am fairly new to VBS, but lately have been working on several different projects, this is my first working with excel and i am very sloppy/inexperienced with arrays.  If you know an easier way or at least can help me get rid of this err, i would really appreciate it.  Thank you.


      


OK, so i cannot upload an excel, so I am not sure how to ge my infile to you.  Tell me what you need to help.  Thanks again
 
 
Post #: 1
 
 RE: excel to array, perform functions to convert units ... - 6/24/2008 11:00:45 PM   
  ehvbs

 

Posts: 2201
Score: 50
Joined: 6/22/2005
From: Germany
Status: offline
Hi chriswebb18,

if you send the .xls to me, I will give it try (no promises of immediate success, though).

Regards

ehvbs

(in reply to chriswebb18)
 
 
Post #: 2
 
 RE: excel to array, perform functions to convert units ... - 6/25/2008 12:16:12 AM   
  ehvbs

 

Posts: 2201
Score: 50
Joined: 6/22/2005
From: Germany
Status: offline
Hi chriswebb18,

you may check out whether something like this:


      

(forgot the) output:


      

could work for you. (Ask for details, if necessary!)

Good luck!

ehvbs

< Message edited by ehvbs -- 6/25/2008 12:29:05 AM >

(in reply to ehvbs)
 
 
Post #: 3
 
 RE: excel to array, perform functions to convert units ... - 6/25/2008 2:19:11 AM   
  chriswebb18

 

Posts: 71
Score: 0
Joined: 4/7/2008
Status: offline
i dont know that i completely understand that code... or partially, really.  i am not using sql in any part of this either, so i dont know if that works with those db connectors and the insert from commands.  it is excel to excel.  originally, it was from sql, but i dont have access to the original sql db, they exported what they needed done to xls and gave it to me.  

(in reply to ehvbs)
 
 
Post #: 4
 
 RE: excel to array, perform functions to convert units ... - 6/25/2008 5:09:14 AM   
  ehvbs

 

Posts: 2201
Score: 50
Joined: 6/22/2005
From: Germany
Status: offline
Hi chriswebb18,

I just got your email with the .xls. I'll look into it right now.

The sample code should demonstrate how to do to conversions using SQL. The main
idea is to use an Update statement to do the conversions.

Regards

ehvbs

(in reply to chriswebb18)
 
 
Post #: 5
 
 RE: excel to array, perform functions to convert units ... - 6/25/2008 5:43:59 AM   
  ehvbs

 

Posts: 2201
Score: 50
Joined: 6/22/2005
From: Germany
Status: offline
  (1)  in.xls depends on Pro-Pac import C-R.xls. can you create a stand-alone version of in.xls?

(2) your code fails on row 202 - can you check the values?

(in reply to ehvbs)
 
 
Post #: 6
 
 RE: excel to array, perform functions to convert units ... - 6/25/2008 5:58:06 AM   
  chriswebb18

 

Posts: 71
Score: 0
Joined: 4/7/2008
Status: offline
ive got a stand alone version ill send you,  also, line 202-205 have "#N/A" in the cells that are being tested with the if conditionals.  I believe this is the problem, but im not sure why it would be a problem.  the array values should be strings which would accept all of those characters, and i shouldn't need an if to deal with them since the else(or elseif) is optional and they would be treated as a do nothing.  that said... i am trying adding an else at the end just because and try using CString(arrExcelData(9)) and CString(arrExcelData(14)).  ill let you knwo how that works.

no good...  ill keep working on it

It has to be the#n/a, i tried a conditional based off of that string in the array and it got type mismatch error,  then tried adding this to the beginning, before it splits to an array and it give a type mismatch error as well:


      

I think im just going to have to add a piece to the beginning of the script and makes those fields = something else, but how would i detect those if i get error when it reads them?

< Message edited by chriswebb18 -- 6/25/2008 6:20:45 AM >

(in reply to ehvbs)
 
 
Post #: 7
 
 RE: excel to array, perform functions to convert units ... - 6/25/2008 6:17:32 AM   
  ehvbs

 

Posts: 2201
Score: 50
Joined: 6/22/2005
From: Germany
Status: offline
I think we should simplify the code to avoid the mapping of field names to array indices (error prone)

Have a look at this:


      

we can add more interesting functionality step by step.

#NV results in null values; so your if..then ..  will fail on the first comporison. You'll have to decide, how
we want to trear these records:

....
If IsNull( oRS.Fields( "Lawson UOM" ).Value ) Then
  ....
Else
Select Case oRS.Fields( "Lawson UOM" ).Value
    Case "EA"
      ...
End Select
End If

< Message edited by ehvbs -- 6/25/2008 6:34:04 AM >

(in reply to chriswebb18)
 
 
Post #: 8
 
 RE: excel to array, perform functions to convert units ... - 6/25/2008 6:25:49 AM   
  chriswebb18

 

Posts: 71
Score: 0
Joined: 4/7/2008
Status: offline
You can use the scripting fso for excel?  never would have thought to use that, does it have something to do with the combined use with the jet db connector?

thanks, ill try working with this and see how it goes.

(in reply to ehvbs)
 
 
Post #: 9
 
 RE: excel to array, perform functions to convert units ... - 6/25/2008 6:48:42 AM   
  ehvbs

 

Posts: 2201
Score: 50
Joined: 6/22/2005
From: Germany
Status: offline
The fso is used only to get the full file specification. the work is done
by ADO/SQL.

(in reply to chriswebb18)
 
 
Post #: 10
 
 RE: excel to array, perform functions to convert units ... - 6/25/2008 6:51:40 AM   
  chriswebb18

 

Posts: 71
Score: 0
Joined: 4/7/2008
Status: offline
ok, so with the way that this works, how do i know which cell it is reading? I'm assuming nCnt is row, but what identifiers do i use to mark like this below.  or rather what are the array identifiers,  arrexceldata(9) is the 10 column in the row, but im not sure would it be sCS(9) then? and once i'm done, how do i do the objSheet.Cells().Value = x, is it still reading as if excel.  i know it was sloppy the way i had it, im just very inexperienced with arrays and this sort of thing.


      

      
WScript.Echo nCnt, "records processed."

(in reply to chriswebb18)
 
 
Post #: 11
 
 RE: excel to array, perform functions to convert units ... - 6/25/2008 7:21:51 AM   
  ehvbs

 

Posts: 2201
Score: 50
Joined: 6/22/2005
From: Germany
Status: offline
your (new) code tries to use

  If objSheet.Cells(intRow, 15).Value <> "#N/A" Then

That's wrong, "#N/A" results in ERROR. If you change the guard to


Do Until arrExcelData(5) = ""
   If vbError = VarType( objSheet.Cells(intRow, 15).Value )  Then
       ... what shall we do here?
   Else
       arrExcelData(0) = objSheet.Cells(intRow, 1).Value

your code will fail for

    If arrExcelData(9) = "E" And arrExcelData(14) = "EA" Then

in row 2048 because arrExcelData(5) is ERROR. I don't think that

(a) using Excel.Application (instead of ADO/SQL)

(b) using indices instead of field names

(c) copy of cells into array elements and vice versa

is a good design.

(in reply to ehvbs)
 
 
Post #: 12
 
 RE: excel to array, perform functions to convert units ... - 6/25/2008 7:33:26 AM   
  ehvbs

 

Posts: 2201
Score: 50
Joined: 6/22/2005
From: Germany
Status: offline
You use the field/column names, like  oRS.Fields( "Lawson UOM" ).Value.
Which  column did you want to access with "arrExcelData(9)"? If "Lawson UOM",
why are there no values "E" in that column? Some for "arrExcelData(14)".

(in reply to ehvbs)
 
 
Post #: 13
 
 RE: excel to array, perform functions to convert units ... - 6/26/2008 3:55:59 AM   
  chriswebb18

 

Posts: 71
Score: 0
Joined: 4/7/2008
Status: offline
alright thanks, ill work on this.  and that was a typo thanks for catching it.  i had the 2 fields switched for that formula, all the others seem fine.

(in reply to ehvbs)
 
 
Post #: 14
 
 RE: excel to array, perform functions to convert units ... - 6/26/2008 5:25:08 AM   
  chriswebb18

 

Posts: 71
Score: 0
Joined: 4/7/2008
Status: offline
i have made a lot of progress with my original script and don't think i will need any help with it soon, but will work with the structure you gave me to learn that.  thanks for all your help.  im posting my script so far and will post the final when it's done.  

(in reply to chriswebb18)
 
 
Post #: 15
 
 RE: excel to array, perform functions to convert units ... - 6/26/2008 5:49:26 AM   
  ehvbs

 

Posts: 2201
Score: 50
Joined: 6/22/2005
From: Germany
Status: offline
Hi chriswebb18,

I'm happy to hear of your progress; and you are welcome - I rather liked your problem
and working with you.

As food for thought after you have finished your project, I include a further demo script.
I hope it will show you that using ADO/SQL (treating Excel as a database) may be daunting
at first, but will pay off in the long run.

The script assumes a sheet like:

Car     Speed   SpeedUnit
---------------------------
audi      160     kmh
jaguar  100     mph

to be transformed into

Car     Speed   SpeedUnit       NewCol
--------------------------------------
audi      160          kmh            4
jaguar  160          kmh            6

so we have a change/update and a new column. The code


      

output:


      

Regards

ehvbs

(in reply to chriswebb18)
 
 
Post #: 16
 
 RE: excel to array, perform functions to convert units ... - 6/30/2008 1:56:38 AM   
  chriswebb18

 

Posts: 71
Score: 0
Joined: 4/7/2008
Status: offline
a quick question.  do you know why it is that (going by my original script) the #N/A# fields gave an error?  i never did understand that.

(in reply to ehvbs)
 
 
Post #: 17
 
 RE: excel to array, perform functions to convert units ... - 6/30/2008 3:58:03 AM   
  ehvbs

 

Posts: 2201
Score: 50
Joined: 6/22/2005
From: Germany
Status: offline
Hi chriswebb18,

if you read a cell value containing "#N/A" using oSheet.Cells( iRow, iCol ) the return value is a
Variant of subtype vbError. You can't convert such a beast to a string or compare it to string
(which entails such a conversion).

Sample:


      

So if you have garbage input data, you have to check carefully whether the data supports
the actions/operations you want to apply.

Regards

ehvbs

(in reply to chriswebb18)
 
 
Post #: 18
 
 RE: excel to array, perform functions to convert units ... - 6/30/2008 4:11:28 AM   
  chriswebb18

 

Posts: 71
Score: 0
Joined: 4/7/2008
Status: offline
thanks.  I'll