Photo Gallery Member List Search Calendars FAQ Ticket List Log Out


RE: Excel and vbs

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

 

 
  
  Printable Version
All Forums >> [Scripting] >> WSH & Client Side VBScript >> RE: Excel and vbs
  Do you like VisualBasicScript.com? Link to us and help spread the word about our forum. Thanks!
Page: <<   < prev  1 2 3 [4]
Login
Message << Older Topic   Newer Topic >>
 RE: Excel and vbs - 12/19/2007 3:42:08 AM   
  Stefgr

 

Posts: 24
Score: 0
Joined: 11/27/2007
Status: offline
Thanks is working great . Sorry about that , that i am asking all the time just want to check if the scipt will work and at the same time learn.
I want to ask if we want to merge the cell B C that is the surname and the name . i am think like that :

Set ObjFSO = CreateObject("Scripting.FileSystemObject")
Const ForWriting = 2
Set objFile = objFSO.OpenTextFile("test.txt", ForWriting, True)
Dim ex, I, ID
Set ex = CreateObject("Excel.Application")
ex.Workbooks.Open "C:\Log1.xls"    'Open file
bGetData = False
bUserNotFound = True
With ex.ActiveSheet
For I = 1 To ex.ActiveSheet.UsedRange.Rows.Count  
If (ex.ActiveSheet.Range("B"+"C" & I).Value = "SACHLAS NIKOS" And ex.ActiveSheet.Range("I" & I).Value = "6945757417") Or (bGetData And ex.ActiveSheet.Range("B" & I).Value = "") Then
IDD = ex.ActiveSheet.Range("D" & I).Value
IDE = ex.ActiveSheet.Range("E" & I).Value
IDF = ex.ActiveSheet.Range("F" & I).Value
IDG = ex.ActiveSheet.Range("G" & I).Value
IDH = ex.ActiveSheet.Range("H" & I).Value
objfile.WriteLine "Course : " & IDD & " Absences' Limit : " & IDE & " Total Absences : " & IDF & " Excused Absences : " & IDG & " Unexcused Absences : " & IDH
bGetData = True
bUserNotFound = False
Else
bGetData = False
End if
Next 
End With
If bUserNotFound Then
objfile.WriteLine " Data Not Found."
End If
objFile.Close
ex.Quit
WScript.Quit

