1
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’: 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.

2
  • 1
    sht1(with an 'one') is not the same as shtl (with a 'L'). Always use Option Explicit at the start of module to see those annoying typos. Commented Apr 16, 2018 at 9:53
  • @VincentG - good one! Commented Apr 16, 2018 at 9:54

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.