0

I have an excel sheet detailing a list of equipment (about 12000 rows in the sheet). I want to make a dashboard whereby I enter the name of the equipment and it returns the Manufacturer, Date of manufacture and description.

What would be the best way to go about this? I was thinking of writing VBA code in order to match an input to an object type and return the required value however I have not coded in VBA and I'm unsure how to type it out.

BruceWayne
  • 22,923
  • 15
  • 65
  • 110
  • 1
    How's the data laid out? Can you post a sample table? (Actual copy/paste is preferable to screenshot, so we can drop the data in Excel ourselves). Either `Vlookup()` or `Index/Match` should do the trick. – BruceWayne Jul 08 '16 at 18:21
  • @BruceWayne I'm not entirely comfortable putting exact data here because it's company info but I'll lay it out for you: The row headings include the equipment, description, etc. I considered Vlookup however, we have multiple equipment with the same name but different descriptions and dates and Vlookup only gives out 1 result. – Kaustubh Vongole Jul 08 '16 at 18:27
  • 1
    Oh, yeah I don't need real data. If you have company names, employee names, and salaries, just make stuff up (I do it all the time). You can do like "Kaustubh1 Company", "John Doe", and then a formula like `=Randbetween(10000,100000)` for a salary...And it sounds like you can definitely do `Index/Match`, as an array formula. If you could post some mock data, I'd appreciate it! – BruceWayne Jul 08 '16 at 18:38
  • http://eimagine.com/how-to-return-multiple-match-values-in-excel-using-index-match-or-vlookup/ – Tim Williams Jul 08 '16 at 19:11

3 Answers3

1

Forgive me in suggesting a different approach but consider the scalable, relational advantage of SQL (Structured Query Language) that can take the Equipment Name as parameter and query your data into a filtered table resultset. If using Excel for PC, Excel can run SQL using the Jet/ACE SQL Engine (Windows .dll files), the very engine that powers its sibling, MS Access. This approach avoids array formulas, loops, if/then logic, complex multiple index/matching, and vlookups.

Below example prompts user for the Equipment Name using an InputBox which is then passed as a parameter to the WHERE clause of SQL query. We hate for a malicious user to run SQL injection in input box, something like: 1; DELETE FROM [DATA];. Example assumes data exists in a tab called DATA with column headers in first row and an empty tab called RESULTS. Adjustments can be made.

Sub RunSQL()    
    Dim conn As Object, rst As Object, cmd As Object
    Dim equipmentVar As String, strConnection As String, strSQL As String
    Dim i As Integer
    Const adcmdText = 1, adVarChar = 200, adParamInput = 1

    Set conn = CreateObject("ADODB.Connection")
    Set rst = CreateObject("ADODB.Recordset")

    ' RECEIVE USER INPUT
    equipmentVar = InputBox("Enter name of equipment.", "EQUIPMENT SEARCH")
    If equipmentVar = "" Then Exit Sub

    ' CONNECTION STRINGS (TWO VERSIONS)
'    strConnection = "DRIVER={Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)};" _
'                      & "DBQ=C:\Path\To\Workbook.xlsm;"
    strConnection = "Provider=Microsoft.ACE.OLEDB.12.0;" _
                       & "Data Source='C:\Path\To\Workbook.xlsm';" _
                       & "Extended Properties=""Excel 8.0;HDR=YES;"";"

    strSQL = " SELECT [DATA$].Manufacturer, [DATA$].Equipment, " _
                & "   [DATA$].[Date of Manufacturer], [DATA$].[Description] " _
                & " FROM [DATA$]" _
                & " WHERE [DATA$].[Equipment] = ?;"

    ' OPEN DB CONNECTION
    conn.Open strConnection

    ' SET CMD COMMAND
    Set cmd = CreateObject("ADODB.Command")

    With cmd
        .ActiveConnection = conn
        .CommandText = strSQL
        .CommandType = adcmdText
        .CommandTimeout = 15
    End With

    ' BINDING PARAMETER
    cmd.Parameters.Append cmd.CreateParameter("equipParam", adVarChar, adParamInput, 255)
    cmd.Parameters(0).Value = equipmentVar

    ' EXECUTING TO RECORDSET      
    Set rst = cmd.Execute

    ' COLUMN HEADERS
    For i = 1 To rst.Fields.Count
        Worksheets("RESULTS").Cells(1, i) = rst.Fields(i - 1).Name
    Next i

    ' DATA ROWS
    Worksheets("RESULTS").Range("A2").CopyFromRecordset rst

    rst.Close: conn.Close    
    Set rst = Nothing: Set cmd = Nothing: Set conn = Nothing    
End Sub
Community
  • 1
  • 1
Parfait
  • 104,375
  • 17
  • 94
  • 125
0

Sounds like a simple loop through the cells looking for a match, because you have not given the layout I can't tell you the exact code but you are asking for an approach so here it goes.

1) If you want it to work when you enter a value in the cell then you use worksheet_Change i.e. Options Explicit

Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Address = "$A$1" Then
        ' Your code here
    End If
End Sub

The quickest way to code a loop through your 12000 rows but could be any length in future (assuming they start at row 5)

Dim cell As Range

For Each cell In Range("a5", Range("a" & Cells.Rows.Count).End(xlUp))
   'Your if statement to determin if it is a match goes here
   'Your copy code goes here
Next cell

2)You then put in the If statement to check for a match and do any copy display you want when you find one.

Good luck

AndrewT
  • 468
  • 1
  • 8
  • 23
0

Use the filter feature / button in the Ribbon.

Ronnie Royston
  • 16,778
  • 6
  • 77
  • 91