write to next 27 lines

leena24

Board Regular
Joined
Apr 21, 2007
Messages
63
Hi !

I am writing a macro , which can help me write next 27 lines once a condition is fulfilled.

for e.g.

"If Trim((Mid(data, 12, 5))) = chk1 Then "

so what i need is that if data = 'chk1' , then extract subsequent 27 lines from chk1 in a different text file.

Any Ideas ?
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Hi
Your message is not clear. Assuming that you are looking for chk1 as a part of string in col A , paste the following codes in the macro window (Alt f11)
Code:
Sub ggg()
x = Cells(Rows.Count, 1).End(xlUp).Row
For a = 1 To x
b = Cells(a, 1)
If InStr(b, "chk1") > 0 Then
c = a & ":" & a + 27
Rows(c).Copy
Cells(x + 3, 1).PasteSpecial
Exit For
End If
Next a
End Sub
Run the macro. It will copy 27 rows from where it finds chk1 and pastes it 3 rows below last row of data.
Ravi
 
Upvote 0
Hi
i am reading it from a text file. and i have to extract the lines into another text file.
thanks
 
Upvote 0
try
Code:
Sub test()
Dim myDir As string, fn As String, fnNew As String, chk1 As String
Dim txt As String, temp As String, ff As Integer, n As Long, flg As Boolean
chk1 = "blahblah"
myDir = "C\:test"
fn = "\test1.txt"
ff = freeFile
Open myDir & fn For Input As #ff
Do Until EOF(ff)
     Line Input #ff, txt
     If Trim(txt) = chk1 Then
          msg = txt
          Do Until EOF(ff)
               Line Input #ff, txt
               n = n + 1
               If n < 27 Then
                    temp = temp & vbCrLf & txt
               Else
                    flg = True : Exit Do
               End If
           Loop
     End If
     If flg = True Then Exit Do
Loop
Close #ff
If Len(temp) = 0 Then Exit Sub
ff = FreeFile
fnNew = "\test2.txt"
Open myDir & fnNew For Output As #ff
     Print #ff, temp
Close #ff
End Sub
 
Upvote 0
Hi Jindon

How have you been ?

running perfectly but for two issues:-
'it is not extracting the line where it found chk1 .
'chk1 can have multiple values so it has to read the entire file till end and take out data from Mypos.

chk1 = Trim(Mid(data, 12, 4))
Mypos = InStr(chk1, "001") Xor InStr(chk1, "002") Xor InStr(chk1, "400") Xor InStr(chk1, "160")

If Mypos > 0 Then

( I have added your code after this )

*** also like to mention that , the text file from where the data is being read is really huge like 600 mb .

Thanks again ;)
 
Upvote 0
Hi
I'm very well, thanks. Hope you are well, too.
chk1 = Trim(Mid(txt,12,4)) means length of 4, while all your criteria have 3 length.
Not sure if this is alrgith or not.
Code:
Sub test()
Dim myDir As string, fn As String, fnNew As String, chk1 As String
Dim txt As String, temp As String, ff As Integer, n As Long
myDir = "C\:test"
fn = "\test1.txt"
ff = freeFile
Open myDir & fn For Input As #ff
Do Until EOF(ff)
     Line Input #ff, txt
     chk1 = Trim(Mid$(txt,12,4))
     Select Case chk1
          Case "001", "002", "400", "160"
               temp = txt
               Do Until EOF(ff)
                    Line Input #ff, txt
                    n = n + 1
                    If n < 27 Then
                         temp = temp & vbCrLf & txt
                    Else
                         Exit Do
                    End If
                Loop
     End Select
Loop
Close #ff
If Len(temp) = 0 Then Exit Sub
ff = FreeFile
fnNew = "\test2.txt"
Open myDir & fnNew For Output As #ff
     Print #ff, temp
Close #ff
End Sub
 
Upvote 0
Thanks Jindon! It works as in, it is searching all the values in chk1 till the end of the file , however it is writing only the last line of the input file in the output file.

Thanks
 
Upvote 0
Right, try this one
Code:
Sub test()
Dim myDir As string, fn As String, fnNew As String, chk1 As String
Dim txt As String, temp As String, ff As Integer, n As Long
myDir = "C\:test"
fn = "\test1.txt"
ff = freeFile
Open myDir & fn For Input As #ff
Do Until EOF(ff)
     Line Input #ff, txt
     chk1 = Trim(Mid$(txt,12,4))
     If n = 0 Then
          Select Case chk1
               Case "001", "002", "400", "160"
                    temp = txt
                    n = n + 1
          End Select
     Else
          temp = temp & vbCrLf & txt
          n = n + 1
          If n > 27 Then n = 0
     End If
Loop
Close #ff
If Len(temp) = 0 Then Exit Sub
ff = FreeFile
fnNew = "\test2.txt"
Open myDir & fnNew For Output As #ff
     Print #ff, temp
Close #ff
End Sub
 
Upvote 0
ok ...now it is copying only the last 27 lines after the last instance of chk1 . It has to copy all 27 lines after each chk1 instance.
 
Upvote 0
Arrrr

Can you just change
Code:
temp = txt
to
Code:
temp = temp & vbCrLf & txt
also
Code:
Print #ff, temp
to
Code:
Print #ff, Mid$(temp,2)
 
Upvote 0

Forum statistics

Threads
1,214,585
Messages
6,120,399
Members
448,958
Latest member
Hat4Life

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