为了避免写太多行,我尝试了以下代码(由于列表很长-超过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


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

你能帮我吗?

最佳答案

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的值。

关于arrays - 带有引用的数组,用于打开文件的路径,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42449298/

10-11 16:55