Excel 2010 vb script not working correctly

Guys, I have a piece of vb script that is suppose to look into a specific folder read each of the files in there ending with extension ".PRN" then import the contents of these files into an excel workbook. What is happening is that it is partially working I get no errors but when I double check what the script has ran against the files in the folder there are plenty missing. But I am unsure why they are missing.

Can anyone hold any ideas as to why this is the case.

Public Sub Import(cPath As String)
    Dim strFile               As String
    Dim strData               As String
    Dim intFN                 As Integer
    Dim rng                   As Range
    Dim wks                   As Worksheet
    Dim vData                 As Variant
   
    Set wks = ActiveWorkbook.Worksheets("Import")
    Set rng = wks.Range("A1")
    strFile = Dir(cPath & "*.prn")
    Application.ScreenUpdating = False
   
    Sheets("Import").Select
    Cells.Select
    Selection.ClearContents
    Sheets("Main").Select
   
    Do Until Len(strFile) = 0
        intFN = FreeFile
        Open cPath & strFile For Input As #intFN
        strData = Input(LOF(intFN), #intFN)
        Close intFN
        vData = Split(strData, vbCrLf)
        wks.Range(rng, rng.Offset(UBound(vData))).Value = WorksheetFunction.Transpose(vData)
        'rng.Value = Left(strFile, Len(strFile) - 4)
        Set rng = rng.Offset(1, 0)
        strFile = Dir()
    Loop

    Columns("A:A").Select
    ActiveWorkbook.Worksheets("Import").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Import").Sort.SortFields.Add Key:=Range("A1:A1048576" _
                                                                       ), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    With ActiveWorkbook.Worksheets("Import").Sort
        .SetRange Range("A1:A1048576")
        .Header = xlGuess
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
   
    End With
    Range("A1").Select


    Application.ScreenUpdating = True
End Sub


Regards
DarrenJacksonAsked:
Who is Participating?
 
Rgonzo1971Commented:
Hi,

pls try

    Do Until Len(strFile) = 0
         intFN = FreeFile
         Open cPath & strFile For Input As #intFN
         strData = Input(LOF(intFN), #intFN)
         Close intFN
         vData = Split(strData, vbCrLf)
         wks.Range(rng, rng.Offset(UBound(vData))).Value = WorksheetFunction.Transpose(vData)
         'rng.Value = Left(strFile, Len(strFile) - 4)
         Set rng = rng.Offset(UBound(vData), 0)
         strFile = Dir()
     Loop

Open in new window

Regards
0
 
DarrenJacksonAuthor Commented:
Here is a sample of 1 of the files where it only uploaded partially
Sample.prn
0
 
DarrenJacksonAuthor Commented:
Thanks for this let me test it
0
 
DarrenJacksonAuthor Commented:
perfect thanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.