Excel VLOOKUP使用String来查找文件中的数据。太慢了

时间:2014-07-18 20:29:50

标签: excel excel-vba while-loop concatenation vlookup vba

这是我能得到的最好的。任何人都可以搜索并需要这种数据拉取的最佳答案。我不得不把它分成几个部分;这些工作计算机只是无法处理这种类型的负载。最大数据拉力约为800线,大约需要一分钟来拉动所有公式和数据。感谢下面的人们提供的帮助。

Sub Update()

Dim ScreenUpdateState As Boolean
Dim StatusBarShow As Boolean
Dim CalcState As Long
Dim EventState As Boolean

Dim ws As Worksheet
Dim location_string As String
Dim count As Integer

'Save the current state of Excel settings
ScreenUpdateState = Application.ScreenUpdating
StatusBarShow = Application.DisplayStatusBar
CalcState = Application.Calculation
EventState = Application.EnableEvents

'Change Excel to faster procedure settings
Application.ScreenUpdating = False
Application.DisplayStatusBar = False
Application.Calculation = xlCalculationManual
Application.EnableEvents = False
ActiveSheet.DisplayPageBreaks = False


Set ws = ThisWorkbook.Sheets("%")
location_string = Sheets("Driver(s)").Cells(5, "G").Text

For count = 7 To 139
Cells(count, "F").Formula = "=IFERROR((VLOOKUP($C" & count    & ",'S:\xxxx\xxxxx\xxxxxx\xxxxx\xxxxxxxxxx\[xxxxxxxxxxxxxxxxxxxxxxxxxx.xlsx]" + location_string + "'!$A:$K,11,FALSE)),"" - "")"
Next count

'Restore Excel settings to original state
Application.ScreenUpdating = ScreenUpdateState
Application.DisplayStatusBar = StatusBarShow
Application.Calculation = CalcState
Application.EnableEvents = EventState

MsgBox ("Update Complete")
End Sub
祝你好运!

  • 罗斯

Orignal主题:

  

好的,我现在有这个并且它有效。然而,它被放慢使用   这一个代码只运行所需计算的1/16   需要几分钟才能完成。任何人都知道加快速度的方法   过程

Sub Test()
Dim ws As Worksheet
Dim location_string As String
Dim count As Integer

Set ws = ThisWorkbook.Sheets("%")
location_string = ws.Cells(2, "E").Text

count = 7

While count < 138
Cells(count, "F").Formula = "=IFERROR((VLOOKUP($C" & count & ",
'S:\xxxx\xxxx\xxxx\xxxx\xxxxx\[xxxxxx.xlsx]" + location_string + "'!$A:$K,11,FALSE)),"" - "")"
count = count + 1
Wend

MsgBox ("Done")
End Sub
     

以下是原帖:

     

我在另一张纸上有一个值列表,它将创建一部分   我需要的字符串:

=CONCATENATE ((INDEX('Driver(s)'!$B$1:$B$48,'Driver(s)'!$G$3,1)),"Epic")
     

这会将单元格设置为='O614Epic

     

现在尝试添加一个Vlookup来拉取:

S:\xxxxxxxxxxxxxxx\xxxxxxxxx\xx\xx\xx\[Random File Name.xlsx]0614Epic'!$A:$K
     

根据下拉框,#### Epic文件将更改为   此刻将值更正为字符串但无法获取Vlookup   从正确的工作簿中提取。我也需要这个打开非打开   工作簿。要导入Excel工作簿本身的数据太多。

     

感谢。

     
      
  • 罗斯
  •   

2 个答案:

答案 0 :(得分:1)

如果没有使用VLOOKUP,请跳到奖励信息。您可以使用VBA查找并将值放入单元格而不是公式,而不是让VLOOKUP公式重新计算每次更改并减慢电子表格的速度。我尽力根据你提供的内容进行定制。如果您对任何部件有疑问,请告诉我。

Function WorksheetExists(ByVal WorksheetName As String) As Boolean
    Dim Sht As Worksheet

For Each Sht In ActiveWorkbook.Worksheets
    If Application.Proper(Sht.Name) = Application.Proper(WorksheetName) Then
        WorksheetExists = True
        Exit Function
    End If
