Macro to loop through worksheets

Jihanebd

New member
Local time
Today, 18:52
Joined
Aug 2, 2016
Messages
2
Hi everyone this is the first time i post anything, i'm trying to execute a code in vba but it doesn't do the job, it's supposed to loop through all worksheets starting from the fourth one, and delete the columns that don't match the sheet's name starting from the sixth column
can someone please help me


Sub sbDelete_Columns_IF_Cell_Cntains_String_Text_Value()

Dim lColumn As Long
Dim iCntr As Long
lColumn = 12
For Each ws In ActiveWorkbook.Worksheets

If ws.Index > 3 Then
For iCntr = lColumn To 6 Step -1
If Cells(1, iCntr) <> ActiveSheet.Name Then
Columns(iCntr).Delete
End If
Next
End If
Next
End Sub
 
is this in excel or access?
 
Columns has 2 values, start and end.


Columns(I & ":" & I ).delete
 
add this:

If ws.Index > 3 Then
ws.Activate


or

If ws.Cells(1, iCntr) <> ActiveSheet.Name Then
ws.Columns(iCntr).Delete
 
I'm sorry, it's on excel not access, anyway the ws.activate worked thank you sooo so much, can you tell me what's its purpose ? what is it for and where did i go wrong in my code. Thank you
 
as its name implies, make the worksheet active.
 

Users who are viewing this thread

Back
Top Bottom