Page 1 of 1 1
Topic Options
#112586 - 2004-01-27 12:31 PM How to import multiple text files into Excel?
Co Offline
MM club member
***

Registered: 2000-11-20
Posts: 1342
Loc: NL
I need a bit help...

I have made this script which I can use to log users who have local admin permissions:
Code:


Break On
$srv='\\server\share\'+@wksta+'.txt'
;$strComputer = "comp1"
If $strComputer = ""
$strComputer = "."
EndIf

Open(1,$srv,5)
$logdata='User'+Chr(9)+'Local Group'+Chr(9)+'Workstation'+Chr(9)+'OS'+Chr(9)+'Build'+Chr(9)+'SP'+Chr(9)+'NT Version'+Chr(13)+Chr(10)
$nul=WriteLine(1,$logdata)
$logdata=$name+Chr(9)+$group+Chr(9)+@Wksta+Chr(9)+@ProductType+Chr(9)+@Build+Chr(9)+@CSD+Chr(9)+@Dos+Chr(13)+Chr(10)
$nul=WriteLine(1,$logdata)
$colGroups = GetObject("WinNT://" + $strComputer + "")
If $colGroups
$colGroups.Filter = "group",""


For Each $objGroup In $colGroups
$group=$objGroup.Name

For Each $objUser In $objGroup.Members
$name=$objUser.Name
$logdata=$name+Chr(9)+$group+Chr(10)+Chr(13)
$nul=WriteLine(1,$logdata)
? $name +"-"+ $group
Sleep 1
Next
Next
EndIf
:end
$logdata=Chr(10)+Chr(13)+Chr(10)+Chr(13)
$nul=WriteLine(1,$logdata)

$nul=Close(1)



with output:
Quote:


User Local Group Workstation OS Build SP NT Version
TESTPC2 Windows XP Professional 2600 Service Pack 1 5.1
Administrator Administrators

Domain Admins Administrators

XPtest Administrators





Every workstation creates its own logfile. I want to import all these files into one Excel Worksheet.

I created a macro in Excel which import a single file:
Code:

Sub import()
'
' import Macro
' Macro recorded 1/27/2004 by Co
'

'
Workbooks.OpenText Filename:="\\Server\share\TESTPC2.txt", _
Origin:=437, StartRow:=1, DataType:=xlDelimited, TextQualifier:= _
xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, Semicolon:=False, _
Comma:=False, Space:=False, Other:=False, FieldInfo:=Array(Array(1, 1), _
Array(2, 1), Array(3, 1), Array(4, 1), Array(5, 1), Array(6, 1), Array(7, 1), Array(8, 1), _
Array(9, 1), Array(10, 1), Array(11, 1), Array(12, 1), Array(13, 1), Array(14, 1), Array(15 _
, 1)), TrailingMinusNumbers:=True
Columns("A:A").EntireColumn.AutoFit
Columns("B:B").EntireColumn.AutoFit
Columns("C:C").EntireColumn.AutoFit
Columns("D:D").EntireColumn.AutoFit
Columns("E:E").EntireColumn.AutoFit
Columns("F:F").EntireColumn.AutoFit
Columns("G:G").EntireColumn.AutoFit
Columns("H:H").EntireColumn.AutoFit
Columns("I:I").EntireColumn.AutoFit
Columns("J:J").EntireColumn.AutoFit
ActiveWindow.ScrollColumn = 2
ActiveWindow.ScrollColumn = 3
ActiveWindow.ScrollColumn = 4
Columns("K:K").EntireColumn.AutoFit
Columns("L:L").EntireColumn.AutoFit
Columns("M:M").EntireColumn.AutoFit
Columns("N:N").EntireColumn.AutoFit
ActiveWindow.ScrollColumn = 5
Columns("O:O").EntireColumn.AutoFit
End Sub





Any idea how to modify this macro so it will import multiple files.. Or is there a way Kixtart can do the job??


Edited by Co (2004-01-28 09:26 AM)
_________________________
Co


Top
#112587 - 2004-01-28 04:44 PM Re: How to import multiple text files into Excel?
Stevie Offline
Starting to like KiXtart
*****

Registered: 2002-01-09
Posts: 199
The OpenText method is only available with the Workbooks object. When it's called, it creates a new workbook with a new worksheet and assigns all text data to this worksheet. OpenText cannot be used to open a second file into the same workbook object, let alone the same worksheet.

The only way to do this is to parse out the data and write it to the same worksheet.

Alternatively, you can create a single workbook object and add a new worksheet for each file you want to import. Via kix, parse the data and add it to each worksheet. When finished you can call the MergeWorkbook object.

Here's another thought: Programmatically copy the contents of each file to the clipboard and paste the contents into a given cell. If this is one column of data, for example, paste the first file into A1, the second into B1, etc. That way, you could have all of your data in one worksheet from the start.
_________________________
Stevie

Top
#112588 - 2004-01-28 04:48 PM Re: How to import multiple text files into Excel?
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11165
Loc: Boston, MA, USA
If all the .CVS files have the same format then copy them all into one .CVS file and import that one.
Code:

shell '%COMSPEC% /c copy *.CVS ALLFILES.CVS'



Edited by sealeopard (2004-01-28 08:56 PM)
_________________________
There are two types of vessels, submarines and targets.

Top
#112589 - 2004-01-28 08:45 PM Re: How to import multiple text files into Excel?
Co Offline
MM club member
***

Registered: 2000-11-20
Posts: 1342
Loc: NL
The amount of colums is always the same. The amount of rows can differ..

I got this peace of script below but it gives an error: Compile Error - User-defined type not defined at:
Function GetFiles(strFolderSpec As String) As Variant


Code:

Function GetFiles(strFolderSpec As String) As Variant

Dim fso As Scripting.FileSystemObject
Dim fld As Scripting.Folder
Dim fil As Scripting.File
Dim temp() As String
ReDim temp(0)

Set fso = New Scripting.FileSystemObject
Set fld = fso.GetFolder(strFolderSpec)
For Each fil In fld.Files
temp(UBound(temp)) = fil.Name
ReDim Preserve temp(UBound(temp) + 1)
Next fil
ReDim Preserve temp(UBound(temp) - 1)

GetFiles = temp()
End Function

Sub Main
Dim strFolder As String
Dim strFiles As Variant
Dim strFile As Variant

strFolder = "Z:\vba\temp\"
strFiles = GetFiles(strFolder)

For Each strFile In strFiles
Workbooks.OpenText Filename:=strFolder & strFile, Tab:=True
Next strFile
End Sub



I had the same ideas you did. I've tried if readfile() and writefile() can do the job but couldn't finished it before the end of the day...
_________________________
Co


Top
#112590 - 2004-01-28 09:11 PM Re: How to import multiple text files into Excel?
Co Offline
MM club member
***

Registered: 2000-11-20
Posts: 1342
Loc: NL
Wow, Jens you are again my man!!

...I don't think I tell my Boss it is that easy

And after this i can use the above macro again

_________________________
Co


Top
Page 1 of 1 1


Moderator:  Shawn, ShaneEP, Ruud van Velsen, Arend_, Jochen, Radimus, Glenn Barnas, Allen, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 2220 anonymous users online.
Newest Members
Viginette, ManuvdWielNL, Sir_Barrington, batdk82, StuTheCoder
17888 Registered Users

Generated in 0.145 seconds in which 0.119 seconds were spent on a total of 12 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org