Hello,
Recently one of our systems underwent a large upgrade and one of the files I receive everyday is about 10x bigger than it was. I had this automated process, where excel would open this file and go to the very bottom, then to the very right, and copy this value into a master sheet for me. This is simply the count of the # of records in the file.
The problem is now, this file is 200 000 rows, too big for our Excel 2003 to handle at work... so I looked around and found some code that can import this into multiple spreadsheets, but I am trying to avoid this as I have to do this with around 40 files, is there a way to just import the last row? or the last section?, -- as the only value in this entire file that I require is the the rightmost value in the very bottom row.
Thanks for any suggestions in advance,
Adam
Last edited by AdamParker; 10-22-2009 at 07:01 PM.
A software upgrade may be the simplest answer, Excel 2007 can handle 1,048,576 rows x 16,384 columns.
Regards
Rick
Win7, Office 2010
Hello Adam,
What type of file are you referring to? Is it an Excel workbook, a CSV, a text file, or a database?
Sincerely,
Leith Ross
Remember To Do the Following....
1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.2. Thank those who have helped you by clicking the Starbelow the post.
3. Please mark your post [SOLVED] if it has been answered satisfactorily.
Old Scottish Proverb...
Luathaid gu deanamh maille! (Rushing causes delays!)
Hello,
Unfortunately a software upgrade is out of the question right now... I work for a place that is very slow to update... we are expecting Excel 2007 to be rolled out next year.
I am importing a text file.
Hello AdamParker,
I haven't test this macro as I don't have a file large enough to import. The macro will prompt you for the text file to open and start importing the data at cell "A1" of the last worksheet in the workbook (usually Sheet3). Once the sheet is filled, a new sheet will be added to the workbook. This process will be repeated until the file is downloaded. The default delimiter is a comma. You change this and the starting worksheet in the code. They are marked in red. The file status is displayed in Excel's Status Bar .
Code:'Written: October 21, 2009 'Author: Leith Ross 'Summary: Imports a text file greater than 65535 lines into Excel. Sub ImportTextFile() Dim Arr As Variant Dim Data As Variant Dim Delimiter As String Dim FileFilter As String Dim FileName As String Dim N As Integer Dim R As Long Dim Wks As Worksheet Delimiter = "," Set Wks = Worksheets("Sheet3") FileFilter = "Text Files (*.csv; *.txt), *.csv;*.txt, All Files (*.*), *.*" FileName = Application.GetOpenFilename(FileFilter, 0, "Open") If FileName = "False" Then Exit Sub N = FreeFile Application.DisplayStatusBar = True Open FileName For Input As #N Do While Seek(N) <= LOF(N) R = R + 1 If R > Rows.Count Then R = 1 Set Wks = Worksheets.Add(After:=Worksheets.Count) End If Line Input #N, Data Arr = Split(Data, Delimiter) Wks.Cells(R, "A").Resize(ColumnSize:=UBound(Arr) + 1).Value = Arr Application.StatusBar = "Writing Row " & R & " on " & Wks.Name Loop Close #N Application.StatusBar = "Download complete." End Sub
Sincerely,
Leith Ross
Remember To Do the Following....
1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.2. Thank those who have helped you by clicking the Starbelow the post.
3. Please mark your post [SOLVED] if it has been answered satisfactorily.
Old Scottish Proverb...
Luathaid gu deanamh maille! (Rushing causes delays!)
Hi Leith,
There is an error in the code unfortunately...
When it gets to "Writing Row 65536 on Sheet3" - an alert comes up stating, "Run-time error '1004': Method 'Add' of object 'Sheets' failed".. when I press Debug, .... the 3rd row here is highlighted, "Set Wks"
What is the problem here?Code:If R > Rows.Count Then R = 1 Set Wks = Worksheets.Add(After:=Worksheets.Count) 'HIGHLIGHTED End If
Also, would it be possible rather than to place this in different sheets, to have this placed into different columns? - So fill column A with 65536 rows, then goto col B, etc... (with no delimiter, place entire row of text into one cell)... Thanks very much Leith, you are helping me out tremendously.
Adam
If all you need is the last value, why copy the file to the worksheet at all?
Microsoft MVP - Excel
Entia non sunt multiplicanda sine necessitate
Because we do this for a few hundred files, only about 40 of these files are now exceeding the excel row limit... could open them manually, but automating this would save time.
Originally Posted by AP
Originally Posted by shg
Originally Posted by AP
My question is, why read any file in at all, instead of just having code that reads to the bottom of the file and gets the one value you need?
Microsoft MVP - Excel
Entia non sunt multiplicanda sine necessitate
Hi shg,
My inexperience with vba is what is causing this confusion...
When I designed this process initially, I used the "record macro" feature, opened the file manually.. pressed ctrl+down, ctrl+right, copied value, closed the file...
I now understand that you're saying that there is no need to open the file... this is correct, but I do not know how to do this... the value can be anywhere from 0 to several million and I just need this recorded in my master sheet... This is just a crude reconciliation process... can you let me know how this is done?
So ... how about this:
Code:Sub TheLastWord() Dim sFile As String Dim iFF As Integer Dim sLine As String Dim asWd() As String sFile = Application.GetOpenFilename(FileFilter:="Text Files, *.csv;*.txt", _ Title:="Pick a file") If sFile = "False" Then Exit Sub iFF = FreeFile Open sFile For Input As #iFF Do Until EOF(iFF) Line Input #iFF, sLine Loop Close #iFF asWd = Split(sLine, ",") MsgBox Prompt:=asWd(UBound(asWd)), Title:="... and the last comma-delimited phrase is:" End Sub
Microsoft MVP - Excel
Entia non sunt multiplicanda sine necessitate
Thank you very much... I made some adjustments, but it works great! Better than before!
You're welcome. A little simplication, and coded as a function:
Code:Function TheLastWord() As String ' Returns the last comma-delimited item from a text file ' VBA function only Dim sFile As String Dim iFF As Integer sFile = Application.GetOpenFilename(FileFilter:="Text Files, *.csv;*.txt", _ Title:="Pick a file") If sFile = "False" Then Exit Function iFF = FreeFile Open sFile For Input As #iFF Do Until EOF(iFF) Line Input #iFF, TheLastWord Loop Close #iFF TheLastWord = Mid(TheLastWord, InStrRev(TheLastWord, ",") + 1) End Function
Last edited by shg; 10-23-2009 at 12:54 PM. Reason: typos
Microsoft MVP - Excel
Entia non sunt multiplicanda sine necessitate
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks