0

Okay the title is a little ambiguous but what I'm trying to do is the following: I have a column of file names and I want to open them all using vba. I'm assuming it would be something like what I have below but I'm missing something. The number of files can change so I can't use a definite range.

For each cl in workbookC.worksheets("sheet1").range("A"). If cl.value <> "" then. Open (filename="cl.value"). End if. Next cl
4

1 回答 1

1

下面的代码应该可以工作。请注意,我建议不仅要确保单元格不是空白,还要在尝试打开之前使用 FileSystemObject 检查文件是否存在。

Dim sourceWorksheet
Set sourceWorksheet = workbookC.Worksheets("sheet1")

Dim row
For row = 1 To 10       'rows containing filenames
    If sourceWorksheet.Cells(row, 1).Value <> "" Then
        Dim xlwb
        Set xlwb = Workbooks.Open(sourceWorksheet.Cells(row, 1).Value)
            'do stuff
        xlwb.Close
    End If
Next
于 2013-05-13T23:01:20.800 回答