Home » excel » Error while deleting columns using excel vba

Error while deleting columns using excel vba

Posted by: admin May 14, 2020 Leave a comment

sub column_delete()

dim x as workbook
dim sht1 as worksheet
dim currentColumn as integer
dim columnHeader as String

Set x = Workbooks.Open("Path to the file")
Set sht1 = x.Sheets("Page 1")
*For currentColumn = sht1.UsedRange.Columns.Count To 1 Step -1*

*columnHeader = shtl.UsedRange.Cells(1, currentColumn).Value*

Select Case columnHeader
 Case "Date", "Time", "Header" 
 Case Else

End Select

End Sub

For all the above highlighted steps i am getting error as Run-time error ‘424’:
Object required.

But i believe i have declared the required object. Sometimes, when i reopen excel the error goes and other time it appears.

Can you please help me with this issue.

P.S : I am a beginner in excel vba.

How to&Answers:

Always write Option Explicit on the top of your module.

Thus, errors like sht1 and shtl will be avoided, as far as you get an error on compile time with the nondefined variable highlighted:

enter image description here