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

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.

My Aswer Is This

Well-known Member
Joined
Jul 5, 2014
Messages
19,343
Office Version
  1. 2021
Platform
  1. Windows
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,191,076
Messages
5,984,495
Members
439,893
Latest member
johnsboxftm

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
Top