Not sure about this.

Try this macro. You could assign this macro on a button as well for easy 
use.

Sub kTest()
    
    Dim ka, k(), i  As Long, n As Long, j   As Long, c As Long
    Dim WhichCol    As String, Cols(), x, SearchKey As String
    
    Const SearchCell    As String = "E3" '<<== adjust to suit
    
    WhichCol = Application.InputBox("Enter the Column Name to Search", 
"Search Col", "For e.g. All or A or B or A,B", Type:=2)
    
    If WhichCol = "False" Then Exit Sub
    
    SearchKey = Trim(Range(CStr(SearchCell)))
    
    If Len(SearchKey) = 0 Then
        MsgBox "Search text not found", vbInformation
        Exit Sub
    End If
    
    With Sheets("database")
        ka = Intersect(.UsedRange, .Range("a:g"))
    End With
    
    If InStr(1, WhichCol, ",") Then
        x = Split(WhichCol, ",")
        For i = 0 To UBound(x)
            ReDim Preserve Cols(0 To i)
            Cols(i) = Cells(1, x(i)).Column
        Next
    ElseIf LCase$(WhichCol) = "all" Then
        For i = 1 To UBound(ka, 2)
            ReDim Preserve Cols(0 To i - 1)
            Cols(i - 1) = i
        Next
    Else
        ReDim Preserve Cols(0 To 0)
        Cols(0) = Cells(1, WhichCol).Column
    End If
    
    ReDim k(1 To UBound(ka, 1), 1 To UBound(ka, 2))
    
    For i = 2 To UBound(ka, 1)
        If Len(ka(i, 1)) Then
            For j = LBound(Cols) To UBound(Cols)
                If InStr(1, ka(i, Cols(j)), SearchKey, 1) Then
                    n = n + 1
                    For c = 1 To UBound(ka, 2)
                        k(n, c) = ka(i, c)
                    Next
                    Exit For
                End If
            Next
        End If
    Next
    
    If n Then
        [c8].Resize(i, c - 1).ClearContents
        [c8].Resize(n, UBound(k, 2)) = k
    Else
        MsgBox "No record(s) found", vbInformation
        Exit Sub
    End If
    
End Sub

Kris

-- 
FORUM RULES (934+ members already BANNED for violation)

1) Use concise, accurate thread titles. Poor thread titles, like Please Help, 
Urgent, Need Help, Formula Problem, Code Problem, and Need Advice will not get 
quick attention or may not be answered.

2) Don't post a question in the thread of another member.

3) Don't post questions regarding breaking or bypassing any security measure.

4) Acknowledge the responses you receive, good or bad.

5)  Cross-promotion of, or links to, forums competitive to this forum in 
signatures are prohibited. 

NOTE  : Don't ever post personal or confidential data in a workbook. Forum 
owners and members are not responsible for any loss.

------------------------------------------------------------------------------------------------------
To post to this group, send email to excel-macros@googlegroups.com

Reply via email to