2

I do not know if it is possible to implement this problem in VBA or it must be done with VB. Net using Visual Studio.

Problem: Excel has its search function and it is a pain if there many value available or you must find a value that far away from column A.

enter image description here

I would like to have something like this

enter image description here

In which I can specify what columns I want to display by their header name. Rearrange the column in the way I would like to, and ability to copy and paste. Like datagrid in Visual basic? Is it possible?

2
  • 1
    It can be done in both VBA and VB.Net. Which one do you want? Commented May 4, 2012 at 19:57
  • If it can be done in VB. Net as a tab (addin) i47.tinypic.com/tsoj.png - it is the best Commented May 4, 2012 at 20:07

1 Answer 1

4

I have given below both the methods - VBA and VB.net (take your pick) :)

USING VB.NET

Place a DataGridView on your VB.net Form and also place a Button. Place this code in the Button

Public Class Form1
    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Dim masterTable As New DataTable

        Dim cnnStr As String = "Provider=Microsoft.ACE.OLEDB.12.0;Extended Properties=""Excel 12.0 Xml;HDR=NO"";Data Source=""{0}"";"

        Using da As New OleDb.OleDbDataAdapter("select * from [Sheet1$] Where F1 = 'Test1'", String.Format(cnnStr, "C:\Book1.xlsx"))
            da.Fill (masterTable)
        End Using
        DataGridView1.DataSource = masterTable
    End Sub
End Class

And you are done :)

SNAPSHOT

I am using limited Data.

enter image description here

You can also change your string "select * from [Sheet1$] Where F1 = 'Test1'" to "select F1 as Name,F2 as PN, F3 as [Inventory Loc] from [Sheet1$] Where F1 = 'Test1'" to display the headers as below

enter image description here

EDIT

In case you are wondering how to do it in VBA

USING VBA

Place a Listbox and a Command Button on a Form and then use this code.

Option Explicit

Private Sub CommandButton1_Click()
    Dim ws As Worksheet, ws1 As Worksheet
    Dim rng As Range
    Dim lastRow As Long
    Dim Ar As Variant

    Set ws = Sheets("Sheet1")

    lastRow = ws.Cells.Find(What:="*", After:=ws.Range("A1"), _
              Lookat:=xlPart, LookIn:=xlFormulas, _
              SearchOrder:=xlByRows, SearchDirection:=xlPrevious, _
              MatchCase:=False).Row

    Set rng = ws.Range("A1:C" & lastRow)

    Set ws1 = Sheets.Add

    With rng
        ws.AutoFilterMode = False
        .AutoFilter Field:=1, Criteria1:="Test1"
        .SpecialCells(xlCellTypeVisible).Copy ws1.Range("A1")
        ws.AutoFilterMode = False

        lastRow = ws1.Cells.Find(What:="*", After:=ws1.Range("A1"), _
                  Lookat:=xlPart, LookIn:=xlFormulas, SearchOrder:=xlByRows, _
                  SearchDirection:=xlPrevious, MatchCase:=False).Row

        Ar = ws1.Range("A1:C" & lastRow)

        Application.DisplayAlerts = False
        ws1.Delete
        Application.DisplayAlerts = True
    End With

    With Me.ListBox1
        .Clear
        .ColumnHeads = False
        .ColumnCount = 3
        .List = Ar
        .ColumnWidths = "50;50;50"
        .TopIndex = 0
    End With
End Sub

SNAPSHOT

enter image description here

MORE FOLLOWUP

Hi Siddharth, thank you very much for both code. For VB. Net it is wonderful. For the VBA in Exel, is there any way that I can copy (using Ctrl + C) to copy the data - single row will be fine, although copy multiple rows is more desirable. I am able to replace "Test1" with textbox i49.tinypic.com/2ceq3yf.jpg – user1370854 5 hours ago

Yes it is possible to copy the single selected or multiple selected items from the listbox to cliboard. To make the listobx multiselect, in the design mode, set the property of the listbox to fmMultiSelectMulti1. Next Add a command button and paste this code.

This code is again based on the data I use above so amend it as applicable. When you press the Copy button, the data is copied to the clipboard and then you can simply use CTL V to paste the data where ever you want; for example in Notepad.

Private Sub CommandButton2_Click()
    Dim MyData As DataObject
    Dim i As Long
    Dim strCopiedText As String

    Set MyData = New DataObject

    With Me.ListBox1
        For i = 1 To .ListCount
            If .Selected(i - 1) Then
                strCopiedText = strCopiedText & _
                                .List(i - 1, 0) & vbTab & _
                                .List(i - 1, 1) & vbTab & _
                                .List(i - 1, 2) & vbCrLf
            End If
        Next i

        If Len(strCopiedText) > 0 Then
            MyData.Clear
            MyData.SetText strCopiedText
            MyData.PutInClipboard
            MsgBox "Data copied to clipboard"
        End If
    End With
End Sub

enter image description here

Sign up to request clarification or add additional context in comments.

12 Comments

@user1370854: Updated the post above :)
Clue: .AutoFilter Field:=1, Criteria1:="Test1" ;)
@user1370854: If you want to search for say PN5 then Field becomes 2 and Criteria1 becomes PN5. Field is the column number where you want to set the criteria :)
Do you want to search for a text in the filter values in Userform?
Is this what you are trying? Criteria1:="=*Test*"
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.