• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

VBA assistance - Changing search from all spreadsheets to just one

jh

New Member
Greetings,


I'm working with a spreadsheet that has embedded VBA and it works great if it's searching all spreadsheets in the workbook, unfortunately only need to search one sheet titled "DATA" and receiving errors because I have objects in other sheets. I've tried playing around the VBA but I'm a novice. I'm sure this is a 30 second fix, and would be grateful to learn the answer.


Below is the untouched VBA. There are two macros that I can include as well if needed, but I believe the issue is in the VBA itself.


Thank you and eagerly look forward to your reply.


Signed,

John


-Code below -


Option Explicit


Private Sub btnClear_Click()

cleanCells

ActiveSheet.txtBox1.Activate

End Sub


Private Sub btnSearch_Click()

Dim wb As Workbook

Dim ws As Worksheet

Dim startPos As Integer

Dim searchWord As String


On Error GoTo errorHandler


searchWord = Trim(txtBox1.Text)

If searchWord = "" Or Len(searchWord) < 3 Then

MsgBox ("Please enter at least 3 characters and try again.")

Exit Sub

End If


' Clean up the previous result

cleanCells


Set wb = ActiveWorkbook


startPos = 7

For Each ws In wb.Worksheets

If LCase(ws.Name) <> "search" Then ' Skip the search sheet

startPos = search_data(searchWord, wb, ws, startPos)

End If

Next


' Back to the search page

wb.Worksheets(1).Select

ActiveSheet.txtBox1.Activate


MsgBox ("Done")


Exit Sub


errorHandler:

MsgBox (Err.Number & ": " & Err.Description)


End Sub


Private Function search_data(searchWord As String, wb As Workbook, ws As Worksheet, counter) As Integer

Dim cell As Range

Dim ws1 As Worksheet

Dim firstAddress As String ' So the search does not repeat from the top

Dim currentRow As Long

Dim visited(150000), arrayPos As Long

Dim i As Long

Dim alreadyVisited, hit As Boolean


On Error GoTo errorHandler


Set ws1 = wb.Worksheets(1)

arrayPos = 0

hit = False


With ws.Range("A:A,C:C,D:D")

Set cell = .Find(what:=Trim(searchWord), LookIn:=xlValues, Lookat:=xlPart, _

MatchCase:=False, searchorder:=xlByColumns)

If Not cell Is Nothing Then

' --- HIT ---

hit = True

firstAddress = cell.Address

currentRow = cell.Row

' ------------------------------------------

' Type the sheet name

' ------------------------------------------

ws1.Select

Range("a" & counter).Select

ActiveCell.FormulaR1C1 = ws.Name

Selection.Font.Bold = True

With Selection

.HorizontalAlignment = xlGeneral

.VerticalAlignment = xlTop

.WrapText = False

.Orientation = 0

.AddIndent = False

.IndentLevel = 0

.ShrinkToFit = False

.ReadingOrder = xlContext

.MergeCells = False

End With

With Selection

.HorizontalAlignment = xlLeft

.VerticalAlignment = xlTop

.WrapText = False

.Orientation = 0

.AddIndent = False

.IndentLevel = 0

.ShrinkToFit = False

.ReadingOrder = xlContext

.MergeCells = False

End With

With Selection.Font

.Color = -16776961

.TintAndShade = 0

End With

With Selection.Interior

.Pattern = xlNone

.TintAndShade = 0

.PatternTintAndShade = 0

End With

counter = counter + 1


' ------------------------------------------

' Copy the header ... first row of a sheet

' ------------------------------------------

ws.Select

ws.Rows("1:1").Select

Selection.Copy

ws1.Select

Range("A" & counter).Select

ActiveSheet.Paste

Application.CutCopyMode = False


' Change the background color

ws1.Rows(counter & ":" & counter).Select

With Selection.Interior

.Pattern = xlSolid

.PatternColorIndex = xlAutomatic

.ThemeColor = xlThemeColorLight2

.TintAndShade = 0.799981688894314

.PatternTintAndShade = 0

End With


counter = counter + 1


' ------------------------------------------

' Insert search hits

' ------------------------------------------

Do

alreadyVisited = False

' Find if the row is already copied

For i = 0 To UBound(visited) - 1

If visited(i) = "" Then

