问题
Hi i have come up with a code to open multiple workbooks. The code is
Sub OPEN_hari()
Dim r As Long
For r = 1 To 10
Workbooks.Open Filename:=Sheet2.Cells(r, 1).Value
Next r
End Sub
But the problem here is if only 3 cells ((1,1) (2,1) (3,1)) has the path of the workbook files, an error message is thrown that coulndt open "" files. How can i make this macro to open only the files that are mentioned without error message. As i am beginner in this any help is highly appreciated
回答1:
There're two ways of getting what you need:
Run an infinite loop, checking the emptiness of a cell:
Dim i as Long: i = 0 While Not IsEmpty(Sheet1.Cells(i + 1, 1)) 'assuming the value is in Column A 'your code i = i + 1 Wend
Get the number of used rows, however there's a risk that a cell might have been used, but is have no data now, so I'm including a check:
Dim ur as Long: ur = Sheet1.UsedRange.Rows.Count Dim i as Long For i = 0 to (ur - 1) If LenB(Sheet1.Cells(i + 1, 1).Value) > 0 then 'your code End If Next i
来源:https://stackoverflow.com/questions/15770574/how-to-count-non-empty-cells-in-macro