Hello All,
I tried to do a loop that would write an entry to an error log sheet in the event a folder couldn't be created or already existed, but I haven't done it well, and continue to endlessly generate errors. If someone has any insight into where I went wrong, or suggestions on a more elegant way of approaching this, I would greatly appreciate it!
Just in case here's the error log code:
I tried to do a loop that would write an entry to an error log sheet in the event a folder couldn't be created or already existed, but I haven't done it well, and continue to endlessly generate errors. If someone has any insight into where I went wrong, or suggestions on a more elegant way of approaching this, I would greatly appreciate it!
VBA Code:
Sub CreateTheMonthlyFolders()
Dim Lastrow As Long
Dim fRow As Long
Dim fRowv As Long
Dim ws As Worksheet
Dim fPath As String
On Error GoTo err
Set ws = ThisWorkbook.Sheets("FOLDERStest")
With ws
Lastrow = .Range("B" & Rows.Count).End(xlUp).Row
fRowv = ThisWorkbook.Sheets("FOLDERStest").Range("Z1").Value
For fRow = fRowv To Lastrow
If .Range("B" & fRow).Value <> "" Then
fPath = .Range("D" & fRow) & .Range("E" & fRow)
MkDir fPath
fRowv = fRowv + 1
Else
If fRow > Lastrow Then
ThisWorkbook.Sheets("FOLDERStest").Range("Z1").Value = 5
MsgBox "loop ended"
Exit Sub
End If
End If
Next fRow
End With
err:
MsgBox "error " & fRow & ". last row is " & Lastrow & ". fRow is " & fRow
fRowv = fRowv + 1
ThisWorkbook.Sheets("FOLDERStest").Range("Z1").Value = fRowv
Call ErrorLog
Call CreateTheMonthlyFolders
End Sub
Just in case here's the error log code:
VBA Code:
Sub ErrorLog()
Dim Lastrow As Long
Sheets("ErrorLOG").Select
Lastrow = Range("B" & Rows.Count).End(xlUp).Row
Range("B" & Lastrow).Offset(1, 0).Select
ActiveCell.FormulaR1C1 = "=NOW()"
ActiveCell.Copy
Selection.PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:= _
xlNone, SkipBlanks:=False, Transpose:=False
Sheets("FOLDERStest").Select
Range("Z1").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("ErrorLOG").Select
Range("B" & Lastrow).Offset(1, 1).Select
ActiveSheet.Paste
End Sub