Finding values in multiple rows or columns, then returning the first value in the row or column

seanbrown01

New Member
Joined
Feb 28, 2020
Messages
2
Office Version
  1. 2016
Platform
  1. Windows
I spent a day reading Excel Help articles and trying many different approaches to solving this, but have not been successful. If anyone can help me with the proper formula to make this happen, I would be most grateful. The goal is to identify which physical host (Hostname) a vm computer (vmname) resides on. Example images attached... I have a sizable spreadsheet that is not in a format that is "vlookup friendly". I've also tried to find a way to quickly change the data structure, to make it vlookup friendly, without success. Appreciate any help that can be provided. Thanks!
 

Attachments

  • Snag_24b6f85a.png
    Snag_24b6f85a.png
    19.1 KB · Views: 16
  • Snag_24b70f7c.png
    Snag_24b70f7c.png
    39.8 KB · Views: 17
  • Snag_24b7247b.png
    Snag_24b7247b.png
    42.2 KB · Views: 16

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
A VBA rather than formula approach is:

VBA Code:
Sub subVmnameToHostname()
    Dim dic As Object
    Set dic = CreateObject("Scripting.Dictionary")
    Dim rng As Range
    Set rng = Sheet2.UsedRange
    Dim r As Integer, c As Integer, i As String
    For r = 1 To rng.Rows.Count
        i = Sheet2.Cells(r, 1)
        For c = 2 To rng.Columns.Count
            If Len(Sheet2.Cells(r, c)) > 0 Then
                dic(Sheet2.Cells(r, c)) = i
            Else
                Exit For
            End If
        Next c
    Next r
    Sheet1.Activate
    Cells(1, 1) = "Vmname": Cells(1, 2) = "Hostname"
    Dim k
    c = 2
    For Each k In dic.keys
        Debug.Print k, dic(k)
        Cells(c, 1) = k: Cells(c, 2) = dic(k)
        c = c + 1
    Next
End Sub

My source data (NB: Sheet2):
Book1
ABCDEFG
1Hostname001vmname001vmname002vmname003
2Hostname002vmname004
3Hostname003vmname005vmname006vmname007vmname008vmname009vmname010
Sheet2


Output to (a blank) Sheet1:
Book1
AB
1VmnameHostname
2vmname001Hostname001
3vmname002Hostname001
4vmname003Hostname001
5vmname004Hostname002
6vmname005Hostname003
7vmname006Hostname003
8vmname007Hostname003
9vmname008Hostname003
10vmname009Hostname003
11vmname010Hostname003
Sheet1
 
Upvote 0
with Power Query
HOST123456
Hostname001vmname001vmname002vmname003
Hostname002vmname004
Hostname003vmname005vmname006vmname007vmname008vmname009vmname010
HOSTVM
Hostname001vmname001
Hostname001vmname002
Hostname001vmname003
Hostname002vmname004
Hostname003vmname005
Hostname003vmname006
Hostname003vmname007
Hostname003vmname008
Hostname003vmname009
Hostname003vmname010

Code:
// Table1
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    Result = Table.SelectColumns(Table.UnpivotOtherColumns(Source, {"HOST"}, "Attribute", "VM"),{"HOST", "VM"})
in
    Result
 
Upvote 0
Welcome to the MrExcel forum.

If you want a formula, consider this. With this lookup sheet:

Book2 (version 1).xlsb
ABCDEFG
1Hostname001vmname001xxxyyyzzz
2Hostname002aaabbbcccvmname002
3Hostname003xxxyyyvmname003
4Hostname004rrrsssttttttvvvvmname004
Sheet5


Here are 2 formulas that work:

Book2 (version 1).xlsb
ABC
1VmnameHostname
2vmname001Hostname001Hostname001
3vmname002Hostname002Hostname002
4vmname003Hostname003Hostname003
Sheet4
Cell Formulas
RangeFormula
B2:B4B2=INDEX(Sheet5!$A:$A,AGGREGATE(15,6,ROW(Sheet5!$B$1:$Z$10)/(Sheet5!$B$1:$Z$10=A2),1))
C2:C4C2=INDEX(Sheet5!A:A,AGGREGATE(15,6,ROW(Sheet5!$A$1:$A$4)/(COUNTIF(OFFSET(Sheet5!$B$1:$Z$1,ROW(Sheet5!$A$1:$A$4)-ROW(Sheet5!$A$1),0),A2)>0),1))


I provided 2 since these type of formulas are pretty computation intensive, especially with a large spreadsheet. If you try one, and your sheet slows down, try the other. If they are both slow, you may be best served by using one of the VBA or Power Query options.
 
Upvote 0
I cannot thank you enough, for such a quick reply. Hope you all have a great weekend!
 
Upvote 0

Forum statistics

Threads
1,215,734
Messages
6,126,542
Members
449,316
Latest member
sravya

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top