VBA: Find rows that meet a criteria and delete the column

Apple1

Board Regular
Joined
Jan 18, 2015
Messages
121
Hi

Using EXcel 2010.

I need a VBA code that will look in the Year and Month rows for certain criteria and delete that column

For eg, Year=2010, and Month=4, so Column B will be deleted. Thank you
ABCD
Year2010201020122011
Month3432

<tbody>
</tbody>
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
Assuming your year numbers are in row (1) and your Month numbers are in Row (2)
And they are entered like 2016 and 4
Try this:

Code:
Sub Test()
Application.ScreenUpdating = False
Dim i As Long
Dim LastColumn As Long
LastColumn = Cells(1, Columns.Count).End(xlToLeft).Column
Dim yy As Long
Dim mm As Long
yy = InputBox("Enter year like 2012 ")
mm = InputBox("Enter month like 10 ")
 
    For i = LastColumn To 1 Step -1
        If Cells(1, i).Value = yy And Cells(2, i).Value = mm Then
            Columns(i).Delete
        End If
    Next
Application.ScreenUpdating = True
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,979
Messages
6,122,550
Members
449,088
Latest member
davidcom

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