Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

Macro not workinng

0

The following code is not able to import text after "$"  which should import as semicolon separated.

Dim ATTFile As Object, filepath As String, textline As String, start As Integer, i As Integer, j As Double

j = 2

    filepath = ThisWorkbook.Path & "\" & Sheets(1).Range("D1048576").End(xlUp).Value

    Open filepath For Input As #1

    Do Until EOF(1)

        Line Input #1, textline

        If Left(textline, 1) = "$" Then start = start + 1

        If start = 2 Then

            For i = 0 To UBound(Split(Replace(CStr(textline), " ", ""), ";"))

                Sheets(2).Cells(j, i + 1) = Split(Replace(CStr(textline), " ", ""), ";")(i)

            Next i

            j = j + 1

        End If

    Loop

    Close #1

Answer
Discuss

Answers

0

Hi Jaysan and welcome to the Forum.

You didn't embed a source file so I'm guessing (from your Replace statements) that you have text files of several lines with data separated by spaces like this:

Head W X Y Z
$ a b c d
 A B C D
$ 1 2 3 4
$ 5 6 7 8

and you want to import only those starting with $. (Bear with me please...)

In the code below, I defined a new variable MyArray (which simplifies the Ubound line andthat  writing to cells) and made both the Replace and Split methods have the semicolon ";" (i.e. replace spaces in the text line with a semi-colon then split that string into an array using the semi-colon as the division) . See changes in bold (including commenting out lines using "start"- which may be wrong- see text below code):

Sub checkATT()
Application.Calculation = xlCalculationManual
Application.ScreenUpdating = False
Application.DisplayStatusBar = False
Application.EnableEvents = False
ActiveSheet.DisplayPageBreaks = False
 
Sheets(2).Range("A2:W1048576").ClearContents
Sheets(2).Range("X4:AF1048576").ClearContents
 
Dim ATTFile As Object, filepath As String, textline As String, start As Integer, i As Integer, j As Double
Dim MyArray As Variant
 
j = 2
    filepath = ThisWorkbook.Path & "\" & Sheets(1).Range("D1048576").End(xlUp).Value
    Open filepath For Input As #1
    
    Do Until EOF(1)
        Line Input #1, textline
        If Left(textline, 1) = "$" Then 'start = start + 1
        
        'If start = 2 Then
            MyArray = Split(Replace(CStr(textline), " ", ";"), ";")
            For i = 0 To UBound(MyArray)
                Sheets(2).Cells(j, i + 1) = MyArray(i)
            Next i
            j = j + 1
        End If
    Loop
    Close #1
    
Sheets(2).Range("X3:AF3").Copy
Sheets(2).Range("X4:AF" & Sheets(2).Range("A1048576").End(xlUp).Row).PasteSpecial xlPasteFormulas
Sheets(1).Range("S1") = "Table based on ATT File " & Chr(34) & Sheets(1).Range("D1048576").End(xlUp).Value & Chr(34)
 
Application.CalculateFull
    
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
Application.DisplayStatusBar = True
Application.EnableEvents = True
ActiveSheet.DisplayPageBreaks = True
End Sub


That modified code is in the attached revised file.

If however you want to import all text after the first line strting with $, then restore the code for "start" but change the test to read:
       'If start >= 2 Then

Hope this fixes things for you. If so, please remember to mark this Answer as Selected.
Discuss

Discussion

 Did that work for you,  Jaysan? 
John_Ru (rep: 6152) Jul 25, '23 at 1:05 am
Add to Discussion


Answer the Question

You must create an account to use the forum. Create an Account or Login