Copying last 18 records of text file to worksheet

sdruley

Well-known Member
Joined
Oct 3, 2010
Messages
557
Office Version
  1. 365
Platform
  1. Windows
URL:
https://www.screencast.com/t/Zq5GPIg7Dsm
EMBEDDED CODE:
<!-- copy and paste. Modify height and width if desired. --> <a href="https://content.screencast.com/users/pharaohdruley/folders/Default/media/1f24e3a6-f70e-4c59-98e5-08f405443f6c/09.08.2018-11.38.png"><img class="embeddedObject" src="https://content.screencast.com/users/pharaohdruley/folders/Default/media/1f24e3a6-f70e-4c59-98e5-08f405443f6c/09.08.2018-11.38.png" width="2670" height="1028" border="0" /></a>
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
Maybe something like this...

Code:
Option Explicit

Sub CopyLast18RecordsOfTextFileToWorksheet()


    Dim oFSO As Object
    Dim oFile As Object
    Dim oTextStream As Object
    Dim sFullName As String
    Dim vText As Variant
    Dim LastRow As Long
    
    sFullName = "C:\Users\Domenic\Desktop\sample.txt"
    
    Set oFSO = CreateObject("Scripting.FileSystemObject")
    
    If Not oFSO.FileExists(sFullName) Then
        MsgBox "File not found!", vbExclamation
        Exit Sub
    End If
    
    Set oFile = oFSO.GetFile(sFullName)
    
    If oFile.Size = 0 Then
        MsgBox "File is empty!", vbExclamation
        Exit Sub
    End If
    
    Set oTextStream = oFile.OpenAsTextStream
    
    vText = Split(oTextStream.ReadAll, vbCrLf)
    
    Worksheets.Add
    
    Range("A1").Resize(UBound(vText) + 1).Value = Application.Transpose(vText)
    
    LastRow = Cells(Rows.Count, "A").End(xlUp).Row
    If LastRow - 18 > 0 Then
        Rows("1:" & LastRow - 18).Delete
        LastRow = Cells(Rows.Count, "A").End(xlUp).Row
    End If
    
    Range("A1:A" & LastRow).TextToColumns _
        Destination:=Range("A1"), _
        DataType:=xlDelimited, _
        Comma:=True


    oTextStream.Close
    
    Set oFSO = Nothing
    Set oFile = Nothing
    Set oTextStream = Nothing
    
End Sub

Hope this helps!
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,175
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top