可能重复:
Using FSO to insert folder name into cell based on criteria being met
这是我正在尝试做的事情:
连连呢?提前谢谢。
Dim FSO As Object, fld As Object, Fil As Object
Dim fsoFile As Object
Dim fsoFol As Object
Set FSO = CreateObject("Scripting.FileSystemObject")
Set fld = FSO.getfolder(folderPath)
If FSO.folderExists(fld) Then
For Each fsoFol In FSO.getfolder(folderPath).subfolders
For Each fsoFile In fsoFol.Files
If Mid(fsoFile.Name, InStrRev(fsoFile.Name, ".") + 1) = "xls" Then
fileName = fsoFile.Name
Application.ScreenUpdating = False
Set wbkCS = Workbooks.Open(fsoFile.Path)
If fileName Like "V*.xls" Then
wbkCS.Worksheets("Cut Sheet").Range("S4:S2000").Copy
With wbkVer.Worksheets("Cutsheets")
Set firstRange = .Range("A" & .Rows.Count).End(xlUp).Offset(1, 0)
firstRange.PasteSpecial xlPasteValues
For Each firstRange.Value in wbkVer.Worksheets("Cutsheets")
If firstRange.Value <> "" Then
firstRange.Offset(0, 5).Value = fsoFol.Name
End If
Next
答案 0 :(得分:1)
For Each a In b
循环的语法错误:b
返回一个集合(在这种情况下,Cells
中的Worksheet
,a
必须是该集合的成员类型(在本例中为`Range。
正确的语法是
For Each firstRange in wbkVer.Worksheets("Cutsheets")
那就是说,你真的不想这样做。循环将做的是检查工作表上的每个单元格(数十亿!),这将所以慢。此外,它会逃跑,因为每次找到一个非空白单元格时,它会创建另一个空白单元格来触发If
。
<强>更新强>
根据您的评论,我认为您想要的是:
A
中粘贴的非空白的每个单元格,请将fsoFol.Name
的值放在专栏F
如果是这样,请尝试此操作(从wbkCS.Worksheets("Cut Sheet").Range("S4:S2000").Copy
下来替换
Dim rngSource As Range
Set rngSource = wbkCS.Worksheets("Cut Sheet").Range("S4:S2000")
With wbkVer.Worksheets("Cutsheets")
Set firstRange = .Range("A" & .Rows.Count).End(xlUp).Offset(1, 0).Resize(rngSource.Rows.Count, 1)
firstRange.Value = rngSource.Value
firstRange.SpecialCells(xlCellTypeConstants).Offset(0, 5) = fsoFol.Name
End With