Find criteria in a row then delete columns past but to a stop point

Danoz

New Member
Joined
Jul 22, 2010
Messages
39
This is a different topic, in the previous one it is to delete rows, and this one is to delete columns. I suggest you create a new thread.
Thanks Dante Amor for your assistance with the rows query.

This is a separate query regarding deleting columns once criteria is met, then stopping at a point.



I have a variable named value "MAX" which can be between 1 & 12. 1 to 12 is numbered in 'E3' to 'P3' of the worksheet, 'A3' to 'D3' are blank. I want to delete the columns where the value is greater than MAX, but not if MAX =12, or to stop once the script hits 12 in 'P3'.

Here's what I have so far, which is based on a 2009 q&a somewhere in the forum so apologies if it's outdated.

"Delete unneeded columns in selections"

Dim LR As Long, i As Long
Application.ScreenUpdating = False
If MAX = 12 Then End If -------this is where it sticks-------
Else:LR = Range(Columns.Count & "3").End(xlLeft).Column
For i = LR To 1 Step -1
With Range(i & "3")
If .Value = 12 Then End If
Else:
If .Value > MAX Then .Resize(7).EntireColumn.Delete
End With
Next i
Application.ScreenUpdating = True


I've had a go at amending since the first attempt, but still no luck. Here's my current version that doesn't work.


Code:
' Delete unneeded columns in selections
    If MAXPYMTS >= 12 Then
        Range("B1").Select
        
        End If
        
    LR = Range(Columns.Count & "3").End(xlLeft).Column
    For i = LR To 1 Step -1
        If MAXPYMTS <= "11" And .Value > MAXPYMTS Then
        With Range(i & "3")
        .Select.EntireColumn.Delete
        End With
    Next i
    
    Else:
        If .Value = 12 Then
    End If
        
    Range("B1").Select
        
    Application.ScreenUpdating = True
 
How about
Code:
Sub test()
   Dim Mx As Variant
   Mx = InputBox("Max value")
   If Mx < 12 Then Columns(5 + Mx).Resize(, 12 - Mx).Delete
End Sub
 
Upvote 0

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.

Forum statistics

Threads
1,224,823
Messages
6,181,178
Members
453,021
Latest member
Justyna P

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