使用VBA

时间:2017-08-15 23:04:11

标签: excel vba excel-vba

我的问题是:如何通过VBA将多个vLookup公式添加到一个单元格中?

我知道我可以像这样添加一个vLookUp:

... = Application.WorksheetFunction.vLookUp("Search","Matrix","Index")

我的问题是:我有一个255页的工作簿,在我的“总结表”中,我需要变量公式,在这255个工作表中搜索我需要的数据。 因此,excel中宏的输出必须类似于(所有在一个单元格中):

=vLoookUp($A2;Sheet1!A1:A1000;2)+SVERWEIS($A2;Sheet2!A1:A1000;2)+ ...(255 times)

甚至可以用VBA做这样的事情吗?

这是我用来将不同选项分成255张的代码:

这是我到目前为止编写的代码,用于分割股票的不同变化: (它有点工作,但我确定它不是很有效,我是所有这个编程材料的新手)

Sub Sheets()
Application.ScreenUpdating = False
ActiveWindow.WindowState = xlMinimized

Dim Data As String
Dim i As Long
Dim k As Long
Dim x As Long
Dim y As String


For i = 2 To 255

Sheetname = Worksheets("Input").Cells(i, 1).Value
Worksheets.Add.Name = Sheetname
ActiveSheet.Move After:=Worksheets(ActiveWorkbook.Sheets.Count)

x = 1

For k = 2 To 876
Data = Worksheets("Input").Cells(i, k).Value
y = Cells(1, x).Address(RowAbsolute:=False, ColumnAbsolute:=False)
BloomB = "=BDH(" & y & ",""TURNOVER"",""8/1/2011"",""4/30/2016"",""Dir=V"",""Dts=S"",""Sort=A"",""Quote=C"",""QtTyp=Y"",""Days=T"",""Per=cd"",""DtFmt=D"",""UseDPDF=Y"")"
Worksheets(Sheetname).Cells(1, x) = Data
Worksheets(Sheetname).Cells(2, x) = BloomB
x = x + 2
Next k

Application.Wait (Now + TimeValue("0:00:05"))
Next i

ActiveWindow.WindowState = xlMaximized
Application.ScreenUpdating = True
End Sub

1 个答案:

答案 0 :(得分:0)

抱歉,这听起来像是一个非常糟糕的设计。也许Access或SQL Server更适合这种任务。此外,您知道VLOOKUP函数将返回第一个匹配项,但没有后续匹配项,右侧。只是想让你意识到这一点。好的,现在尝试一下。

Function VLOOKAllSheets(Look_Value As Variant, Tble_Array As Range, _
                          Col_num As Integer, Optional Range_look As Boolean)
''''''''''''''''''''''''''''''''''''''''''''''''
'Written by OzGrid.com
'Use VLOOKUP to Look across ALL Worksheets and stops _
at the first match found.
'''''''''''''''''''''''''''''''''''''''''''''''''

Dim wSheet As Worksheet
Dim vFound

On Error Resume Next

    For Each wSheet In ActiveWorkbook.Worksheets
        With wSheet
        Set Tble_Array = .Range(Tble_Array.Address)
            vFound = WorksheetFunction.VLookup _
            (Look_Value, Tble_Array, _
            Col_num, Range_look)
        End With
        If Not IsEmpty(vFound) Then Exit For
    Next wSheet

    Set Tble_Array = Nothing
    VLOOKAllSheets = vFound

End Function

所有内容的完整描述都在这里。

http://www.ozgrid.com/VBA/VlookupAllSheets.htm