Open txt file

jjpski

New Member
Joined
Jan 4, 2011
Messages
29
Good Morning,

I am completely confused on how I can parse the file sort it by dates but retain only the last 6 months then remove duplicate computer names.

Need a solution to open a txt file that is comma delimited with four fields. I need all duplicate Workstation removed but retain the recent date within the last six months.

example to the text file
Date,Workstation,Username,Domain
"2019-08-10 07:00:38","Computer1","User","Domain"
"2020-02-10 08:47:20","Computer2","User","Domain"
"2020-03-01 09:01:00","Computer3","User","Domain"
"2020-04-01 10:05:58","Computer1","User","Domain"
"2020-05-07 08:15:15","Computer2","User","Domain"

Output:
CELL(1,1) = 2020-05-07
CELL(1,2) = Computer2
CELL(2,1) = 2020-04-01
CELL(2,2) = Computer1
CELL(3,1) = 2020-03-01
CELL(3,2) = Computer3

Thanks in advance.
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
"2019-08-10 07:00:38","Computer1","User","Domain"
"2020-02-10 08:47:20","Computer2","User","Domain"
"2020-03-01 09:01:00","Computer3","User","Domain"
"2020-04-01 10:05:58","Computer1","User","Domain"
"2020-05-07 08:15:15","Computer2","User","Domain"

The following macro considers the structure of the data in the txt file as you put it in your example.

Change in the macro "C:\trabajo\pcs.txt" for the folder and name of your file.

VBA Code:
Sub Open_txt()
  Dim sPathIn As String, sComp As String, d As String
  Dim dic As Object, sLine As Variant, sItems As Variant, sDate As Date
  '
  sPathIn = "C:\trabajo\pcs.txt"
  Set dic = CreateObject("Scripting.Dictionary")
  Open sPathIn For Input As #1
  Do While Not EOF(1)
    Line Input #1, sLine
    sLine = Replace(sLine, """", "")
    sItems = Split(sLine, ",")
    d = Split(sItems(0), " ")(0)
    sDate = CDate(Right(d, 2) & "/" & Mid(d, 6, 2) & "/" & Left(d, 4)) + TimeValue(Split(sItems(0), " ")(1))
    sComp = sItems(1)
    If dic.exists(sComp) Then If sDate > dic(sComp) Then dic(sComp) = sDate
    dic(sComp) = sDate
  Loop
  Close #1
  '
  Range("A2").Resize(dic.Count, 2).Value = Application.Transpose(Array(dic.keys, dic.Items))
End Sub

Output:
varios 18dic2019 read_txt_files.xlsm
AB
1
2Computer12020-04-01 10:05:58 a.m.
3Computer22020-05-07 8:15:15 a.m.
4Computer32020-03-01 9:01:00 a.m.
Workstation


If you want another date view, just adjust the format of column B.
 
Last edited:
Upvote 0
Edit:
Consider this macro

VBA Code:
Sub Open_txt()
  Dim sPathIn As String, sComp As String, d As String
  Dim dic As Object, sLine As Variant, sItems As Variant, sDate As Date
  '
  sPathIn = "C:\trabajo\pcs.txt"
  Set dic = CreateObject("Scripting.Dictionary")
  Open sPathIn For Input As #1
  Do While Not EOF(1)
    Line Input #1, sLine
    sLine = Replace(sLine, """", "")
    sItems = Split(sLine, ",")
    d = Split(sItems(0), " ")(0)
    sDate = CDate(Right(d, 2) & "/" & Mid(d, 6, 2) & "/" & Left(d, 4)) + TimeValue(Split(sItems(0), " ")(1))
    sComp = sItems(1)
    If dic.exists(sComp) Then
      If sDate > dic(sComp) Then dic(sComp) = sDate
    Else
      dic(sComp) = sDate
    End If
  Loop
  Close #1
  '
  Range("A2").Resize(dic.Count, 2).Value = Application.Transpose(Array(dic.keys, dic.Items))
End Sub
 
Upvote 0
I'm glad to help you. Thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,524
Messages
6,120,049
Members
448,940
Latest member
mdusw

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