简体   繁体   English

在VLOOKUP中使用单元格值作为表数组值(无宏)

[英]Using cell value as table array value in VLOOKUP (No Macro)

Cell value of B6 = 'Trading Income B6的单元格值='交易收入

=VLOOKUP(B6,'\\myComp.myComp.com\abc\Treas\P&L\data\[DataUS.xls]Smith, Bob'!$A$1:$D$2000,2,FALSE)

This returns (5,555,529.00) 这返回(5,555,529.00)

However, say I wanted to place 但是,说我想放

'\\myComp.myComp.com\abc\Treas\P&L\data\[DataUS.xls]Smith, Bob'!$A$1:$D$2000

in a cell (Lets say B7). 在一个单元格中(让我们说B7)。 How would I structure the VLOOKUP? 我将如何构造VLOOKUP? I tried: 我试过了:

VLOOKUP(B6, B7, 2, FALSE)

And it returns #N/A 它返回#N / A

Thank you 谢谢

尝试

=VLOOKUP(B6;'\\myComp.myComp.com\abc\Treas\P&L\data\[DataUS.xls]Smith, Bob'!$A$1:$D$2000;2;FALSE)

You have to use INDIRECT , see this . 您必须使用INDIRECT请参阅此内容 In your case, use INDIRECT(B7) instead of B7 . 在您的情况下,请使用INDIRECT(B7)而不是B7

Try this: 尝试这个:

=VLOOKUP(B6, INDIRECT(B7), 2, FALSE)

Just ensure that cell B7 contains the exact path ie written exactly in the same manner it is written in the formula. 只要确保单元格B7包含确切的路径,即以与公式中编写的方式完全相同的方式编写即可。

Now adding macros doe sn't really increase the level of complexity, at least I have not seen any case as yet. 现在添加宏并没有真正增加复杂性,至少到目前为止我还没有看到任何情况。 On the contrary a macro, if done correctly, increase efficiency, 相反,如果正确执行宏,则会提高效率, 在此处输入图片说明 dependency and certainty of the results. 结果的依赖性和确定性。

I suggest this solution which includes macros, provides the simplicity and flexibility of building the link to external data using a Name to hold the External Reference created using user input, which is split in the different parts of the external link to make it easier changes ie Path, Filename, Worksheet and Range . 我建议该解决方案包括宏,它提供使用名称来建立使用用户输入创建的外部引用的外部数据链接的简便性和灵活性,该名称被拆分为外部链接的不同部分,以使更改更容易,即路径,文件名,工作表和范围

This includes the creation of five Names to handle the linked formula, here is when you might feel like you got it right when mentioning “increasing the level of complexity” , however we can use the power and flexibility of macros not only to produce the expected outcome in a report, analysis, etc.; 这包括创建五个名称来处理链接的公式, 这是当您提到“增加复杂性级别”时感觉很正确的时候 ,但是我们可以使用宏的功能和灵活性,不仅产生预期的结果报告,分析等中的结果; but also to build forms, reports, graphs, data, etc. and by using macros it also eliminates the need for insanity checks, which at times make us insane, providing and excellent tool to reinstate, review and even change the parameters of large projects when required. 而且还可以构建表格,报告,图形,数据等,并且通过使用宏,还消除了疯狂检查的必要,这有时使我们发疯,提供了一种出色的工具来恢复,审查甚至更改大型项目的参数在需要的时候。

The code provided below includes the creation of the Names , also the refresh the Name that holds the External Link Reference once the user changes any part of the external reference in the worksheet. 下面提供的代码包括名称的创建,并且一旦用户在工作表中更改了外部引用的任何部分,就刷新包含外部链接引用名称

First we run this code to create the names (copy this in a module) 首先,我们运行以下代码来创建名称(将其复制到模块中)

Option Explicit
Option Base 1

Sub FmlLnk_WshAddNames()
Const kRowIni   As Byte = 2
Const kCol      As Byte = 3
Const kWshTrg   As String = "Sht(1)"
Dim aNames As Variant
aNames = fNames_Get
Dim WshTrg As Worksheet
Dim bRow As Byte
Dim b As Byte

    Set WshTrg = ThisWorkbook.Worksheets(kWshTrg)
    With WshTrg
        For b = 1 To UBound(aNames)
            bRow = IIf(b = 1, kRowIni, 1 + bRow)
            .Names.Add Name:=aNames(b), RefersTo:=.Cells(bRow, kCol)
            .Names(aNames(b)).Comment = "Name to create link to external range"
    Next: End With
End Sub

Function fNames_Get() As Variant
    fNames_Get = Array("_Path", "_Filename", "_Worksheet", "_Range")
End Function

Now that the Names to hold the parts of the external link are created we add the worksheet event to automatically update the name holding the External Link Reference (see https://msdn.microsoft.com/EN-US/library/office/ff198331.aspx ) 现在已经创建了用于保存外部链接各部分的名称,我们添加了工作表事件以自动更新保存外部链接引用的名称(请参阅https://msdn.microsoft.com/EN-US/library/office/ff198331 .aspx

To go to the event procedures for the Worksheet that contains the formula right-click the sheet tab and click “View Code” on the shortcut menu. 要转到包含公式的工作表的事件过程,请右键单击工作表选项卡,然后在快捷菜单上单击“查看代码”。 Copy the code below in the Worksheet code 复制下面的代码在工作表代码中

Option Explicit
Option Base 1

Private Sub Worksheet_BeforeDoubleClick(ByVal RngTrg As Range, bCancel As Boolean)
Const kFmlLnk As String = "_FmlLnk"
Dim aNames As Variant, vName As Variant
aNames = fNames_Get
Dim WshThs As Worksheet
Dim bLnkExt As Boolean
Dim sLnkExt As String

Set WshThs = RngTrg.Worksheet
With WshThs
    Application.Goto .Cells(1), 1

    Rem Validate ActiveCell
    bLnkExt = False
    For Each vName In aNames
        If .Names(vName).RefersToRange.Address = RngTrg.Address Then
            bLnkExt = True
            Exit For
    End If: Next

    Rem Reset Name Link External
    If bLnkExt Then

        Rem Built External Formula Link
        sLnkExt = "=" & Chr(39) & .Names(aNames(1)).RefersToRange.Value2 & _
            "[" & .Names(aNames(2)).RefersToRange.Value2 & "]" & _
            .Names(aNames(3)).RefersToRange.Value2 & Chr(39) & Chr(33) & _
            .Names(aNames(4)).RefersToRange.Value2

        Rem Add External Formula Link Name
        .Names.Add Name:=kFmlLnk, RefersTo:=sLnkExt
        .Names(kFmlLnk).Comment = "Name to link external range in Formula"
End If: End With
End Sub

This procedure will run every time the users double-clicks in any of the four Names created in the worksheets that holds the External Link Formula 每当用户双击包含外部链接公式的工作表中创建的四个名称中的任何一个时,将运行此过程

The Formula to use the external link name is: 使用外部链接名称的公式是:

=VLOOKUP($B9,_FmlLnk,3,0)

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM