What is your x variable equal too? The reason you are getting the error is
because VBA can't find that sheet index. Worksheets are indexed as well as
named. You can refer to a sheet using the index number (in your case, x) or
its name. For example, when you open a new workbook in Excel you have 3
sheets in it, thus:
Sheets(1) = Sheets("Sheet1")
Sheets(2) = Sheets("Sheet2")
Sheets(3) = Sheets("Sheet3")
Try this code. I made a few recommendations that may help you in the future
as well.
1. I noticed you had some undeclared variables (x and sht). I would always
declare your variables.
2. I would ALWAYS use Option Explicit at the top of your modules. This
will identify any issues before your code is run.
3. It may be easier to use Range("AB:AB").Column, instead of column number
28, because column numbers are kinda hard to figure out. For example, what
is column number 17? Answer: Its much easier to read your code when you use
Range("Q:Q").Column.
4.) In this example of code I would use the sheet name instead of x. Just
put the sheet name in where I have Sheets("Sheet Name Here")
Hope this helps! If so, let me know, click "YES" below.
Option Explicit
Sub AAA()
Dim sht As Worksheet
Dim StartCol As Long
Dim EndCol As Long
Dim ColNdx As Long
Set sht = Sheets("Sheet Name Here")
StartCol = Range("A:A").Column ' column A
EndCol = Range("AB:AB").Column ' column AB
For ColNdx = EndCol To StartCol Step -1
If Application.CountA(sht.Columns(ColNdx)) = 1 Then
sht.Columns(ColNdx).Delete
End If
Next ColNdx
End Sub