I am changed the If (ex.ActiveSheet.Range("B"+"C" & I).Value = "SACHLAS NIKOS"     (so we will not have seperate surname and name)  but i am getting the Data Not Found."

(in reply to ebgreen)
 
 
Post #: 61
 
 RE: Excel and vbs - 12/19/2007 3:48:49 AM   
  ebgreen


Posts: 4595
Score: 29
Joined: 7/12/2005
Status: offline
try changing this:

If (ex.ActiveSheet.Range("B"+"C" & I).Value = "SACHLAS NIKOS" 

to this:

If (ex.ActiveSheet.Range("B" & I).Value & " " & (ex.ActiveSheet.Range("C" & I).Value = "SACHLAS NIKOS" 

_____________________________

"... 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 Stefgr)
 
 
Post #: 62
 
 RE: Excel and vbs - 12/19/2007 3:58:41 AM   
  Stefgr

 

Posts: 24
Score: 0
Joined: 11/27/2007
Status: offline
i tried this :

Set ObjFSO = CreateObject("Scripting.FileSystemObject")
Const ForWriting = 2
Set objFile = objFSO.OpenTextFile("test.txt", ForWriting, True)
Dim ex, I, ID
Set ex = CreateObject("Excel.Application")
ex.Workbooks.Open "C:\Log1.xls"    'Open file
bGetData = False
bUserNotFound = True
With ex.ActiveSheet
For I = 1 To ex.ActiveSheet.UsedRange.Rows.Count  
If (ex.ActiveSheet.Range("B" & I).Value & " " & ex.ActiveSheet.Range("C" & I).Value = "APOSTOLIDIS FOIVOS" And ex.ActiveSheet.Range("I" & I).Value = "6945757417") Or (bGetData And ex.ActiveSheet.Range("B" & I).Value = "") Then
IDD = ex.ActiveSheet.Range("D" & I).Value
IDE = ex.ActiveSheet.Range("E" & I).Value
IDF = ex.ActiveSheet.Range("F" & I).Value
IDG = ex.ActiveSheet.Range("G" & I).Value
IDH = ex.ActiveSheet.Range("H" & I).Value
objfile.WriteLine "Course : " & IDD & " Absences' Limit : " & IDE & " Total Absences : " & IDF & " Excused Absences : " & IDG & " Unexcused Absences : " & IDH
bGetData = True
bUserNotFound = False
Else
bGetData = False
End if
Next 
End With
If bUserNotFound Then
objfile.WriteLine " Data Not Found."
End If
objFile.Close
ex.Quit
WScript.Quit

You said : If (ex.ActiveSheet.Range("B" & I).Value & " " & (ex.ActiveSheet.Range("C" & I).Value = "SACHLAS NIKOS"    but there was an error then i remove the ( from (ex.ActiveSheet.Range("C" & I).Value = "SACHLAS NIKOS"

Its working perfect :)

< Message edited by Stefgr -- 12/19/2007 4:01:09 AM >

(in reply to ebgreen)
 
 
Post #: 63
 
 RE: Excel and vbs - 12/19/2007 4:03:27 AM   
  ebgreen


Posts: 4595
Score: 29
Joined: 7/12/2005
Status: offline
Good...glad it is working.

_____________________________

"... 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 Stefgr)
 
 
Post #: 64
 
 RE: Excel and vbs - 12/19/2007 4:07:10 AM   
  Stefgr

 

Posts: 24
Score: 0
Joined: 11/27/2007
Status: offline
Thank you very much ! i will do some test tommorow and i will post here.
Thanks again.

(in reply to ebgreen)
 
 
Post #: 65
 
 RE: Excel and vbs - 12/19/2007 7:37:14 AM   
  ehvbs

 

Posts: 2012
Score: 48
Joined: 6/22/2005
From: Germany
Status: offline
Hi all,

having followed this discussion with the feeling that there should be another possible approach
(less Excel and more database/SQL), but afraid of disturbing the flow to what now looks like a
workable solution, I now think another of my monster posts can't hurt (Stefgr is in good hands
and doesn't need even to look at this). Furthermore as the holidays are coming somebody may
become under-worked ....

Let's treat this problem as a step by step project.

It's a project so:

Create a new directory somewhere (I called mine "stefgr", because before
thinking about the problem I couldn't come up with a more descriptive
name

Create a new text file (stefgr.notes) to collect all relevant info using a
free form (you are reading a part of my .notes file just now)

Download the "total_abs_list.xls" file to \stefgr; copy it to "total_abs_list.org.xls "
(then you can look at 'it' with Excel, while accessing 'it' from the script)

Create a new .vbs file (stefgr.vbs) containing


      
  run it; expected output:

      
  Now you can tell your boos, that the program terminates successfully.

First we need a ADO database connection to "total_abs_list.xls"; let's throw
in a simple logging facility and a ADO connection to text files as well.

For the Log we need a file to append to: add

      

at the top, and


      
at the end.

To access an ADO database of a specific kind, we have to create a ADO connection
and open this with a database type specific connections string. Read all about
connection strings at

       http://www.connectionstrings.com

Add these functions:


      
  Perhaps you'll have to change "Excel 8.0" to "Excel 9.0" or whatever to suit
your computer.

To see what we have done, add a new main function:


      
  run stefgr.vbs; expected output:

      

Now the time has come to look at the data; for that we need a new main
function (frsLook) that does some SQL to ".\total_abs_list.xls" and a utility
Sub (showRS) that displays the result set of a SELECT statement. After these
additions the file should look like:


      
expected output:

      

The column names are not so good and the last row doesn't belong to the data. Let's
do something drastic and change/delete these inconveniencies (now you see, why it
is a good idea to keep .org. copies).

Output after these arrangements:

      

For a more orderly view at the data, let's concentrate on the persons (Surname,FrsName,Phone3)
and the courses (Course). The new main function is:


      

You can use this function as a template/frame to experiment with SQL SELECT
statements. But the output is for hard core programmers only, not for users/bosses.
So let's create HTML output. New main function:


      
supported by this utility function:

      

Writing this, I was confronted with a nasty problem: The

  goFS.CreateTextFile( sFSpec, True ).Write sHTML

line caused an "bad args or params error". It took me some time to understand: Converting
the UNICODE VBScript strings to ASCII/ANSI/WindowsCodepageEncoding the poor IO-System
found some characters it couldn't handle. Instead of plainly stating this fact it blamed
my wonderful HTML string.
The Surname "KRITSOPI" and the FrsNames "CLAUDIA-ANDREEA" and "EKATERINI" contained
funny greek letters that couldn't be handled by my computer/locale (it can only do german
funny letters).

I used code like:

      
to find the culprits and changed them. (Such problems can be avoided by dealing in
UNICODE only, but if you are programming for your cultural context this better
principle isn't often adhered to.)

Now Stefgr is interested in queries/reports. Let's do some simple examples:

      
output:

      
E.g. how about this:

      

For the more interesting queries concerning persons and their time, the gaps in table
must be filled. Though this could be done automagically, I will do it on a manually
created copy of sheet1 to sheet2 to save time (and explanations). So use Excel to
copy sheet1 to a new sheet2 in "total_abs_list.xls". Furthermore, delete the (mostly)
empty column A. Cautious people may copy this changed table to Sheet3 - you never know.

The new code is straightforwards:


      

Open the connection, open the recordset (not in the default (read-only, forwards)
mode suited to display, but modifiable. Get the good data from the first line.
Loop over the rows (no ActiveExec.Range( x - y * z ).Friday.AllSelected here).
If a value is missing the the last good one, else store the nice data for later.
[At last I hope so.] To check with (Stefgr's) naked eye:


      

From this there is just a small step to a full report:


      

The SQL may seem complex, but the code gets repetitive. As I don't understand Stefgr's
informational problem/need(s) well, I won't add further queries haphazardly, but the
technique is demonstrated by:


      

Now the full script as it is at the moment:

      

I'm sure that if you work thru this (reading about ADO in ado<NN>.chm (ado28.chm
probably)) and about SQL in a good book (SQL in a Nutshell) or an online tutorial
- http://sqlzoo.net/) you'll aquire reusable skills/knowledge in an efficient
way. Feel free to ask and/or describe your information processing pet project
to let us all see, if the technique/code base is really as extensible/reusable
as I claim.

[disclaimer: Christmas is coming and I may not be able to spend as much time here
as necessary for quick responses]

Speaking of Christmas: Merry Christmas & an error free new year to you all.

ehvbs

(in reply to Stefgr)
 
 
Post #: 66
 
 RE: Excel and vbs - 12/19/2007 7:47:37 AM   
  ebgreen


Posts: 4595
Score: 29
Joined: 7/12/2005
Status: offline
Ehvbs, considering how much knowledge you have to share, it is a shame that you make your posts so terribly short. I think it would do the community a great deal of good if you could just open up and put just a little bit more information into your posts.

_____________________________

"... 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 ehvbs)
 
 
Post #: 67
 
 
Page:  <<   < prev  1 2 3 [4]
 
  

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 >> RE: Excel and vbs Page: <<   < prev  1 2 3 [4]
Jump to: