Excel VBA 数组 +vlookup

imjellybrah26
Private Sub CommandButton1_Click()

Application.ScreenUpdating = False

Dim ArrMonth As Variant
ArrMonth = Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec")

Dim i As Long
Dim FilePath As String

On Error Resume Next
For i = LBound(ArrMonth) To UBound(ArrMonth)
    FilePath = "\\shared_network\Task List\2018 Task List\02.DLP TISR\Statistics\ECM\incident_summary - " & ArrMonth(i) & ".csv"

    If Dir(FilePath) <> "" Then
        Stop
    Else
        Range("C14:C19").Offset(i * 6, 0).FormulaR1C1 = "=IFERROR(VLOOKUP(RC[-1],FilePath!R3C2:R8C3,2,FALSE),0)"
    End If

Next i

Application.ScreenUpdating = False

End Sub

我上面有这段代码,我想用来:

  • 定义一个数组
  • 让 VBA 查看特定文件夹以查找文件。
  • 如果文件不存在,请停止。
  • 如果文件存在,则继续(否则)

以下是我在尝试运行代码时注意到的一些问题:

  • 最初,包含此行中的“FilePath” - Range("C14:C19")。Offset(I * 6, 0) 包含数组。但是,会弹出一个窗口,要求我手动选择丢失文件的位置(当它不存在时)。
  • 我尝试将上面的新代码与 vLookup 公式中的“FilePath”行一起使用,但它会问我在哪里打开名为“FilePath”的文件。

如果需要任何说明,请随时发表评论。这行代码杀了我。

凯克罗夫

经过一些尝试,我得到了这个:

Private Sub CommandButton1_Click()

Application.ScreenUpdating = False 'Deactivate ScreenUpdating for better performance
Application.DisplayAlerts = False 'Deactivate DisplayAlerts to prevent the pop-up of VLOOKUP
Dim ArrMonth As Variant 'Create Array of the months, alternatives possible
ArrMonth = Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec")
Dim i As Integer 'Counting var
Const FilePath As String = "\\shared_network\Task List\2018 Task List\02.DLP TISR\Statistics\ECM\" 'Dir to the Files; make sure the "\" is at the end
Const Table As String = "Sheet1" 'Remember: Every sheet needs the same name
On Error Resume Next 'Let's assume there are only errors that we can ignore
For i = LBound(ArrMonth) To UBound(ArrMonth) 'Circle through every single month
    If Dir(FilePath & "incident_summary - " & ArrMonth(i) & ".csv") = "" Then 'If the Dir is non-existened, Dir(...) returns not null, but "", so if "" occurs, this path doesn't exist
        Stop 'Stop right here if Dir non-existening
    Else
        Range("C14:C19").Offset(i * 6, 0).FormulaR1C1 = "=IFERROR(VLOOKUP(RC[-1],'" & FilePath & "[incident_summary - " & ArrMonth(i) & ".csv]" & Table & "'!R3C2:R8C3,2,FALSE),0)" 'Do what I want you to do
    End If
Next i
Application.ScreenRefresh 'Refresh the Screen to update the diffrences
Application.ScreenUpdating = True 'Reactivate ScreenUpdating, bc it won't activate itself
Application.DisplayAlerts = True 'Reactivate DisplayAlerts, bc it won't reactivate itself

End Sub

获得更多信息后,这应该有效:

Private Sub CommandButton1_Click()

Application.ScreenUpdating = False 'Deactivate ScreenUpdating for better performance
Application.DisplayAlerts = False 'Deactivate DisplayAlerts to prevent the pop-up of VLOOKUP
Dim ArrMonth As Variant 'Create Array of the months, alternatives possible
ArrMonth = Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec")
Dim i As Integer 'Counting var
Const FilePath As String = "\\shared_network\Task List\2018 Task List\02.DLP TISR\Statistics\ECM\" 'Dir to the Files; make sure the "\" is at the end
'Leave this one out Const Table As String = "Sheet1" 'Remember: Every sheet needs the same name
On Error Resume Next 'Let's assume there are only errors that we can ignore
For i = LBound(ArrMonth) To UBound(ArrMonth) 'Circle through every single month
    If Dir(FilePath & "incident_summary - " & ArrMonth(i) & ".csv") = "" Then 'If the Dir is non-existened, Dir(...) returns not null, but "", so if "" occurs, this path doesn't exist
        Stop 'Stop right here if Dir non-existening
    Else
        'Replace This:
        'Range("C14:C19").Offset(i * 6, 0).FormulaR1C1 = "=IFERROR(VLOOKUP(RC[-1],'" & FilePath & "[incident_summary - " & ArrMonth(i) & ".csv]" & Table & "'!R3C2:R8C3,2,FALSE),0)" 'Do what I want you to do
        'By this:
        Range("C14:C19").Offset(i * 6, 0).FormulaR1C1 = "=IFERROR(VLOOKUP(RC[-1],'" & FilePath & "[incident_summary - " & ArrMonth(i) & ".csv]" & ArrMonth(i) & "'!R3C2:R8C3,2,FALSE),0)" 'Do what I want you to do
    End If
Next i
Application.ScreenRefresh 'Refresh the Screen to update the diffrences
Application.ScreenUpdating = True 'Reactivate ScreenUpdating, bc it won't activate itself
Application.DisplayAlerts = True 'Reactivate DisplayAlerts, bc it won't reactivate itself

End Sub

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Excel VBA中的Vlookup

来自分类Dev

VBA Excel VLookup

来自分类Dev

Excel vlookup表数组

来自分类Dev

VBA Excel - IFERROR & VLOOKUP 错误

来自分类Dev

带范围的 Excel VBA Vlookup

来自分类Dev

vba excel vlookup:在存储的数组中查找值,在excel中的查找表,将值输出到数组

来自分类Dev

Excel VBA-每个-In和Vlookup

来自分类Dev

用于多个vlookup的Excel VBA代码

来自分类Dev

使用 vlookup 的 Excel VBA 用户表单

来自分类Dev

PHP等效于数组上的Excel vlookup

来自分类Dev

Excel VBA:显示数组?

来自分类Dev

Excel VBA:显示数组?

来自分类Dev

在Excel VBA VLookup函数中使用索引和匹配

来自分类Dev

Excel VBA:vlookup在日期范围表中查找日期行

来自分类Dev

Excel VBA Vlookup运行时错误1004

来自分类Dev

如何使用vlookup vba访问封闭的Excel工作簿

来自分类Dev

Excel VBA如何使用vlookup或获取动态值的替代方法

来自分类Dev

Excel VBA-用户窗体vlookup错误处理

来自分类Dev

Excel-VBA 使用 vlookup 结果的 LEFT 返回

来自分类Dev

Vba Excel Vlookup 搜索值是字符串

来自分类Dev

带有变量的 VBA 中的 Excel Vlookup 公式

来自分类Dev

如何通过 Excel 中的 VBA 从 VLOOKUP 中提取值?

来自分类Dev

在Excel VBA中委托数组

来自分类Dev

Excel VBA数组范围的循环

来自分类Dev

Excel VBA-打印(数组)

来自分类Dev

加速数组循环 vba Excel

来自分类Dev

VLOOKUP返回Excel中所有匹配项的数组

来自分类Dev

Excel-从VLOOKUP从数组到COUNTIF的条件行

来自分类Dev

Excel:使用vlookup但在数组中使用通配符