Keeping one country from a sheet and deleting the rest

mike76

New Member
Joined
Jan 23, 2015
Messages
2
Hi you all,

Every quarter I receive an Excel file with lots of countries and several sheets with different variables being measured for each country company. What I'm supposed to do with it is to create an Excel file for every country. What I'm doing until now is just deleting it manually, which takes a lot of time.

I uploaded a simple example file. First sheet is the original output structure, usually coming with 20-25 sheets measuring different variables from several companies and countries. In the example, for the sake of simplicity, I just put two countries: the UK and France. Second sheet is what I need to produce, keeping only the UK and deleting France. Of course, I also have to do one file with only France.

I hope I've made myself clear so you can help me with this.

Thanks :)

Example file
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
I've found a solution by a Reddit user. Cross-post works after all...

Code:
Sub Cleaner()
Dim savedel As Boolean
Dim cellcounter As Integer
Dim country As String


country = InputBox("Enter Country to Save")
If country = "" Then Exit Sub


cellcounter = 1


Application.ScreenUpdating = False


Do Until cellcounter > Selection.SpecialCells(xlCellTypeLastCell).Row


    'Ignore deletion of any spacer rows
    If IsEmpty(Range("D" & cellcounter)) = True And IsEmpty(Range("E" & cellcounter)) = True Then
        savedel = 1


        'Ignore heading rows
        ElseIf Len(Range("F" & cellcounter)) > 0 And IsNumeric(Left(Range("F" & cellcounter), 1)) = False Then
            savedel = 1


        'Ignore deletion of the country sought
        ElseIf Range("B" & cellcounter).Value = country Then
            savedel = 1


        'Flag non-country for deletion
        ElseIf Range("B" & cellcounter).Value <> country And IsEmpty(Range("B" & cellcounter).Value) = False Then
            savedel = 0
    End If


    'If flagged, delete row
    If savedel = 0 Then
        Rows(cellcounter).Delete
        cellcounter = cellcounter - 1
    End If


cellcounter = cellcounter + 1


Loop


Application.ScreenUpdating = False


End Sub
 
Upvote 0

Forum statistics

Threads
1,215,509
Messages
6,125,216
Members
449,215
Latest member
texmansru47

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