Next Sht

End Function

Sub RossQuestion()
    Dim wbdata As Workbook
    Dim ws As Worksheet
    Dim Cell As Range
    Dim location_string As String
    Dim strcheck As String
    Dim count As Integer

Set ws = ThisWorkbook.Sheets("%")
location_string = ws.Cells(2, "E").Text
count = 7

While count < 138
    Set wbdata = Workbooks.Open("S:\xxxx\xxxx\xxxx\xxxx\xxxxx\xxxxxx.xlsx", , True)
    If WorksheetExists(location_string) Then
        Set Cell = wbdata.Sheets(location_string).Columns("A").Find(ws.Range("$C$" & count).Value, _
        wbdata.Sheets(location_string).Range("A1"), xlFormulas, xlWhole, xlByRows, xlNext, False)
        strcheck = Cell.Offset(0, 10).Value
        If Len(Trim(strcheck)) <> 0 Then
            ws.Cells(count, "F").Value = Cell.Offset(0, 10).Value
        Else
            ws.Cells(count, "F").Value = " - "
        End If
    Else
        ws.Cells(count, "F").Value = " - "
    End If
    count = count + 1
    wbdata.Close False
Wend

MsgBox "Done"

End Sub

奖励信息:

如果您未将代码包装在此类内容中,请考虑将此用于所有未来的VBA。 this link中的第一个提示详述了这些操作。

Dim ScreenUpdateState As Boolean
Dim StatusBarShow As Boolean
Dim CalcState As Long
Dim EventState As Boolean

'Save the current state of Excel settings
ScreenUpdateState = Application.ScreenUpdating
StatusBarShow = Application.DisplayStatusBar
CalcState = Application.Calculation
EventState = Application.EnableEvents

'Change Excel to faster procedure settings
Application.ScreenUpdating = False
Application.DisplayStatusBar = False
Application.Calculation = xlCalculationManual
Application.EnableEvents = False
ActiveSheet.DisplayPageBreaks = False

'<<<YOUR CODE HERE>>>

'Restore Excel settings to original state
Application.ScreenUpdating = ScreenUpdateState
Application.DisplayStatusBar = StatusBarShow
Application.Calculation = CalcState
Application.EnableEvents = EventState

原始答案:

虽然您可以引用其他工作簿中的数据(甚至是非打开的),但您在VLOOKUP的table_array参数中的路径必须完全输入。

所以虽然VLOOKUP接受......

=VLOOKUP('Driver(s)'!$G$3, 'S:\xxxxx\FileName.xlsx'!$A:$K, 3, FALSE)

它不会接受table_array中的任何计算或连接,例如......

=VLOOKUP('Driver(s)'!$G$3, 'S:\xxxxx\ & O614Epic & .xlsx'!$A:$K, 3, FALSE)
=VLOOKUP('Driver(s)'!$G$3, 'S:\xxxxx\ & INDIRECT(B1) & Epic.xlsx'!$A:$K, 3, FALSE)

完整路径字符串以外的任何内容都被认为过于不稳定。 MATCH INDEX也是如此。不幸的是,VLOOKUP并不像你喜欢的那样动态,#### Epic需要你输入O614Epic而不是来自另一个单元格。

总有VBA。 VBA可以做到一切。

答案 1 :(得分:0)

删除循环并尝试:

Sub Test()
    Dim ws As Worksheet
    Dim location_string As String
    Dim myformula As String

    With Application
        .ScreenUpdating = False
        .DisplayAlerts = False
    End With

    Set ws = ThisWorkbook.Sheets("%")
    location_string = ws.Cells(2, "E").Value '~~> I'd suggest you use Value

    myformula = "=IFERROR((VLOOKUP($C7,'S:\xxxx\xxxx\xxxx\xxxx\xxxxx\[xxxxxx.xlsx]" & _
        location_string & "'!$A:$K,11,FALSE)),"" - "")"

    Range("F7:F138").Formula = myformula
    Msgbox "Done"

    With Application
        .ScreenUpdating = True
        .DisplayAlerts = True
    End With
End Sub

我的计算机需要5秒,但如果目标文件位于网络服务器中,则会有所不同。 HTH。