Results 1 to 4 of 4

How do I prevent variable from getting cleared before completing loop

Threaded View

  1. #1
    Registered User
    Join Date
    07-05-2018
    Location
    Canada
    MS-Off Ver
    2016
    Posts
    26

    How do I prevent variable from getting cleared before completing loop

    I'm getting a runtime error 75 file/path access error due to the script "forgetting" the filepath name stored in ReadTextFile

    The file being read looks something like this:
    %
    O1234( 1234567.01 1ST.NCF )
    stuff
    ( TOOL 25: 38.1 FACE ENDMILL )
    ( 1 1/2" FACE MILL )
    stuff
    ( TOOL 22: 101.6 FACE ENDMILL )
    ( 4" FACE MILL )
    stuff
    M30
    %
    Sheet1 looks something like this:
    Vert 4
    C:\Data\My Code\1234567\Place\1234567.07 1st.NCF
    C:\Data\My Code\5789975\Folder\5789975.12 2nd.NCF
    C:\Data\My Code\32478\Subfoler 5\32478.35 1st.NCF
    And the script is as follows:
    Sub ExtractDataFromFiles()
        Dim wb As Workbook
        Dim wsQuery As Worksheet
        Dim wsData As Worksheet
        Dim LastRow As Long
        Dim FileCell As Range
        Dim FileText As String
        Dim TextLine As Variant
        Dim ToolData As String
        Dim Extract As Boolean
        
        ' Set references to worksheets
        Set wb = ThisWorkbook
        Set wsQuery = wb.Sheets("Sheet1")
        Set wsData = wb.Sheets("Sheet2")
        
        ' Clear the data on the destination sheet
        wsData.Cells.Clear
        
        ' Loop through each file path in column D of the Query sheet
        LastRow = wsQuery.Cells(wsQuery.Rows.Count, "D").End(xlUp).Row
        For Each FileCell In wsQuery.Range("D2:D" & LastRow)
            FileText = ReadTextFile(FileCell.Value)
            
            ' Check if the file could not be opened
            If FileText = "File Not Found" Then
                ' Handle the error as needed (e.g., display a message)
                MsgBox "File not found: " & FileCell.Value
                Exit Sub ' Skip processing this file and move to the next one
            End If
            
            ' Reset variables for each file
            ToolData = ""
            Extract = False
            
            ' Split the file text into an array of lines
            Dim Lines() As String
            Lines = Split(FileText, vbNewLine)
            
            ' Loop through each line in the array
            For Each TextLine In Lines
                If InStr(1, TextLine, "%", vbTextCompare) > 0 Then
                    ' Extract the line after the first percent sign
                    Extract = True
                ElseIf Extract Then
                    ' Extract the line that follows the previously extracted line
                    ToolData = ToolData & TextLine & vbNewLine
                    Extract = False
                ElseIf InStr(1, TextLine, "( TOOL ", vbTextCompare) > 0 Then
                    ' Extract lines that start with "( TOOL "
                    ToolData = ToolData & TextLine & vbNewLine
                    Extract = True
                End If
            Next TextLine
            
            ' Write the extracted data to the destination sheet
            wsData.Cells(wsData.Rows.Count, 1).End(xlUp).Offset(1, 0).Value = ToolData
        Next FileCell
    End Sub
    
    Function ReadTextFile(ByVal FilePath As String) As String
        ' This function reads a text file and returns its contents as a string
        Dim FileContent As String
        Dim FileNum As Integer
        
        FileNum = FreeFile
        Open FilePath For Input As FileNum
        FileContent = Input$(LOF(FileNum), FileNum)
        Close FileNum
        
        ReadTextFile = FileContent
    End Function
    Attached Files Attached Files

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. finding cell value and completing loop based on that
    By ammartino44 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-02-2015, 08:06 PM
  2. [SOLVED] How to prevent certain cells from being deleted when page is cleared
    By behnam in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 07-23-2013, 01:17 PM
  3. A Loop not completing
    By sgarciareilly in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 04-17-2013, 07:07 PM
  4. Loop only completing first loop
    By jcommins in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-08-2013, 01:08 PM
  5. Why did an inner loop variable start overwriting the outer loop range suddenly?
    By 111StepsAhead in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-16-2012, 03:24 PM
  6. For Loop not completing
    By somnath.deb in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 09-01-2011, 11:55 PM
  7. How do I prevent Crtl+End moving to a cleared cell?
    By Ralph in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 03-04-2006, 06:35 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1