你如何在工作簿的每个工作表中使用Application.XLookup?

huangapple go评论69阅读模式
英文:

How can I use Application.XLookup across every sheet in my workbook?

问题

这段代码可以调整为在工作簿的每个工作表中查找,而不仅仅是指定的工作表。下面是修改后的代码示例:

Sub namelookup()
    Dim ws As Worksheet
    Dim result As Variant
    
    For Each ws In ThisWorkbook.Worksheets
        result = Application.XLookup(Val(SalesForm.BHSDMAINNUMBERLF.Value), ws.Range("S:S"), ws.Range("T:T"), result)
    Next ws
    
    SalesForm.BHSDTAPNAMELF.Value = result
End Sub

这将遍历工作簿中的每个工作表,并将结果累积在一个变量中,然后将最终结果赋给了 SalesForm.BHSDTAPNAMELF.Value

英文:

Here is my code, first off:

Sub namelookup()
   SalesForm.BHSDTAPNAMELF.Value = Application.XLookup(Val(SalesForm.BHSDMAINNUMBERLF.Value), Worksheets("NICK TAPS").Range("S:S"), Worksheets("NICK TAPS").Range("T:T"), Application.XLookup(Val(SalesForm.BHSDMAINNUMBERLF.Value), Worksheets("CB TAPS").Range("S:S"), Worksheets("CB TAPS").Range("T:T"), Application.XLookup(Val(SalesForm.BHSDMAINNUMBERLF.Value), Worksheets("GG TAPS").Range("S:S"), Worksheets("GG TAPS").Range("T:T"))))
End Sub

This works for what I need, but how can I adjust this to not only look through specified sheets, but every single one on a workbook?

答案1

得分: 1

I see that you are using the "if_not_found" argument to nest multiple XLOOKUPs inside one another. That is indeed a way to go, but with more than 2 sheets, it's getting hard to read. You'll be better off looking at loops like in the example below.

Also as an aside, when you have a complex formula in the VB Editor, you can always split it on multiple rows for better readability:

SalesForm.BHSDTAPNAMELF.Value = _
Application.XLookup( _
    Val(SalesForm.BHSDMAINNUMBERLF.Value), _
    Worksheets("NICK TAPS").Range("S:S"), _
    Worksheets("NICK TAPS").Range("T:T"), _
    Application.XLookup( _
        Val(SalesForm.BHSDMAINNUMBERLF.Value), _
        Worksheets("CB TAPS").Range("S:S"), _
        Worksheets("CB TAPS").Range("T:T"), _
        Application.XLookup( _
            Val(SalesForm.BHSDMAINNUMBERLF.Value), _
            Worksheets("GG TAPS").Range("S:S"), _
            Worksheets("GG TAPS").Range("T:T") _
        ) _
    ) _
)

An example using the `For Each` loop

For simplicity let's say you have just 2 sheets (Sheet1 and Sheet2) with the following data:

你如何在工作簿的每个工作表中使用Application.XLookup?

data

你如何在工作簿的每个工作表中使用Application.XLookup?

data

To get the value from any sheets in the workbook, you could create a function with a loop over each sheet like this:

Public Function LookupAcrossSheets(LookupValue As Variant, FirstLookupArray As Range, FirstReturnArray As Range, MyWorkbook As Workbook) As Variant

    Dim temp As Variant
    
    Dim ws As Worksheet
    For Each ws In MyWorkbook.Sheets
        temp = Application.XLookup(LookupValue, ws.Range(FirstLookupArray.Address), ws.Range(FirstReturnArray.Address))
        If Not IsError(temp) Then
            Exit For
        End If
    Next
        
    LookupAcrossSheets = temp

End Function

Then you could use it in a sub like this:

Sub GetValue()

    Dim wb As Workbook
    Set wb = ThisWorkbook 'Or replace the reference here to the workbook you need
    
    MsgBox "Value for ""a"" is " & LookupAcrossSheets("a", wb.Sheets(1).Range("A:A"), wb.Sheets(1).Range("B:B"), wb)
    MsgBox "Value for ""e"" is " & LookupAcrossSheets("e", wb.Sheets(1).Range("A:A"), wb.Sheets(1).Range("B:B"), wb)

