search a text file

taha54

Board Regular
Joined
Aug 11, 2006
Messages
103
hi all,
i was not sure if it is possible in Excel VBA to open a text file and search for a specifc word so lets say i have a 3 pages text file with english word "temp.txt", and i am looking for the words "i am happy" then take these words and store them in any excel file called " temp2.xsl"


let me know if we can do that, and if not what do you suggest

thanks
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
Just to confirm,

The text that you copy from the text file and paste into the code is all caps? The search text must be in all caps since my code is searching by all capital letters.

The two places in the code where it specifies the path and filename match your actual path and filename?

This code is pretty simple and there must be something obvious to cause it not to work.

Let me know if you have checked the items above.

Thanks,

David
 
Upvote 0
in the txt file, everything is in small cabs , and thats what i have in the VBA code, i changed them from big to small , could that be the problem, because both path places are correct
 
Upvote 0
taha54

Try stepping through the code using F8.

David

As far as I can see your code actually takes care of the lower/upper case issue by using UCase.

ps taha54, can you post the exact code you are trying?
 
Upvote 0
hi Norie, here is the exact txt file, very simple, i am trying it as a sample only for other big fles in the future

"i am glad that you are one of my people ,
but i am happy that faisal taha is one of you"

and i want to make sure that this file has "i am happy"

and if yes then put it in A(1)
 
Upvote 0
taha54

Can you post the exact code, not the text?:)

You might have changed something in it to cause it not to work.

ps Dave's code works perfectly fine for me.:o
 
Upvote 0
sorry Norie :)

Code:
Public Sub TestIt()
[A1].Select     'Start in cell A1
If Len(Dir("C:\Documents and Settings\Administrator\Desktop\faisal.txt")) > 0 Then    'replace text inside quotes with your path and filename
    Open "C:\Documents and Settings\Administrator\Desktop\faisal.txt" For Input As #1
    While Not EOF(1)
        Line Input #1, Hold$
        
        If InStr(UCase$(Hold$), "i am happy") Then
            ActiveCell.Value = Hold$
            ActiveCell.Offset(1, 0).Select
        End If
    Wend
    Close #1
Else
    MsgBox "Cannot find file."
End If

End Sub

thanks dave
 
Upvote 0
The text in the following line must be typed in all caps...

If InStr(UCase$(Hold$), "i am happy") Then

Should be...

If InStr(UCase$(Hold$), "I AM HAPPY") Then

Please try this and post back.



Norie,
Could you take a look at my other post about Workbooks.open?

Thanks,

David
 
Upvote 0
it worked for the whole line....its good enough

Norie is it okay if i ask another question or will you get mad again :wink:
 
Upvote 0
If you do not want to whole line, I could modify the code to only list what you want. Can you tell me what you want or are you happy with it the way it is?

Thanks,

David
 
Upvote 0

Forum statistics

Threads
1,225,335
Messages
6,184,335
Members
453,227
Latest member
Slainte

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