Object Required - Nested For Loop

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

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
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
I made a mistake in the previous code with the i variable, it should have been x. So the actual error I'm getting is at the Object Required on the "If InStr(1, x, KeepCols(Count), vbTextCompare) = False Then" line.

Corrected Code
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, x, KeepCols(Count), vbTextCompare) = False Then
    x.EntireColumn.Delete
    Else
   End If
    Next Count
    Next x
        
  Workbooks(Filename).Close (True)
  Filename = Dir()
  Loop
  
End Sub
 
Last edited:
Upvote 0
Is the below correct?

You are getting the last column number.
Then you are using it to give the last row number.
Then if the instr is true to delete column A (which it will always be)

Code:
[COLOR="#006400"]lastCol = Cells(1, Columns.Count).End(xlToLeft).Column[/COLOR]
 
 For Each [COLOR="#006400"]x[/COLOR] In Range("A1:A" & [COLOR="#006400"]lastCol[/COLOR])
    For Count = 1 To UBound(KeepCols)
    If InStr(1, x, KeepCols(Count), vbTextCompare) = False Then
   [COLOR="#006400"] x.EntireColumn[/COLOR].Delete
 
Last edited:
Upvote 0
Is the below correct?

You are getting the last column number.
Then you are using it to give the last row number.
Then if the instr is true to delete column A (which it will always be)

Code:
[COLOR=#006400]lastCol = Cells(1, Columns.Count).End(xlToLeft).Column[/COLOR]
 
 For Each [COLOR=#006400]x[/COLOR] In Range("A1:A" & [COLOR=#006400]lastCol[/COLOR])
    For Count = 1 To UBound(KeepCols)
    If InStr(1, x, KeepCols(Count), vbTextCompare) = False Then
   [COLOR=#006400] x.EntireColumn[/COLOR].Delete

Thank you, I am a dope... I've corrected it to the following however I still have the same error at the same line.

Code:
Sub removecols()
Dim KeepCols As Variant
Dim x As Range
KeepCols = Array("Manufacturer", "Manufacturer Part Number", "Description", "Quantity Available", "Part Status")


Path = "C:\Users\Sayre\Desktop\Hirose\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(Cells(1, 1), Cells(1, lastCol))
    For Count = 1 To UBound(KeepCols)
    If StrComp(x, KeepCols(Count), vbTextCompare) = 0 Then
    x.EntireColumn.Delete
    Else
   End If
    Next Count
    Next x
        
  Workbooks(Filename).Close (True)
  Filename = Dir()
  Loop
  
End Sub
 
Upvote 0
Don't know if this will avert the error on that line, but it may help get you on the right track. Your OP indicates you want to delete all columns that don't match one of the values in your array, but that's not what your code is doing. This part:
Rich (BB code):
For Each x In Range(Cells(1, 1), Cells(1, lastCol))
    For Count = 1 To UBound(KeepCols)
         If StrComp(x, KeepCols(Count), vbTextCompare) = 0 Then
            x.EntireColumn.Delete
        Else
        End If
    Next Count
Next x
is missing the first element in the array KeepCols which is a zero-based array (unless you forgot to tell us you have Option Base 1 set). And, this line:
Rich (BB code):
If StrComp(x, KeepCols(Count), vbTextCompare) = 0

is true when x and keepcols(count) are equal, so you don't want to delete the entire column x is in.

 
Upvote 0

Forum statistics

Threads
1,215,436
Messages
6,124,869
Members
449,192
Latest member
MoonDancer

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top