Exit For

Else

If currentRow = visited(i) Then

alreadyVisited = True

Exit For

End If

End If

Next i


If alreadyVisited = False Then

visited(arrayPos) = currentRow

arrayPos = arrayPos + 1

ws.Select

ws.Rows(cell.Row & ":" & cell.Row).Select

Selection.Copy

ws1.Select

Range("A" & counter).Select

ActiveSheet.Paste

Application.CutCopyMode = False


counter = counter + 1

End If


Set cell = .FindNext(cell)

currentRow = cell.Row

Loop While Not cell Is Nothing And cell.Address <> firstAddress


End If

End With
>
' ------------------------------------------

ws.Select

ws.Range("A1").Select ' scroll the page to the top


If hit = True Then

search_data = counter + 1 ' return the current row number to insert with one row space

Else

search_data = counter

End If


Exit Function


errorHandler:

MsgBox (Err.Number & ": " & Err.Description)

search_data = counter + 1


End Function


Private Sub cleanCells()

Dim wb As Workbook


On Error GoTo errorHandler


Set wb = ActiveWorkbook

wb.Worksheets(1).Select

Range("A7").Select

Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select

Application.CutCopyMode = False

Selection.ClearContents

With Selection.Interior

.Pattern = xlNone

.TintAndShade = 0

.PatternTintAndShade = 0

End With

Selection.Borders(xlDiagonalDown).LineStyle = xlNone

Selection.Borders(xlDiagonalUp).LineStyle = xlNone

Selection.Borders(xlEdgeLeft).LineStyle = xlNone

Selection.Borders(xlEdgeTop).LineStyle = xlNone

Selection.Borders(xlEdgeBottom).LineStyle = xlNone

Selection.Borders(xlEdgeRight).LineStyle = xlNone

Selection.Borders(xlInsideVertical).LineStyle = xlNone

Selection.Borders(xlInsideHorizontal).LineStyle = xlNone


Exit Sub


errorHandler:

MsgBox (Err.Number & ": " & Err.Description)

End Sub


Private Sub txtBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)

If KeyCode = 13 Then

btnSearch_Click

End If

End Sub
 
HI, jh!


Once again...

http://chandoo.org/forums/topic/filter-and-highlight#post-71133


This time Redmond guys are not guilty, but I still feels like the little monkey...


So not having read and tested the looong code provided without a sample file, I'd try changing this piece of code from:

-----

[pre]
Code:
startPos = 7
For Each ws In wb.Worksheets
If LCase(ws.Name) <> "search" Then ' Skip the search sheet
startPos = search_data(searchWord, wb, ws, startPos)
End If
Next
-----

to:

-----

startPos = 7
For Each ws In wb.Worksheets
If LCase(ws.Name) <> "Data" Then ' Skip the search sheet
startPos = search_data(searchWord, wb, ws, startPos)
End If
Next
[/pre]
-----


It isn't the cleaner solution but the less intrusive.


Regards!
 
Hi ,


I have not gone through in enough detail to be sure , but I think only one statement needs to be modified :

[pre]
Code:
For Each ws In wb.Worksheets
If LCase(ws.Name) <> "search" Then ' Skip the search sheet
startPos = search_data(searchWord, wb, ws, startPos)
End If
Next
The above segment is skipping the sheet whose name is search or SEARCH ; all other sheets will be included in the search. If you modify this to :

If LCase(ws.Name) = "data" Then ' Skip all sheets other than the DATA sheet
startPos = search_data(searchWord, wb, ws, startPos)
End If
[/pre]
With this change , only the sheet whose name is data or DATA will be searched ; all other sheets will be excluded from the search.


Narayan
 
Greetings,


I apologize, now and in the future I will put together a sample and share for review. I tried that quick fix that was mentioned (That would have been great if that was it. Unfortunately didn't resolve my error)


I'll have one up for review as quick as I can.


Thank you again.


Signed,

John
 
Hi, jh!

Strange thing that it didn't worked, as both NARANYANK991 and me suggested the same modificacion... maybe we're losing that fine touch?

Consider uploading a sample file (including manual examples of desired output), it'd be very useful for those who read this and might be able to help you. Thank you.

Give a look at the green sticky posts at this forums main page for uploading guidelines.

Regards!
 
Back
Top