bsharath
asked on
Macro to query with a txt file and make all words bold
Hi,
I have a txt file which has some name/numbers.I want a macro which can read the txt file search that text in the excel and if found make all the text bold and color to red.
Regards
Sharath
I have a txt file which has some name/numbers.I want a macro which can read the txt file search that text in the excel and if found make all the text bold and color to red.
Regards
Sharath
ASKER
Macro with no errors and no results too.
>Open "C:\mytext.txt" For Input As #FF
In this line you have to put your text file path
In this line you have to put your text file path
ASKER
Put input as "#name"
Am i correct
every line shoiuld have #
Am i correct
every line shoiuld have #
I did not understand you, can you paste the macro u r using and the path where your text file is?
ASKER
This is the macro.
Sub Macro1()
Dim FF As Integer, str1 As String
FF = FreeFile
Open "C:\mytext.txt" For Input As #FF
While EOF(FF)
Line Input #FF, str1
For i = 1 To Sheet1.UsedRange.Rows.Coun t
For j = 1 To Sheet1.UsedRange.Columns.C ount
If Sheet1.Cells(i, j) = str1 Then
Sheet1.Cells(i, j).Font.Bold = True
Sheet1.Cells(i, j).Interior.Color = RGB(255, 0, 0)
End If
Next
Next
Wend
Close FF
End Sub
I have the txt file in C:\filename.txt
in the txt filei have
Sharath
Reddy
ramesh
And have Sharath
Reddy in the excel sheet 1.
Sub Macro1()
Dim FF As Integer, str1 As String
FF = FreeFile
Open "C:\mytext.txt" For Input As #FF
While EOF(FF)
Line Input #FF, str1
For i = 1 To Sheet1.UsedRange.Rows.Coun
For j = 1 To Sheet1.UsedRange.Columns.C
If Sheet1.Cells(i, j) = str1 Then
Sheet1.Cells(i, j).Font.Bold = True
Sheet1.Cells(i, j).Interior.Color = RGB(255, 0, 0)
End If
Next
Next
Wend
Close FF
End Sub
I have the txt file in C:\filename.txt
in the txt filei have
Sharath
Reddy
ramesh
And have Sharath
Reddy in the excel sheet 1.
In that case change your macro to
Sub Macro1()
Dim FF As Integer, str1 As String
FF = FreeFile
Open "C:\filename.txt" For Input As #FF
While Not EOF(FF)
Line Input #FF, str1
For i = 1 To Sheet1.UsedRange.Rows.Coun t
For j = 1 To Sheet1.UsedRange.Columns.C ount
If Sheet1.Cells(i, j) = str1 Then
Sheet1.Cells(i, j).Font.Bold = True
Sheet1.Cells(i, j).Interior.Color = RGB(255, 0, 0)
End If
Next
Next
Wend
Close FF
End Sub
Sub Macro1()
Dim FF As Integer, str1 As String
FF = FreeFile
Open "C:\filename.txt" For Input As #FF
While Not EOF(FF)
Line Input #FF, str1
For i = 1 To Sheet1.UsedRange.Rows.Coun
For j = 1 To Sheet1.UsedRange.Columns.C
If Sheet1.Cells(i, j) = str1 Then
Sheet1.Cells(i, j).Font.Bold = True
Sheet1.Cells(i, j).Interior.Color = RGB(255, 0, 0)
End If
Next
Next
Wend
Close FF
End Sub
Is it working fine now?
ASKER
I just checked.This is searching case sencitive.
I want the script to search even though the case is diffe.
Ex:
Sharath
sharath
SHARATH
SHarath
any thing it has to search
Will it search even though it is in any colum or row?
I want the script to search even though the case is diffe.
Ex:
Sharath
sharath
SHARATH
SHarath
any thing it has to search
Will it search even though it is in any colum or row?
Then use the UCase function as shown
Sub Macro1()
Dim FF As Integer, str1 As String
FF = FreeFile
Open "C:\filename.txt" For Input As #FF
While Not EOF(FF)
Line Input #FF, str1
For i = 1 To Sheet1.UsedRange.Rows.Coun t
For j = 1 To Sheet1.UsedRange.Columns.C ount
If UCase(Sheet1.Cells(i, j)) = UCase(str1) Then
Sheet1.Cells(i, j).Font.Bold = True
Sheet1.Cells(i, j).Interior.Color = RGB(255, 0, 0)
End If
Next
Next
Wend
Close FF
End Sub
Sub Macro1()
Dim FF As Integer, str1 As String
FF = FreeFile
Open "C:\filename.txt" For Input As #FF
While Not EOF(FF)
Line Input #FF, str1
For i = 1 To Sheet1.UsedRange.Rows.Coun
For j = 1 To Sheet1.UsedRange.Columns.C
If UCase(Sheet1.Cells(i, j)) = UCase(str1) Then
Sheet1.Cells(i, j).Font.Bold = True
Sheet1.Cells(i, j).Interior.Color = RGB(255, 0, 0)
End If
Next
Next
Wend
Close FF
End Sub
ASKER
I get this error.
Run-time error '13':
Type mismatch
If the names in the file is sharathreddy or Sharath reddy
and in the excel i just have it as sharath will it search the text?
Run-time error '13':
Type mismatch
If the names in the file is sharathreddy or Sharath reddy
and in the excel i just have it as sharath will it search the text?
The previous macro worked fine in my excel sheet
This is the macro which will search the text even if Sharath is in the excel file, and sharathreddy is in the text file
Sub Macro1()
Dim FF As Integer, str1 As String
FF = FreeFile
Open "C:\filename.txt" For Input As #FF
While Not EOF(FF)
Line Input #FF, str1
For i = 1 To Sheet1.UsedRange.Rows.Coun t
For j = 1 To Sheet1.UsedRange.Columns.C ount
If InStr(1, UCase(str1), UCase(Sheet1.Cells(i, j)), vbTextCompare) > 0 Then
Sheet1.Cells(i, j).Font.Bold = True
Sheet1.Cells(i, j).Interior.Color = RGB(255, 0, 0)
End If
Next
Next
Wend
Close FF
End Sub
This is the macro which will search the text even if Sharath is in the excel file, and sharathreddy is in the text file
Sub Macro1()
Dim FF As Integer, str1 As String
FF = FreeFile
Open "C:\filename.txt" For Input As #FF
While Not EOF(FF)
Line Input #FF, str1
For i = 1 To Sheet1.UsedRange.Rows.Coun
For j = 1 To Sheet1.UsedRange.Columns.C
If InStr(1, UCase(str1), UCase(Sheet1.Cells(i, j)), vbTextCompare) > 0 Then
Sheet1.Cells(i, j).Font.Bold = True
Sheet1.Cells(i, j).Interior.Color = RGB(255, 0, 0)
End If
Next
Next
Wend
Close FF
End Sub
ASKER
Can you send me a sample file
As the sheet name in my file is Rack servers
I changed the macro too. But get a compiling error
As the sheet name in my file is Rack servers
I changed the macro too. But get a compiling error
ASKER
Where should i run this
sample file io will not be able to upload from my office but this macro can be placed in the ThisWorkBook module which you can get after Pressing Alt+F11 in the workbook to open the macro editor and then run this macro.
ASKER
The sheet name is rack servers.
When i run i get no error or output.
I have the name Sharath in the txt file and even in the excel but no results
When i run i get no error or output.
I have the name Sharath in the txt file and even in the excel but no results
can you upload your file?
ASKER
buddy i dont have excel 2007 :(, only excel 2003. Cant open the file.
ASKER
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
And sharath please put such questions in MS Excel Zone only
ASKER
Ok sure hitesh
I get this error.
-------------------------- -
Microsoft Visual Basic
-------------------------- -
Run-time error '424':
Object required
-------------------------- -
OK Help
-------------------------- -
I just put name sharath in the excel sheet and ran the macro i got the error.
I get this error.
--------------------------
Microsoft Visual Basic
--------------------------
Run-time error '424':
Object required
--------------------------
OK Help
--------------------------
I just put name sharath in the excel sheet and ran the macro i got the error.
i put the name sharath in the sheet and did not get any error, in which cell did you put it?
do you know how to debug a macro?
do you know how to debug a macro?
ASKER
In colum 3 "C"
No i dont know to debug :<
No i dont know to debug :<
ASKER
Should change the sheet name.Or the sheet name in the macro?
Yes try changing the sheetname and see the result
Dim FF As Integer, str1 As String
FF = FreeFile
Open "C:\mytext,txt" For Input As #FF
While EOF(FF)
Line Input #FF, str1
For i = 1 To Sheet1.UsedRange.Rows.Coun
For j = 1 To Sheet1.UsedRange.Columns.C
If Sheet1.Cells(i, j) = str1 Then
Sheet1.Cells(i, j).Font.Bold = True
Sheet1.Cells(i, j).Interior.Color = RGB(255, 0, 0)
End If
Next
Next
Wend
Close FF
End Sub