admin管理员组文章数量:1287878
Sub TEST()
Dim Path As String, r As Range, filename As String, count As Integer
r = Range("J2")
Path = r & "*.docx"
On Error Resume Next
filename = Dir(Path)
Do While filename <> ""
count = count + 1
filename = Dir()
Loop
Range("I2") = count
End Sub
I have empty cells in I2:I1000 and the paths in J2:J1000 and i want fill the I2:I1000 with the counts By far i can fill only one cell. How can i fill all at once?
Thanks
Sub TEST()
Dim Path As String, r As Range, filename As String, count As Integer
r = Range("J2")
Path = r & "*.docx"
On Error Resume Next
filename = Dir(Path)
Do While filename <> ""
count = count + 1
filename = Dir()
Loop
Range("I2") = count
End Sub
I have empty cells in I2:I1000 and the paths in J2:J1000 and i want fill the I2:I1000 with the counts By far i can fill only one cell. How can i fill all at once?
Thanks
Share Improve this question asked Feb 22 at 17:27 Paliouras GeePaliouras Gee 131 silver badge2 bronze badges1 Answer
Reset to default 1Using a For
clause to loop through cells.
Note: The path in column J should end with "\", otherwise, the code should be adjusted to:
Path = r & "\" & "*.docx"
Sub TEST()
Dim Path As String, r As Range, filename As String, count As Long
For Each r In Range("J2:J1000") ' loop through cells
If Len(r.Value) > 0 Then ' check if the cell is blank
count = 0 ' reset counter
Path = r & "*.docx"
' Path = r & "\" & "*.docx" ' add path delimiter
filename = Dir(Path)
Do While filename <> ""
count = count + 1
filename = Dir()
Loop
r.Offset(, -1) = count ' populate Col I
End If
Next
End Sub
Microsoft documentation:
Range.Offset property (Excel)
本文标签: excelcount files in folder by taking path from row cellsStack Overflow
版权声明:本文标题:excel - count files in folder by taking path from row cells - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741332864a2372866.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论