Question : How could the related programs be adapted to be interactive - asking the user for the number of levels and the source file and pull into Excel.

It would be very useful to have the program ask the user how many levels there are and to be able to choose a directory and then select file for processing.

For example,
If the file was formatted as the following,  then the answer to the levels question would be 2.
United States
<tab>New York
<tab>California
Canada
<tab>British Columbia
<tab>Manitoba

and would be turned into:
United States<tab>New York
United States<tab>California
Canada<tab>British Columbia
Canada<tab>Manitoba

If the file was formatted as the following, then the answer to the levels question would be 3.
United States
<tab>New York
<tab><tab>Buffalo
<tab><tab>New York City
<tab>California
<tab><tab>San Francisco
<tab><tab>Los Angeles
Canada
<tab>British Columbia
<tab><tab>Vancouver

United States<tab>New York<tab>Buffalo
United States<tab>New York<tab>New York City
United States<tab>California<tab>San Francisco
United States<tab>California<tab>Los Angeles
Canada<tab>British Columbia<tab>Vancouver

Answer : How could the related programs be adapted to be interactive - asking the user for the number of levels and the source file and pull into Excel.

when pulled into excel, do you want it split into Cells or have the tabs in one cell ?

I have modified exx1976 code to be used as Excel VBA.


1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
Sub Import1()

    Dim rngStart As Range
    Set oFS = CreateObject("Scripting.FileSystemObject")
    FOpen = Application.GetOpenFilename("Text Files (*.txt), *.txt")
    Set oFile = oFS.OpenTextFile(FOpen, 1)
    'Set outfile = oFS.CreateTextFile("c:\SaveDataFile.txt")
    Set rngStart = ActiveCell
    'save the split data at the currect active cell.
    Dim myArr()
    ReDim myArr(0)
    Dim tempArr()
    Do While Not oFile.AtEndOfStream
        sData = oFile.Readline
        If InStr(sData, vbTab) Then
            strarr = Split(sData, vbTab)
            If UBound(strarr) <= UBound(myArr) Then
                SaveData myArr
                myArr(UBound(strarr)) = strarr(UBound(strarr))
                If UBound(strarr) < UBound(myArr) Then
                    ReDim tempArr(UBound(strarr))
                    For i = 0 To UBound(strarr)
                        tempArr(i) = myArr(i)
                    Next
                    ReDim Preserve myArr(UBound(strarr))
                    For i = 0 To UBound(myArr)
                        myArr(i) = tempArr(i)
                    Next
                    myArr(UBound(strarr)) = strarr(UBound(strarr))
                End If
            End If
            If UBound(strarr) > UBound(myArr) Then
                ReDim Preserve myArr(UBound(strarr))
                myArr(UBound(strarr)) = strarr(UBound(strarr))
            End If
        Else
            If UBound(myArr) > 0 Then SaveData myArr
            ReDim myArr(0)
            myArr(0) = sData
        End If
    Loop
    SaveData myArr
    oFile.Close
    rngStart.Select  'move back to beginning
    

End Sub

Sub SaveData(arr As Variant)

    For i = 0 To UBound(arr)
        ActiveCell.Offset(0, i).Value = arr(i)
    Next
    ActiveCell.Offset(1, 0).Select
End Sub
Random Solutions  
 
programming4us programming4us