Link to home
Start Free TrialLog in
Avatar of jcgroove
jcgroove

asked on

Import single specific delimited column from text file to a userform textbox

I have a text file that has alot of superfluous data in it that i need to pull specific data from using vba. The specific data is listed beneath the CALIBRATION DATA under the "Channel" heading. I only need the channel number and its following comma to be imported to a userform textbox. Additionally, I need there to be no spaces between the different numbers.
example: (2,3,5,7,8)

I have attached the text file I'm using.

Thanks for any help!
29.txt
ASKER CERTIFIED SOLUTION
Avatar of Rgonzo1971
Rgonzo1971

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 jcgroove
jcgroove

ASKER

Thanks! That worked really well. Here is the final product.

Private Sub CommandButton2_Click()
    Dim SelectedFile As String
    With Application.FileDialog(msoFileDialogFilePicker)
    .AllowMultiSelect = False
    .Title = "Select the text file"
    .ButtonName = "Confirm"
    .InitialFileName = "C:\"
    If .Show = -1 Then
    SelectedFile = .SelectedItems(1)
    Open SelectedFile For Input As #1
    ImportTextFile = Input$(LOF(1), 1)
    Close #1
    ImportTextFile = Mid(ImportTextFile, InStr(1, ImportTextFile, "= CALIBRATION DATA =") + 2)
    ImportTextFile = Mid(ImportTextFile, 1, InStr(1, ImportTextFile, vbCrLf & vbCrLf))
    aImportTextFile = Split(ImportTextFile, vbCrLf)
    For Each Item In aImportTextFile
        If Item Like "*#,*" Then
            Res = Res & WorksheetFunction.Clean(Split(Trim(Item), " ")(0))
        End If
    Next
    Else
    Unload Me
    End If
    End With
    TextBox1.Value = Res
    If Right(TextBox1, 1) = "," Then TextBox1 = Left(TextBox1, Len(TextBox1) - 1)
End Sub

Open in new window