I am working with vba for data processing within excel, I have some reports, each one on its own sheet, in which I need to fill in the cells that are blank with the value of the cell above them.
The problem is that when I finish the first sheet, it gives me a 1004 error, and I would like it to go past that error, and continue with the next sheet. The code I have is the following:
Function structure_sheets()
With ThisWorkbook
For Each Sheet In .Sheets
If (InStrRev(Sheet.Name, "Node") > 0) Then
LastRow = Sheet.UsedRange.Rows.Count
With Range("A8:F" & Range("F" & LastRow).End(xlDown).Row)
.SpecialCells(xlBlanks).FormulaR1C1 = "=R[-1]C"
.Value = .Value
End With
End If
Next
End With
End Function
And the error:
OnErrors don't seem to work with these blocks of code.
If anyone has an idea, suggestions are welcome.
Thank you very much.
Let's go for points:
Function structure_sheets()
) are used to return a value, in case you don't return anything, which is this case, you should use theSub
.Option Explicit
at the top of your module. It will force you to declare all variables.With ThisWorkbook
since you only reference it once.LastRow
you correctly reference the sheet (in turn, referenced to the workbook because it is inside the loop that runs through it), but when you do theWtih Range...
you never make reference to the sheet, so if the active sheet <> the worksheet loop, that's not going to work for you. You should always make full references to ranges: book-sheet-range.Taking all of the above into account, this is what your code would look like: