Hello, I have written a VB subroutine that is supposed to clear all data from an excel worksheet except the first row which hold some titles. The code is as follows
Sub clear_data(sheet_name As String)
Worksheets(sheet_name).Range(Cells(2, 1), Cells(65536, 256)).ClearContents
End Sub
An example call to this subroutine to clear a worksheet named "rail profile" is as follows:
Call clear_data("rail profile"
The subroutine works fine if the sheet is selected but if it is not selected I get a "runtime error '1004' Application.defined or object-defined" error.
Does anyone know why I may be getting this error and how I can fix things so that the subroutine works no matter what worksheet is currently selected.
Thanks
Sub clear_data(sheet_name As String)
Worksheets(sheet_name).Range(Cells(2, 1), Cells(65536, 256)).ClearContents
End Sub
An example call to this subroutine to clear a worksheet named "rail profile" is as follows:
Call clear_data("rail profile"
The subroutine works fine if the sheet is selected but if it is not selected I get a "runtime error '1004' Application.defined or object-defined" error.
Does anyone know why I may be getting this error and how I can fix things so that the subroutine works no matter what worksheet is currently selected.
Thanks