Possible Duplicate:
Using FSO to insert folder name into cell based on criteria being met
Here’s what I’m trying to do:
- Check to see where the last cell with data is located in column “A” (works)
- Paste the data into column “A” to wbkVer.Worksheets(“Cutsheets”) (works)
- Find the name of the folder, which is fsoFol.Name (works)
- For each pasted cell in “A”, check for a value.
- If there’s a value, I need to put the fsoFol.Name in the offset(0,5)–this should be able to reference off of the firstRange variable, but per cell, and I can’t figure out how to do it –or the best way to do that. (doesn’t work)
- if there’s no value, skip it.
Suggestions? Thank you in advance.
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
Your syntax of the
For Each a In bloop is wrong:breturns a collection (in this case theCellsin aWorksheet,amust be of the type of members of that collection (in this case `Range.So correct syntax is
That said, you really don’t want to do this. What the loop will do is check every cell on the sheet (billions!), that will be so slow. Also, it will run away, since each time it finds a non blank cell it creates another one to tigger the
If.Update
Based on your comment, I think what you want is:
Athat is not blank, put the value offsoFol.Namein columnFIf so, try this (replace from
wbkCS.Worksheets("Cut Sheet").Range("S4:S2000").Copydown)