包含打开文件路径的引用的数组

时间:2017-02-24 22:23:15

标签: arrays vba excel-vba excel

我尝试了下面的代码,以避免写太多行(因为列表很长 - 超过134),但在尝试打开文件的行中出错。

以下是代码的概念:

Sub update_chemicals()
'update chemical database with macroeconomic indicators

'Dim local_main As String

Dim mylocal As Variant
Dim local_final As String

Dim myname As Variant
Dim name_final As String

'disrectories references
Dim local_1 As String
Dim local_2 As String
Dim local_3 As String
Dim local_4 As String
Dim local_5 As String

'Files
Dim name_1 As String
Dim name_2 As String
Dim name_3 As String
Dim name_4 As String
Dim name_5 As String

'Coal
local_1 = "G:\path\01. CARVAO\Carvao.V1.xlsm"
name_1 = "Carvao.V1.xlsm"

'Perlita
local_2 = "G:\path\Perlita.xlsm"
name_2 = "Perlita.xlsm"

'Diatomaceus Earth
local_3 = "G:\path\Terra_Diatomacea.xlsm"
name_3 = "Terra_Diatomacea.xlsm"

'Chloridric Acid
local_4 = "G:\path\Chloridric_Acid_v2.xlsm"
name_4 = "Chloridric_Acid_v2.xlsm"

'Hexane and Gasoline
local_5 = "G:\path\Price History - Hexane and Gasoline - PLATTS - Jan'17.xlsm"
name_5 = "Price History - Hexane and Gasoline - PLATTS - Jan'17.xlsm"


mylocal = Array("local_1", "local_2", "local_3", "local_4", "local_5")

myname = Array("name_1", "name_2", "name_3", "name_4", "name_5")

   '/////////////////////////////////////////////// COAL \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

For x = 0 To 4

    local_final = mylocal(x)
    name_final = myname(x)

        Workbooks.Open filename:=local_final

我尝试打开文件时出现错误

你可以帮帮我吗?

2 个答案:

答案 0 :(得分:3)

mylocal = Array("local_1", "local_2", "local_3", "local_4", "local_5")

myname = Array("name_1", "name_2", "name_3", "name_4", "name_5")

这意味着你实际上存储了local_1&数组中的name_1而不是local_1&的值NAME_1。

答案 1 :(得分:2)

替换为此行,您应该调用变量本身,而不是变量名称的字符串:

mylocal = Array(local_1,local_2,local_3,local_4,local_5)