Macro to loop through worksheets (1 Viewer)

Jihanebd

New member
Local time
Today, 01:55
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
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 09:55
Joined
May 7, 2009
Messages
19,169
is this in excel or access?
 

Ranman256

Well-known member
Local time
Yesterday, 21:55
Joined
Apr 9, 2015
Messages
4,339
Columns has 2 values, start and end.


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

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 09:55
Joined
May 7, 2009
Messages
19,169
add this:

If ws.Index > 3 Then
ws.Activate


or

If ws.Cells(1, iCntr) <> ActiveSheet.Name Then
ws.Columns(iCntr).Delete
 

Jihanebd

New member
Local time
Today, 01:55
Joined
Aug 2, 2016
Messages
2
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
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 09:55
Joined
May 7, 2009
Messages
19,169
as its name implies, make the worksheet active.
 

Users who are viewing this thread

Top Bottom