Delete multiple columns w/ vba

ncheever

New Member
Joined
Apr 24, 2013
Messages
9
Hi,

I need to be able to delete multiple columns based on header content. I would like to incorporate a Msgbox where I can input the values to be found in the headers and then deleted (the whole column).

I have some code (thanks to the contribution of another board member on a different question) and it works great, except I want to incorporate a Msgbox that asks for the values I want to delete.


Code:
Sub data_shrnk()
Dim rng As Range

With Worksheets("Dump").Range("A1:CU1")
        Set rng = Worksheets("Dump").Range("A1:CU1").Find(What:="2010", _
            LookAt:=xlPart, MatchCase:=False)
        Do While Not rng Is Nothing
            rng.EntireColumn.Delete
            Set rng = .FindNext
        Loop
    End With

Currently the code will go thru and delete columns with "2010" in the header. I am not sure where/ how to code for the Msgbox especially for multiple values such as: "2006", "2007", "2008", "2009", and "2010".

Can someone help me with this?
Thanks in advance
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
Code:
Sub data_shrnk()
Dim rng As Range
Dim GoBaby As String


GoBaby = Application.InputBox("Enter a year")


With Worksheets("Dump").Range("A1:CU1")
        Set rng = Worksheets("Dump").Range("A1:CU1").Find(What:=GoBaby, _
            LookAt:=xlPart, MatchCase:=False)
        Do While Not rng Is Nothing
            rng.EntireColumn.Delete
            Set rng = .FindNext
        Loop
    End With


End Sub
 
Upvote 0
This is great. However, since I have multiple years to delete, is it possible to code the Msgbox so it comes up again and asks if I have other years to delete before exiting the sub? Thanks
 
Upvote 0
Not pretty, but appears working for me:

Code:
Sub data_shrnk()
Dim rng As Range
Dim Enter_Year As String


GoBaby:
Enter_Year = InputBox("Enter Year", "What Years to Delete?")


    If StrPtr(Enter_Year) = 0 Then
     
        Exit Sub
        Else
            If Len(Enter_Year) = 0 Then
    
                msg = MsgBox("Year was NOT entered." & vbCrLf & vbCrLf & "Click the ""OK"" button to try again.", vbCritical)
                GoTo GoBaby
          
            End If
    End If




    With Worksheets("Dump").Range("A1:CU1")
        Set rng = Worksheets("Dump").Range("A1:CU1").Find(What:=Enter_Year, _
            LookAt:=xlPart, MatchCase:=False)
        On Error Resume Next
            rng.EntireColumn.Delete
            'Set rng = .FindNext
        
    End With


GoTo GoBaby:


End Sub
 
Upvote 0

Forum statistics

Threads
1,214,911
Messages
6,122,196
Members
449,072
Latest member
DW Draft

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