Hi Sayre,
!!! What a great thread this has turned out to be! I've been taking a peak at it here and but had no time to reply until now…..Well that's what I get for not keeping up ……. take a look as soon as I can tomorrow.
…I know the feeling. When You are laerning like me You’d like to keep on it but other things pull you away. It is annoying that when we are not able to spend more time on it. That is why it is so good that there are Forums like this with people like Rick giving so much of their time voluntarily..
!!! …... Rick you're way ahead of me thanks so much! I'll …...
…Rick…..We are not worthy!
ray:
…Sayre…………………..
. But it is much appreciated that You give feedback when You can and contribute back. - I’ll take a look at your latest stuff when I get time!!
. I had a go at something new. It is not complete and I am not quite happy with it just jet. – There are a few problems* and the whole thing I do not really understand. – I just Cobbled it together with a lot of googling and even more trial and error. It sort of works. But:
.* It only does csv files
.* when the format of any entry in some columns changes the entry can vanish!! (In the example below I changed 1068 with 1068h and.. it vanished!?!
.* It does not like ### in the heading. – It crashes! (So I changed that to XXX in the example below)
.* It seems to not recognize the decimal point? (That could be the problem with my German system which confuses the different English/German conventions with a , comma and point . for the decimal point?)
.* It does not copy the headers (But needs them**)
. Here is a screen shot highlighting the problems (**Note it does not copy the headings, but it has to have them and the Code must be given them)
Book1 |
---|
|
---|
| A | B | C | D | E | F | G | H |
---|
1 | | | | | | | | |
---|
2 | USSJCIRCL001PRD | 1065 | __LINEID__ | Disk_0 | Online | 2790 | 0 | |
---|
3 | USSJCIRCL002PRD | 1066 | __LINEID__ | Disk_0 | Online | 2790 | 0 | |
---|
4 | USSJCIRCL003PRD | 1067 | __LINEID__ | Disk_0 | 2Online | 2790 | 0 | |
---|
5 | USSJCIRCL004PRD | | __LINEID__ | Disk_0 | Online | 2790 | 0 | |
---|
6 | USSJCIRDB001PRD | 1056 | __LINEID__ | Disk_0 | Online | 1360 | 0 | |
---|
|
---|
. I was not going to post the code especially as it does not work fully yet. But as you may be “back on” I thought I would include it out of interest. Here it is:-
<font face=Calibri><SPAN style="color:#00007F">Sub</SPAN> GetCSVtxtDataADOdb2() <SPAN style="color:#007F00">'Using Microsoft's ActiveX Data Objects database to read text file to VBA</SPAN><br><SPAN style="color:#007F00">' This requires that you add a reference</SPAN><br><SPAN style="color:#007F00">' Microsoft Active Data Objects to support the ADO code.</SPAN><br><SPAN style="color:#007F00">' The idea is to create an ADO connection to the csv (txt) files folder,</SPAN><br><SPAN style="color:#007F00">' then select the columns you want using a standard SQL statement in</SPAN><br><SPAN style="color:#007F00">' an ADO Recordset to link to the actual file.</SPAN><br><SPAN style="color:#007F00">' So you Need to reference the Microsoft ActiveX Data Objects 2.5 Library (called early binding)</SPAN><br><SPAN style="color:#007F00">' Tools>>References>>then check Microsoft ActiveX Data Objects 2.5 Library</SPAN><br><SPAN style="color:#007F00">' ..Or crashes.....</SPAN><br><SPAN style="color:#007F00">'Dim DBcnn As ADODB.Connection ' ......here.</SPAN><br><SPAN style="color:#007F00">'Set DBcnn = New ADODB.Connection</SPAN><br><SPAN style="color:#007F00">'</SPAN><br><SPAN style="color:#007F00">' The next two lines are an alternative called Late binding.</SPAN><br><SPAN style="color:#00007F">Dim</SPAN> DBcnn <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Object</SPAN><br><SPAN style="color:#00007F">Set</SPAN> DBcnn = CreateObject("ADODB.Connection")<br><br><SPAN style="color:#00007F">Dim</SPAN> TextdtaPathstr <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN> <SPAN style="color:#007F00">'For a text file, Data Source is the folder, not the file</SPAN><br><SPAN style="color:#00007F">Let</SPAN> TextdtaPathstr = ThisWorkbook.Path<br><SPAN style="color:#00007F">Let</SPAN> DBcnn.Provider = "Microsoft.Jet.OLEDB.4.0"<br><SPAN style="color:#00007F">Let</SPAN> DBcnn.ConnectionString = "Data Source=" & TextdtaPathstr & ";" & "Extended Properties=""text;HDR=Yes;FMT=Delimited;"""<br>DBcnn.Open <SPAN style="color:#007F00">' So now you are "connected" or "pluged in" or "the tap is turned on!" and stuff can be got!</SPAN><br><br><SPAN style="color:#007F00">'Set up Excel File for recieving data</SPAN><br><SPAN style="color:#00007F">Dim</SPAN> nextRow <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Integer</SPAN><br><SPAN style="color:#00007F">Let</SPAN> nextRow = 2<br>ActiveSheet.Cells.Clear<br><br><SPAN style="color:#007F00">'This bit gets wot can be now got, for example a record set. You need to have and Know what your headings are.</SPAN><br><SPAN style="color:#00007F">Dim</SPAN> DBRecordset <SPAN style="color:#00007F">As</SPAN> ADODB.Recordset<br><SPAN style="color:#00007F">Set</SPAN> DBRecordset = <SPAN style="color:#00007F">New</SPAN> ADODB.Recordset<br><SPAN style="color:#00007F">Dim</SPAN> TextdtaFilename <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN><br><SPAN style="color:#00007F">Let</SPAN> TextdtaFilename = "SayreTextDataCSV_XXX"<br>DBRecordset.Open "SELECT hostname,hostuid,lineid,Disk_XXX,Status,Size_GB,Free_GB,Dyn,Gpt FROM [" & TextdtaFilename & ".txt]", DBcnn <SPAN style="color:#007F00">' Note: You can change hostname,hostuid,lineid,Disk_XXX,Status,Size_GB,Free_GB,Dyn,Gpt with *</SPAN><br>DBRecordset.MoveFirst <SPAN style="color:#007F00">' Moveto next "Record". For the case of a text file it is the next Row / line. But it is more compliceted with ACCESS etc.!</SPAN><br><br><SPAN style="color:#007F00">' This bit copies the selected records, ' starting at the row below the last used row in the Sheet.</SPAN><br>ActiveSheet.Cells(nextRow, 1).CopyFromRecordset DBRecordset<br><br><SPAN style="color:#007F00">'And then finally close and dispose of the connection and recordset. -Good practice to close / shut off all these things!</SPAN><br>DBRecordset.Close<br>DBcnn.Close<br><SPAN style="color:#00007F">Set</SPAN> DBRecordset = <SPAN style="color:#00007F">Nothing</SPAN><br><SPAN style="color:#00007F">Set</SPAN> DBcnn = <SPAN style="color:#00007F">Nothing</SPAN><br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN> <SPAN style="color:#007F00">'GetCSVtxtDataADOdb2()</SPAN></FONT>
. If you happen to test it out I would be interested, for example if you get the same problem with the decimal point not being recognized on your English system.
. Here is the slightly modified text data File of yours that I am using. (Put it in the same Folder as the Excel File you copy the code in.
FileSnack | Easy file sharing
Note
the comments about the Early / late Binding stuff. But I think I have organized it ( Late Binding) that it should work straight away).
. A guy on you tube uses this method a lot very successfully but with ACCESS files rather than Text Files.. I may try to contact him to see if he could help
………………………
!!! Apo, I used your code as well and it most definitely works fast on big files! Using the string function was exactly what I was talking about. DocAElstein is right, that is some super code....
.
. I agree – my favorite so far. It is something a little bit along the lines of my latest code..--- But his works!!, that is to say it does not seem to have the problems that mine does, (Shame it does not do Tab delimiters)....(But
!!! Apo, I used your code ....... I think this will be my go to method for all large files.
.
….. Do not forget the minor correction I made to his code if you use it or you will lose the last two rows of your data!!
Alan