我目前在工作表的B列中有一个文件路径(C:/something.xlxs)的文件列表。我想编写一个宏来读取单元格中的值,打开文件,然后转到下一个单元格并打开该文件等。
我目前有这个:
Sub openFiles()
Dim sFullName As String
Dim i As Integer
For i = 1 To 5
If Not IsEmpty(Cells(i, 2)) Then
sFullName = Cells(i, 2).Value
Workbooks.Open fileName:=Cells(i, 2).Value, UpdateLinks:=0
End If
Next i
End Sub
它只打开列表中的第一个文件然后停止。这可能是一个小错误,但我无法发现它。
由于
答案 0 :(得分:0)
尝试
Dim wb As Excel.Workbook
Set wb = Workbooks.Open(Filename:=sFullName)
答案 1 :(得分:0)
我会这样做:
Sub openFiles()
Dim sFullName As String
Dim i As Integer
Dim wsh As Worksheet
On Error GoTo Err_openFiles
Set wsh = ThisWorkbook.Worksheets("Sheet1")
i = 1
Do While wsh.Range("B" & i)<>""
sFullName = wsh.Range("B" & i)
Application.Workbooks.Open sFullName, UpdateLinks:=False
i = i+1
Loop
Exit_openFiles:
On Error Resume Next
Set wsh = Nothing
Exit Sub
Err_openFiles:
MsgBox Err.Description, vbExclamation, Err.Number
Resume Exit_openFiles
End Sub
上面的代码在上下文中工作,并提供了一种处理错误的方法。