Link to home
Start Free TrialLog in
Avatar of Troush2009
Troush2009Flag for United States of America

asked on

Make a macro that inport data from a txt file into a excel file.

I have a excel file that I want to create a button on it. When button is clicked then it will pull dsts from a IDSOdump.txt file and put it in the excel file.


Below is an example of the IDSOdump.txt file data.

;ed-chan:::TR::dbchg=off,naevt=off,sirpt=off,aud=off:mlp=off;

   DNVRCOIID31 09-03-30 07:26:45
M  TR COMPLD
;rtrv-crs-ds0::ds0int-577-1-1-1&&-24:cb;

   DNVRCOIID31 09-03-30 07:26:46
M  CB COMPLD
   "FROM=DS0INT-0577-1-1-01,TO=DS0INT-0583-1-1-05:CCSTATE=XC,DS0TC=TRSP,DS0TW=FF::,SST=RDLD"
   "FROM=DS0INT-0577-1-1-02,TO=DS0INT-0583-1-1-06:CCSTATE=XC,DS0TC=TRSP,DS0TW=FF::,SST=RDLD"
   "FROM=DS0INT-0577-1-1-03,TO=DS0INT-0583-1-2-05:CCSTATE=XC,DS0TC=TRSP,DS0TW=FF::,SST=RDLD"
   "FROM=DS0INT-0577-1-1-04,TO=DS0INT-0583-1-2-06:CCSTATE=XC,DS0TC=TRSP,DS0TW=FF::,SST=RDLD"
   "FROM=DS0INT-0577-1-1-05,TO=DS0INT-0583-1-1-09:CCSTATE=XC,DS0TC=TRSP,DS0TW=FF::,SST=RDLD"
   "FROM=DS0INT-0577-1-1-06,TO=DS0INT-0583-1-2-09:CCSTATE=XC,DS0TC=TRSP,DS0TW=FF::,SST=RDLD"
   "FROM=DS0INT-0577-1-1-23,TO=DS0INT-0583-1-1-03:CCSTATE=XC,DS0TC=TRSP,DS0TW=FF::,SST=RDLD"
   "FROM=DS0INT-0577-1-1-24,TO=DS0INT-0583-1-2-03:CCSTATE=XC,DS0TC=TRSP,DS0TW=FF::,SST=RDLD"
;rtrv-crs-ds0::ds0int-577-1-2-1&&-24:cb;

   DNVRCOIID31 09-03-30 07:26:51
M  CB COMPLD
   "FROM=DS0INT-0577-1-2-01,TO=DS0INT-0583-1-1-15:CCSTATE=XC,DS0TC=TRSP,DS0TW=FF"
   "FROM=DS0INT-0577-1-2-02,TO=DS0INT-0583-1-1-16:CCSTATE=XC,DS0TC=TRSP,DS0TW=FF"
   "FROM=DS0INT-0577-1-2-03,TO=DS0INT-0583-1-1-17:CCSTATE=XC,DS0TC=TRSP,DS0TW=FF"
   "FROM=DS0INT-0577-1-2-04,TO=DS0INT-0583-1-1-18:CCSTATE=XC,DS0TC=TRSP,DS0TW=FF"
;rtrv-crs-ds0::ds0int-577-1-3-1&&-24:cb;

The only information I want in the spread sheet is every time it say "FROM=DS0INT-0577-1-1-01,TO=DS0INT-0583-1-1-05" I want the following in the spread sheet.
I would like to strip the 0 off of the begin of each number but if it is another number beside 0 then leave it.
Column A      and      Column B
577-1-1-01              583-1-1-05


IDSOdump.txt
Tellabs-iDS0.xls
Avatar of ExcelGuide
ExcelGuide
Flag of Netherlands image

Hi, I have written a code for this part. See code snippet. Create a button on your sheet and assign the macro to it. One thing though: the code is not quite fast...I could take a couple of minutes (+-2 minutes)

hope to see your reply soon.
Sub macro1()
Dim cel As Range
 
    Flnm = Application.GetOpenFilename(, , "Open File")
    
    With ActiveSheet.QueryTables.Add(Connection:= _
        "TEXT;" & Flnm & "", Destination _
        :=Range("A1"))
        .Name = "IDSOdump"
        .FieldNames = True
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .RefreshStyle = xlInsertDeleteCells
        .SavePassword = False
        .SaveData = True
        .AdjustColumnWidth = True
        .RefreshPeriod = 0
        .TextFilePromptOnRefresh = False
        .TextFilePlatform = 850
        .TextFileStartRow = 1
        .TextFileParseType = xlDelimited
        .TextFileTextQualifier = xlTextQualifierDoubleQuote
        .TextFileConsecutiveDelimiter = False
        .TextFileTabDelimiter = False
        .TextFileSemicolonDelimiter = False
        .TextFileCommaDelimiter = True
        .TextFileSpaceDelimiter = False
        .TextFileColumnDataTypes = Array(1, 1, 1, 1)
        .TextFileTrailingMinusNumbers = True
        .Refresh BackgroundQuery:=False
    End With
 
    Columns("C:H").Select
    Selection.Delete Shift:=xlToLeft
 
    For Each cel In Columns("A:B").Cells
        If Mid(cel.Value, 5, 4) = "FROM" Or Left(cel.Value, 3) = "TO=" Then
            cel.Replace What:="   ""FROM=DS0INT-", Replacement:="", LookAt:= _
                xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
                ReplaceFormat:=False
            cel.Replace What:="TO=DS0INT-", Replacement:="", LookAt:=xlPart, _
                SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
                ReplaceFormat:=False
            cel.Replace What:=":CCSTATE=XC", Replacement:="", LookAt:=xlPart, _
                SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
                ReplaceFormat:=False
        Else
            cel.Clear
        End If
    Next
 
Columns(1).SpecialCells(xlCellTypeBlanks).EntireRow.Delete
 
End Sub

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Robberbaron (robr)
Robberbaron (robr)
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Troush2009

ASKER

Great work this is what I was looking for.