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" 'Nothing' Case Else *shtl.Columns(currentColumn).Delete* End Select Next End Sub
For all the above highlighted steps i am getting error as Run-time error ‘424’:
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.
Option Explicit on the top of your module.
Thus, errors like
shtl will be avoided, as far as you get an error on compile time with the nondefined variable highlighted: