Hi all
I have a spreadsheet with a 'Search' area. The User enters a search phrase or word and the code loops through the spreadsheet and returns any rows that contain the expression or word into a new sheet.
This all works fine. I have now been asked if the search phrase or word can be highlighted (bold) in the new spreadsheet. I have tried to do this, but end up with the row as bold rather then the word.
Can anyone help?
Here is my code:
I have a spreadsheet with a 'Search' area. The User enters a search phrase or word and the code loops through the spreadsheet and returns any rows that contain the expression or word into a new sheet.
This all works fine. I have now been asked if the search phrase or word can be highlighted (bold) in the new spreadsheet. I have tried to do this, but end up with the row as bold rather then the word.
Can anyone help?
Here is my code:
Code:
'Private Sub CommandButton1_Click()
Option Compare Text
Sub Button5_Click()
Application.ScreenUpdating = False
Dim SearchRow As Integer
Dim CopyRow As Integer
Dim CopySheet As String
Dim SearchText As String
Dim SearchText2 As String
Dim Col As String
Dim Col2 As String
Dim Row As Integer
Dim SearchPage As String
On Error GoTo Err_Execute
'Set Search Page to copy from
SearchPage = "Summary"
'copy data to row 4 in Sheet2
CopyRow = 4
'Set start Row for Search Boxes (on search page)
Row = 3
'Set Column Number to search in
'jub removed oct06 - Col = Range("H" & CStr(Row)).Value
Col = "A"
'Set Column Number 2 to search in
'jub removed oct06 - Col2 = Range("H" & CStr(Row + 1)).Value
Col2 = "C"
'Set SearchText for Col
'jub changed here
SearchText = Range("D" & CStr(Row)).Value
'Set SearchText for Col2
SearchText = Range("D" & CStr(Row)).Value
'Start search in row of the sheet to copy from
SearchRow = 2
'Copy data to Sheet Number
CopySheet = "Navigator"
'MsgBox "All data on " & CopySheet & " will be deleted first" (MDI Commented)
Sheets(CopySheet).Rows.Delete
Sheets(SearchPage).Select
Rows(CStr(1) & ":" & CStr(1)).Select
Selection.Copy
Sheets(CopySheet).Select
Sheets(CopySheet).Rows(CStr(1) & ":" & CStr(1)).Select
ActiveSheet.Paste
Sheets(SearchPage).Select
While Len(Range(Col & CStr(SearchRow)).Value) > 0
If InStr((Range(Col & CStr(SearchRow)).Value), SearchText) > 0 Or _
InStr((Range(Col2 & CStr(SearchRow)).Value), SearchText) > 0 Then
'If ((Range(Col & CStr(SearchRow)).Value Like (SearchText) Or _
'Range(Col & CStr(SearchRow)).Value = (SearchText))) Or _
'((Range(Col2 & CStr(SearchRow)).Value Like (SearchText) Or _
'Range(Col2 & CStr(SearchRow)).Value = (SearchText))) Then
'Select row to copy (from Sheet1)
Rows(CStr(SearchRow) & ":" & CStr(SearchRow)).Select
Selection.Copy
'Paste row into Sheet2
Sheets(CopySheet).Select
Sheets(CopySheet).Rows(CStr(CopyRow) & ":" & CStr(CopyRow)).Select
ActiveSheet.Paste
'Move to next row
CopyRow = CopyRow + 1
'Go back to Sheet1 to continue searching
Sheets(SearchPage).Select
'End If
Else
If Not IsNull(Col2) Then
If InStr((Range(Col & CStr(SearchRow)).Value), SearchText) > 0 Or _
InStr((Range(Col2 & CStr(SearchRow)).Value), SearchText) > 0 Then
'If ((Range(Col & CStr(SearchRow)).Value Like (SearchText) Or _
' Range(Col & CStr(SearchRow)).Value = (SearchText)) And _
' (Range(Col2 & CStr(SearchRow)).Value Like (SearchText2) Or _
' Range(Col2 & CStr(SearchRow)).Value = (SearchText2))) Then
'Select row to copy (from Sheet1)
Rows(CStr(SearchRow) & ":" & CStr(SearchRow)).Select
Selection.Copy
'Paste row into Sheet2
Sheets(CopySheet).Select
Sheets(CopySheet).Rows(CStr(CopyRow) & ":" & CStr(CopyRow)).Select
ActiveSheet.Paste
'Move to next row
CopyRow = CopyRow + 1
'Go back to Sheet1 to continue searching
Sheets(SearchPage).Select
End If
End If
End If
SearchRow = SearchRow + 1
Wend
'Position on cell A3
Application.CutCopyMode = False
'Range("A3").Select
'MsgBox "All matching data has been copied to & (CopySheet)" (MDI Commented)
Sheets(CopySheet).Select
ActiveSheet.UsedRange.Columns.AutoFit
Columns("A:A").ColumnWidth = 25
Columns("B:B").ColumnWidth = 12
Columns("C:C").EntireColumn.AutoFit
Columns("D:D").ColumnWidth = 22
Columns("E:E").ColumnWidth = 12
Columns("F:F").ColumnWidth = 9
Columns("G:G").ColumnWidth = 12
Columns("H:H").ColumnWidth = 18
Cells.Select
Cells.EntireRow.AutoFit
Rows("2:3").Select
Selection.Delete Shift:=xlUp
Range("C3").Select
Range("A1:H1000").Sort Key1:=Range("C3"), Order1:=xlAscending, Header:= _
xlGuess, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal
Range("A3").Select
Range("A1:H1000").Sort Key1:=Range("A3"), Order1:=xlAscending, Header:= _
xlGuess, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal
Range("A1").Select
Exit Sub
Err_Execute:
MsgBox "An error occurred..."
End Sub
Sub NewSearch()
Sheets("Search").Select
End Sub