End Sub

Note that this approach relies on the fact that all the sheets are set up exactly the same way, so you can give the address for the first sheet and it will be reused for the others.


EDIT: In your case, that would look like:

Dim wb As Workbook
Set wb = ThisWorkbook 'Or replace the reference here to the workbook you need

SalesForm.BHSDTAPNAMELF.Value = LookupAccrossSheets(Val(SalesForm.BHSDMAINNUMBERLF.Value), wb.Sheets(1).Range("S:S"), wb.Sheets(1).Range("T:T"), wb)
英文:

I see that you are using the "if_not_found" argument to nest multiple XLOOKUPs inside one another. That is indeed a way to go, but with more that 2 sheets, it's getting hard to read. You'll be better off looking at loops like in the example below.

Also as an aside, when you have a complex formula in the VB Editor, you can always split it on multiple rows for better readability:


    SalesForm.BHSDTAPNAMELF.Value = _
    Application.XLookup( _
        Val(SalesForm.BHSDMAINNUMBERLF.Value), _
        Worksheets("NICK TAPS").Range("S:S"), _
        Worksheets("NICK TAPS").Range("T:T"), _
        Application.XLookup( _
            Val(SalesForm.BHSDMAINNUMBERLF.Value), _
            Worksheets("CB TAPS").Range("S:S"), _
            Worksheets("CB TAPS").Range("T:T"), _
            Application.XLookup( _
                Val(SalesForm.BHSDMAINNUMBERLF.Value), _
                Worksheets("GG TAPS").Range("S:S"), _
                Worksheets("GG TAPS").Range("T:T") _
            ) _
        ) _
    )

<h2> An example using the For Each loop</h2>
For simplicity let's say you have just 2 sheets (Sheet1 and Sheet2) with the following data:

你如何在工作簿的每个工作表中使用Application.XLookup?

<sup>data</sup>

你如何在工作簿的每个工作表中使用Application.XLookup?

<sup>data</sup>

To get the value from any sheets in the workbook, you could create a function with a loop over each sheet like this:


Public Function LookupAcrossSheets(LookupValue As Variant, FirstLookupArray As Range, FirstReturnArray As Range, MyWorkbook As Workbook) As Variant

    Dim temp As Variant
    
    Dim ws As Worksheet
    For Each ws In MyWorkbook.Sheets
        temp = Application.XLookup(LookupValue, ws.Range(FirstLookupArray.Address), ws.Range(FirstReturnArray.Address))
        If Not IsError(temp) Then
            Exit For
        End If
    Next
        
    LookupAcrossSheets = temp

End Function

Then you could use it in a sub like this:


Sub GetValue()

    Dim wb As Workbook
    Set wb = ThisWorkbook &#39;Or replace the reference here to the workbook you need
    
    MsgBox &quot;Value for &quot;&quot;a&quot;&quot; is &quot; &amp; LookupAcrossSheets(&quot;a&quot;, wb.Sheets(1).Range(&quot;A:A&quot;), wb.Sheets(1).Range(&quot;B:B&quot;), wb)
    MsgBox &quot;Value for &quot;&quot;e&quot;&quot; is &quot; &amp; LookupAcrossSheets(&quot;e&quot;, wb.Sheets(1).Range(&quot;A:A&quot;), wb.Sheets(1).Range(&quot;B:B&quot;), wb)

End Sub

Note that this approach relies on the fact that all the sheets are setup exactly the same way, so you can give the address for the first sheet and it will be reuse for the others.

<hr>

EDIT: In your case, that would look like:


    Dim wb As Workbook
    Set wb = ThisWorkbook &#39;Or replace the reference here to the workbook you need
    
    SalesForm.BHSDTAPNAMELF.Value = LookupAccrossSheets(Val(SalesForm.BHSDMAINNUMBERLF.Value), wb.Sheets(1).Range(&quot;S:S&quot;), wb.Sheets(1).Range(&quot;T:T&quot;), wb)

huangapple
  • 本文由 发表于 2023年6月12日 06:37:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/76452787.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定