audioboxer
New Member
- Joined
- Aug 13, 2012
- Messages
- 23
Hi Everyone,
Can't seem to figure this one out, been picking my brain for a few hours.. I am getting object required for the x.EntireColumn.Delete portion. My guess is it's not carrying the x variable into the next for loop? Basically I'm trying to loop through every workbook in the folder and delete all columns that don't have on of the values in my Array
Any help would be appreciated. Thanks
Can't seem to figure this one out, been picking my brain for a few hours.. I am getting object required for the x.EntireColumn.Delete portion. My guess is it's not carrying the x variable into the next for loop? Basically I'm trying to loop through every workbook in the folder and delete all columns that don't have on of the values in my Array
Any help would be appreciated. Thanks
Code:
Sub removecols()
Dim KeepCols As Variant
KeepCols = Array("Apple", "Banana", "Orange", "Yellow", "Brown")
Path = "C:\Users\User\Desktop\test\"
Filename = Dir(Path & "*.xlsx")
Do While Filename <> ""
Workbooks.Open Filename:=Path & Filename
lastCol = Cells(1, Columns.Count).End(xlToLeft).Column
For Each x In Range("A1:A" & lastCol)
For Count = 1 To UBound(KeepCols)
If InStr(1, i, KeepCols(Count), vbTextCompare) = False Then
x.EntireColumn.Delete
Else
End If
Next Count
Next x
Workbooks(Filename).Close (True)
Filename = Dir()
Loop
End Sub