Deleting rows above and below data required

David Wale

New Member
Joined
Sep 17, 2012
Messages
25
Hello, a sub routine required please.

I want to look down column A and find the first instance of either, GK, DF, MF or

ST, (in this example ST is the the first instance at A32, it could have been GK, DF

or MF).

When the first instance has been found I want to delete all the rows above the

first instance, so ST at A32 would now be at A1.

Also, looking down column A, I want to find the last instance of either GK, DF, MF

or ST and delete all the rows after the last instance, (in this example GK is the last

instance at A58).

Thank you for any anticipated assistance

David Wale


A
179,700 Likes
250,703 Followers
3Your Basket £0.00
4Your Tokens (0)
5Log in
6
7The Sun £1 MILLION Dream team - Fantasy Football 2011/2012
8Proud Partner William Hill
9
10
11
12
13
14Home
15Predictor
16Playing & Prizes
17Stat Zone
18Fixtures
19Bet
20FAQ
21
22Player Stats
23Player Stats
24Select a player to view their profile
25Click on the preferred column header to sort the stats
26All
27Goalkeepers
28Defenders
29Midfielders
30Strikers
31POS.
32ST
33ST
34ST
35ST
36ST
37MF
38MF
39MF
40MF
41MF
42MF
43MF
44MF
45MF
46MF
47MF
48DF
49DF
50DF
51DF
52DF
53DF
54DF
55DF
56DF
57GK
58GK
448Back to Stat Zone
449Follow Us
450
451Twitter
452Facebook
453www.thesun.co.uk
454
455Latest Tweets
456dreamteamfc
457Star Man: Eden Hazard #lovemidweekpoints
458dreamteamfc
459Chelsea are slowly making up for abandoning us for Club World Cup #lovemidweekpoints
460dreamteamfc
461RT @daddy_spoon: @dreamteamfc how much is benteke? A must have - £2million. 75 points and counting #thebelgiansaretakingover
462Top FAQ's
4631. Most asked questions
4642. How to play
4653. Team Selection
4664. Scoring
4675. Transfers
4686. Mini-leagues
4697. Prizes
4708. Packages
4719. Payments
47210. Lost details
47311. How to play
474
475
476
477
478
479
480
481
482
483
484
485
486WE ACCEPT THE FOLLOWING CARDS
487
488PAYMENT POWERED BY
489
490Contact us
491T&C's
492Privacy Policy
493Payment Options
494'The Sun', 'Sun' and 'Dream Team' are registered trademarks. All rights reserved. © News Group Newspapers Ltd, 2012/13.
495News Promotions Limited is licensed and regulated by the Gambling Commission (Great Britain) License No. 000-001742-R-104050-001
496By continuing to use the site, you agree to the use of cookies. You can change this and find out more by following this link.
497Accept Cookies

<COLGROUP> <COL style="WIDTH: 30px; FONT-WEIGHT: bold"><COL style="WIDTH: 64px"> </COLGROUP><TBODY>
</TBODY>


Excel tables to the web >> Excel Jeanie HTML 4
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
Hi David,

Is your data in this column always continuous? That is, in the column listing the GK, DF, MF, ST values, are there ever blank cells part way through the values you want to keep? Or, are there values that are different to GK, DF, MF, ST...?
 
Upvote 0
Perhaps this:-
Code:
[COLOR="Navy"]Sub[/COLOR] MG30Dec57
[COLOR="Navy"]Dim[/COLOR] Rng [COLOR="Navy"]As[/COLOR] Range
[COLOR="Navy"]Dim[/COLOR] Dn [COLOR="Navy"]As[/COLOR] Range
[COLOR="Navy"]Dim[/COLOR] nRng [COLOR="Navy"]As[/COLOR] Range
[COLOR="Navy"]Set[/COLOR] Rng = Range(Range("A1"), Range("A" & Rows.Count).End(xlUp))
[COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] Dn [COLOR="Navy"]In[/COLOR] Rng
[COLOR="Navy"]If[/COLOR] IsError(Application.Match(Dn, Array("GK", "DF", "MF", "ST"), 0)) [COLOR="Navy"]Then[/COLOR]
    [COLOR="Navy"]If[/COLOR] nRng [COLOR="Navy"]Is[/COLOR] Nothing [COLOR="Navy"]Then[/COLOR]
        [COLOR="Navy"]Set[/COLOR] nRng = Dn
    [COLOR="Navy"]Else[/COLOR]
        [COLOR="Navy"]Set[/COLOR] nRng = Union(nRng, Dn)
  [COLOR="Navy"]End[/COLOR] If
[COLOR="Navy"]End[/COLOR] If
[COLOR="Navy"]Next[/COLOR]
[COLOR="Navy"]If[/COLOR] Not nRng [COLOR="Navy"]Is[/COLOR] Nothing [COLOR="Navy"]Then[/COLOR] nRng.EntireRow.Delete
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 
Upvote 0
Does this macro do what you want...
Code:
Sub ReduceData()
  Dim LastRow As Long, StartRow As Long, EndRow As Long
  LastRow = Cells(Rows.Count, "A").End(xlUp).Row
  StartRow = Evaluate("MIN(IF(A1:A" & LastRow & "={""GK"",""DF"",""MF"",""ST""},ROW(A1:A" & LastRow & ")))")
  EndRow = Evaluate("MAX(IF(A1:A" & LastRow & "={""GK"",""DF"",""MF"",""ST""},ROW(A1:A" & LastRow & ")))")
  Rows(EndRow + 1 & ":" & Rows.Count).Delete
  If StartRow > 1 Then Rows("1:" & StartRow - 1).Delete
End Sub
 
Upvote 0
David,

Both Rick and Mick's solutions are far superior to mine, however I spent a bit of time writing mine (VBA amateur) so I will post anyway...

:p

If you decide to use my snail-like code, simply change the range (highlighted in red) to suit your data.

Good luck with your football!

Code:
Sub Delete_Rows()


Dim Cell As Range
Dim Rows_To_Delete As Range
Dim Number_of_Rows As Integer


Set Range_To_Check = Range("[COLOR=#ff0000]A1:A200[/COLOR]")


    For Each Cell In Range_To_Check
        
        If Cell.Text <> "ST" And Cell.Text <> "MF" And Cell.Text <> "DF" And Cell.Text <> "GK" Then
                        
            If Rows_To_Delete Is Nothing Then
                Set Rows_To_Delete = Cell
            Else
                Set Rows_To_Delete = Union(Rows_To_Delete, Cell)
            End If
            
        End If
    
    Next
    
        Rows_To_Delete.Select
        Selection.EntireRow.Delete


End Sub
 
Last edited:
Upvote 0
Firstly I would like to wish you all a Happy New Year.

Secondly, I would like to thank MickG, Rick Rothstein and Novelec for the exact solution I required, and the interest shown by

Patel45.

Since joining Mr. Excel forum in the summer, I have asked for a number of solutions to problems, which have always been

answered.


So, thanks to all of you who have made my spreadsheet activities more rewarding by the week.


Yours cordially


David Wale
 
Upvote 0

Forum statistics

Threads
1,216,073
Messages
6,128,634
Members
449,460
Latest member
jgharbawi

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