I have searched thoroughly the forums on how to keep only columns whose header is the text "x","y" or "c".. and saw a common formula pasted below:
Option Explicit
Option Compare Text
Public Sub DeleteColumns()
Dim iLastCol As Long
Dim iPtr As Long
iLastCol = Cells(1, Columns.Count).End(xlToLeft).Column
For iPtr = iLastCol To 1 Step -1
If InStr(Cells(1, iPtr).Value, "Review ID") > 0 Then
Columns(iPtr).Delete Shift:=xlToLeft
End If
Next iPtr
End Sub
However, whne I paste this directly into my VBA (with adjusted "iptr" and text name), I go through each line with F8 and it just keeps going back an forth between the beggining of the IF statement,and the "End if", while skipping the Columns.Delete command.This is a screenshot of a simplified version of what I'd need to do. I want the macro to delete every column that is not exactly "Review ID" or "Issue ID" (will include about 15 columns headsreas that I need to keep out of 50 or so columns)[TABLE="width: 514"]
<TBODY>[TR]
[TD="class: xl33875, width: 108, bgcolor: #002d72"]Review ID
[/TD]
[TD="class: xl33875, width: 121, bgcolor: #002d72"]Audit ID
[/TD]
[TD="class: xl33875, width: 110, bgcolor: #002d72"]Issue ID
[/TD]
[TD="class: xl33875, width: 164, bgcolor: #002d72"]Legal Entity
[/TD]
[TD="class: xl33875, width: 181, bgcolor: #002d72"]IA Division Owner
[/TD]
[/TR]
[TR]
[TD="class: xl33876, bgcolor: white"][/TD]
[TD="class: xl33876, bgcolor: white"]0
[/TD]
[TD="class: xl33876, bgcolor: white"]R13349-01
[/TD]
[TD="class: xl33876, bgcolor: white"].
[/TD]
[TD="class: xl33876, bgcolor: white"][/TD]
[/TR]
[TR]
[TD="class: xl33876, bgcolor: white"][/TD]
[TD="class: xl33876, bgcolor: white"]0
[/TD]
[TD="class: xl33876, bgcolor: white"]R13720-01
[/TD]
[TD="class: xl33876, bgcolor: white"].
[/TD]
[TD="class: xl33876, bgcolor: white"][/TD]
[/TR]
[TR]
[TD="class: xl33876, bgcolor: white"][/TD]
[TD="class: xl33876, bgcolor: white"]0
[/TD]
[TD="class: xl33876, bgcolor: white"]R14039-01
[/TD]
[TD="class: xl33876, bgcolor: white"][/TD]
[TD="class: xl33876, bgcolor: white"][/TD]
[/TR]
[TR]
[TD="class: xl33876, bgcolor: white"][/TD]
[TD="class: xl33876, bgcolor: white"]0
[/TD]
[TD="class: xl33876, bgcolor: white"]R14039-02
[/TD]
[TD="class: xl33876, bgcolor: white"][/TD]
[TD="class: xl33876, bgcolor: white"][/TD]
[/TR]
[TR]
[TD="class: xl33876, bgcolor: white"][/TD]
[TD="class: xl33876, bgcolor: white"]0
[/TD]
[TD="class: xl33876, bgcolor: white"]R14040-02
[/TD]
[TD="class: xl33876, bgcolor: white"][/TD]
[TD="class: xl33876, bgcolor: white"][/TD]
[/TR]
[TR]
[TD="class: xl33876, bgcolor: white"][/TD]
[TD="class: xl33876, bgcolor: white"]0
[/TD]
[TD="class: xl33876, bgcolor: white"]R14040-03
[/TD]
[TD="class: xl33876, bgcolor: white"][/TD]
[TD="class: xl33876, bgcolor: white"][/TD]
[/TR]
</TBODY>[/TABLE]
Thank You!
</PRE>
Option Explicit
Option Compare Text
Public Sub DeleteColumns()
Dim iLastCol As Long
Dim iPtr As Long
iLastCol = Cells(1, Columns.Count).End(xlToLeft).Column
For iPtr = iLastCol To 1 Step -1
If InStr(Cells(1, iPtr).Value, "Review ID") > 0 Then
Columns(iPtr).Delete Shift:=xlToLeft
End If
Next iPtr
End Sub
However, whne I paste this directly into my VBA (with adjusted "iptr" and text name), I go through each line with F8 and it just keeps going back an forth between the beggining of the IF statement,and the "End if", while skipping the Columns.Delete command.This is a screenshot of a simplified version of what I'd need to do. I want the macro to delete every column that is not exactly "Review ID" or "Issue ID" (will include about 15 columns headsreas that I need to keep out of 50 or so columns)[TABLE="width: 514"]
<TBODY>[TR]
[TD="class: xl33875, width: 108, bgcolor: #002d72"]Review ID
[/TD]
[TD="class: xl33875, width: 121, bgcolor: #002d72"]Audit ID
[/TD]
[TD="class: xl33875, width: 110, bgcolor: #002d72"]Issue ID
[/TD]
[TD="class: xl33875, width: 164, bgcolor: #002d72"]Legal Entity
[/TD]
[TD="class: xl33875, width: 181, bgcolor: #002d72"]IA Division Owner
[/TD]
[/TR]
[TR]
[TD="class: xl33876, bgcolor: white"][/TD]
[TD="class: xl33876, bgcolor: white"]0
[/TD]
[TD="class: xl33876, bgcolor: white"]R13349-01
[/TD]
[TD="class: xl33876, bgcolor: white"].
[/TD]
[TD="class: xl33876, bgcolor: white"][/TD]
[/TR]
[TR]
[TD="class: xl33876, bgcolor: white"][/TD]
[TD="class: xl33876, bgcolor: white"]0
[/TD]
[TD="class: xl33876, bgcolor: white"]R13720-01
[/TD]
[TD="class: xl33876, bgcolor: white"].
[/TD]
[TD="class: xl33876, bgcolor: white"][/TD]
[/TR]
[TR]
[TD="class: xl33876, bgcolor: white"][/TD]
[TD="class: xl33876, bgcolor: white"]0
[/TD]
[TD="class: xl33876, bgcolor: white"]R14039-01
[/TD]
[TD="class: xl33876, bgcolor: white"][/TD]
[TD="class: xl33876, bgcolor: white"][/TD]
[/TR]
[TR]
[TD="class: xl33876, bgcolor: white"][/TD]
[TD="class: xl33876, bgcolor: white"]0
[/TD]
[TD="class: xl33876, bgcolor: white"]R14039-02
[/TD]
[TD="class: xl33876, bgcolor: white"][/TD]
[TD="class: xl33876, bgcolor: white"][/TD]
[/TR]
[TR]
[TD="class: xl33876, bgcolor: white"][/TD]
[TD="class: xl33876, bgcolor: white"]0
[/TD]
[TD="class: xl33876, bgcolor: white"]R14040-02
[/TD]
[TD="class: xl33876, bgcolor: white"][/TD]
[TD="class: xl33876, bgcolor: white"][/TD]
[/TR]
[TR]
[TD="class: xl33876, bgcolor: white"][/TD]
[TD="class: xl33876, bgcolor: white"]0
[/TD]
[TD="class: xl33876, bgcolor: white"]R14040-03
[/TD]
[TD="class: xl33876, bgcolor: white"][/TD]
[TD="class: xl33876, bgcolor: white"][/TD]
[/TR]
</TBODY>[/TABLE]
Thank You!
</PRE>
Last edited: