Modify VBA text import code

suprsnipes

Active Member
Joined
Apr 26, 2009
Messages
434
Hi,

I am using the following code that I found but I would like to modify it as follows;

I would like place the data into different columns based on a fixed width of
8,12,16,21,22,25 (starting from 0).

Code:
Sub GetTextFile()
   
    Dim sFile As String
    Dim sInput As String
    Dim lFNum As Long
    Dim vaFields As Variant
    Dim i As Long
    Dim lRow As Long
    Dim vaStrip As Variant
   
    Const sDELIM = "^" 'Set the delimeter
   
    lFNum = FreeFile
    sFile = "\\C8b22801\c\q\130610"
    vaStrip = Array(vbLf, vbTab) 'list the text to strip
   
    'Open the file
    Open sFile For Input As lFNum
   
    'Loop through the file until the end
    Do While Not EOF(lFNum)
        Line Input #lFNum, sInput 'input the current line
       
        'remove the unwanted text
        For i = LBound(vaStrip) To UBound(vaStrip)
            sInput = Replace(sInput, vaStrip(i), "")
        Next i
       
        'split the text based on the delimeter
        vaFields = Split(sInput, sDELIM)
        lRow = lRow + 1
       
        'Write to the worksheet
        For i = 0 To UBound(vaFields)
            Sheet1.Cells(lRow, i + 1).Value = vaFields(i)
        Next i
    Loop
   
    Close lFNum
   
End Sub

Regards,
suprsnipes
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.

Forum statistics

Threads
1,224,564
Messages
6,179,548
Members
452,927
Latest member
rows and columns

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