Help updating this code to sort current region by Column A then Column G?

Coyotex3

Active Member
Joined
Dec 12, 2021
Messages
496
Office Version
  1. 365
Platform
  1. Windows
Hello, I have a code that finds a specific word and delete 3 rows above it(therefore removing blank cells between two similar ranges.) I would like to be able to select the current region and resort. Here is what I have come up with so far. This removes my columns for me but I can't figure out the rest.
VBA Code:
Sub Delete_Rows()
    Range("A" & Rows.Count).End(xlUp).Select
    Const WordToLook = "NYC"
    Dim RowWord As Long
    RowWord = Columns(4).Find(WordToLook, LookAt:=xlWhole).Row
    Rows(RowWord - 3 & ":" & RowWord - 1).Delete
End Sub
 
Should have used XL2BB to begin with. This is a similar format but the ranges will vary in length, but I would like to go from this:

Order Template.xlsx
ABCDEFGHI
1Order RecipientPrimary AddressSecondary AddressCityCity, State, Zip CodePrimary OrderSecondary OrderThird Order
2Jane Doe 1123 Lets Do ItGetting ThereNJ 123 Lets Do it, Getting There, NJ456124512345699
3Jane Doe 2124 Lets Do ItGetting ThereNJ 124 Lets Do it, Getting There, NJ456134512345699
4Jane Doe 3125 Lets Do ItGetting ThereNJ 125 Lets Do it, Getting There, NJ456144512345699
5
6
7
8John Doe 1123 Make it Happen150 We Did ItNew YorkManhattan, NY, 10001125455456875854
9Blake Doe124 Make it Happen151 We Did ItNew YorkManhattan, NY, 10002125465456975854
10John Doe 3125 Make it Happen152 We Did ItNew YorkManhattan, NY, 10003125475457075854
11John Doe 4126 Make it Happen153 We Did ItNew YorkManhattan, NY, 10004125485457175854
12John Doe 5127 Make it Happen154 We Did ItNew YorkManhattan, NY, 10005125495457275854
13John Doe 6128 Make it Happen155 We Did ItNew YorkManhattan, NY, 10006125505457375854
14
15
16
17Ann Doe129 Make it HappenNYCManhattan, NY, 10007125515457475854
18John Doe 8130 Make it HappenNYCManhattan, NY, 10008125525457575854
19John Doe 9131 Make it HappenNYCManhattan, NY, 10009125535457675854
20
Sheet30
[XR][XH][/XH][XH=w:80]A[/XH][XH=w:105]B[/XH][XH=w:94]C[/XH][XH=w:51]D[/XH][XH=w:154]E[/XH][XH=w:71]F[/XH][XH=w:83]G[/XH][XH=w:58]H[/XH][XH=w:48]I[/XH][/XR][XR][XH]8[/XH][XD=h:l|ch:15.75|fz:12pt]Ann Doe[/XD][XD=h:l|fz:12pt]129 Make it Happen[/XD][XD][/XD][XD=h:l|fz:12pt]NYC[/XD][XD=h:l|fz:12pt]Manhattan, NY, 10007[/XD][XD=fz:12pt]12551[/XD][XD=fz:12pt]54574[/XD][XD=fz:12pt]75854[/XD][XD][/XD][/XR][XR][XH]9[/XH][XD=h:l|ch:15.75|fz:12pt]Blake Doe[/XD][XD=h:l|fz:12pt]124 Make it Happen[/XD][XD=h:l|fz:12pt]151 We Did It[/XD][XD=h:l|fz:12pt]New York[/XD][XD=h:l|fz:12pt]Manhattan, NY, 10002[/XD][XD=fz:12pt]12546[/XD][XD=fz:12pt]54569[/XD][XD=fz:12pt]75854[/XD][XD][/XD][/XR][XR][XH]10[/XH][XD=h:l|ch:15.75|fz:12pt]John Doe 1[/XD][XD=h:l|fz:12pt]123 Make it Happen[/XD][XD=h:l|fz:12pt]150 We Did It[/XD][XD=h:l|fz:12pt]New York[/XD][XD=h:l|fz:12pt]Manhattan, NY, 10001[/XD][XD=fz:12pt]12545[/XD][XD=fz:12pt]54568[/XD][XD=fz:12pt]75854[/XD][XD][/XD][/XR][XR][XH]11[/XH][XD=h:l|ch:15.75|fz:12pt]John Doe 3[/XD][XD=h:l|fz:12pt]125 Make it Happen[/XD][XD=h:l|fz:12pt]152 We Did It[/XD][XD=h:l|fz:12pt]New York[/XD][XD=h:l|fz:12pt]Manhattan, NY, 10003[/XD][XD=fz:12pt]12547[/XD][XD=fz:12pt]54570[/XD][XD=fz:12pt]75854[/XD][XD][/XD][/XR][XR][XH]12[/XH][XD=h:l|ch:15.75|fz:12pt]John Doe 4[/XD][XD=h:l|fz:12pt]126 Make it Happen[/XD][XD=h:l|fz:12pt]153 We Did It[/XD][XD=h:l|fz:12pt]New York[/XD][XD=h:l|fz:12pt]Manhattan, NY, 10004[/XD][XD=fz:12pt]12548[/XD][XD=fz:12pt]54571[/XD][XD=fz:12pt]75854[/XD][XD][/XD][/XR][XR][XH]13[/XH][XD=h:l|ch:15.75|fz:12pt]John Doe 5[/XD][XD=h:l|fz:12pt]127 Make it Happen[/XD][XD=h:l|fz:12pt]154 We Did It[/XD][XD=h:l|fz:12pt]New York[/XD][XD=h:l|fz:12pt]Manhattan, NY, 10005[/XD][XD=fz:12pt]12549[/XD][XD=fz:12pt]54572[/XD][XD=fz:12pt]75854[/XD][XD][/XD][/XR][XR][XH]14[/XH][XD=h:l|ch:15.75|fz:12pt]John Doe 6[/XD][XD=h:l|fz:12pt]128 Make it Happen[/XD][XD=h:l|fz:12pt]155 We Did It[/XD][XD=h:l|fz:12pt]New York[/XD][XD=h:l|fz:12pt]Manhattan, NY, 10006[/XD][XD=fz:12pt]12550[/XD][XD=fz:12pt]54573[/XD][XD=fz:12pt]75854[/XD][XD][/XD][/XR][XR][XH]15[/XH][XD=h:l|ch:15.75|fz:12pt]John Doe 8[/XD][XD=h:l|fz:12pt]130 Make it Happen[/XD][XD][/XD][XD=h:l|fz:12pt]NYC[/XD][XD=h:l|fz:12pt]Manhattan, NY, 10008[/XD][XD=fz:12pt]12552[/XD][XD=fz:12pt]54575[/XD][XD=fz:12pt]75854[/XD][XD][/XD][/XR][XR][XH]16[/XH][XD=h:l|ch:15.75|fz:12pt]John Doe 9[/XD][XD=h:l|fz:12pt]131 Make it Happen[/XD][XD][/XD][XD=h:l|fz:12pt]NYC[/XD][XD=h:l|fz:12pt]Manhattan, NY, 10009[/XD][XD=fz:12pt]12553[/XD][XD=fz:12pt]54576[/XD][XD=fz:12pt]75854[/XD][XD][/XD][/XR][XR][XH]17[/XH][XD=ch:15.75][/XD][XD][/XD][XD][/XD][XD][/XD][XD][/XD][XD][/XD][XD][/XD][XD][/XD][XD][/XD][/XR][XR][XH]18[/XH][XD=ch:15][/XD][XD][/XD][XD][/XD][XD][/XD][XD][/XD][XD][/XD][XD][/XD][XD][/XD][XD][/XD][/XR]
I have no idea what you are talking about.
Please explain clearly & succinctly what you are trying to do.
Will do my best! Sorry.
 
Upvote 0

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
Should have used XL2BB to begin with. This is a similar format but the ranges will vary in length, but I would like to go from this:

Order Template.xlsx
ABCDEFGHI
1Order RecipientPrimary AddressSecondary AddressCityCity, State, Zip CodePrimary OrderSecondary OrderThird Order
2Jane Doe 1123 Lets Do ItGetting ThereNJ 123 Lets Do it, Getting There, NJ456124512345699
3Jane Doe 2124 Lets Do ItGetting ThereNJ 124 Lets Do it, Getting There, NJ456134512345699
4Jane Doe 3125 Lets Do ItGetting ThereNJ 125 Lets Do it, Getting There, NJ456144512345699
5
6
7
8John Doe 1123 Make it Happen150 We Did ItNew YorkManhattan, NY, 10001125455456875854
9Blake Doe124 Make it Happen151 We Did ItNew YorkManhattan, NY, 10002125465456975854
10John Doe 3125 Make it Happen152 We Did ItNew YorkManhattan, NY, 10003125475457075854
11John Doe 4126 Make it Happen153 We Did ItNew YorkManhattan, NY, 10004125485457175854
12John Doe 5127 Make it Happen154 We Did ItNew YorkManhattan, NY, 10005125495457275854
13John Doe 6128 Make it Happen155 We Did ItNew YorkManhattan, NY, 10006125505457375854
14
15
16
17Ann Doe129 Make it HappenNYCManhattan, NY, 10007125515457475854
18John Doe 8130 Make it HappenNYCManhattan, NY, 10008125525457575854
19John Doe 9131 Make it HappenNYCManhattan, NY, 10009125535457675854
20
Sheet30


To this

Order Template.xlsx
ABCDEFGHI
8Ann Doe129 Make it HappenNYCManhattan, NY, 10007125515457475854
9Blake Doe124 Make it Happen151 We Did ItNew YorkManhattan, NY, 10002125465456975854
10John Doe 1123 Make it Happen150 We Did ItNew YorkManhattan, NY, 10001125455456875854
11John Doe 3125 Make it Happen152 We Did ItNew YorkManhattan, NY, 10003125475457075854
12John Doe 4126 Make it Happen153 We Did ItNew YorkManhattan, NY, 10004125485457175854
13John Doe 5127 Make it Happen154 We Did ItNew YorkManhattan, NY, 10005125495457275854
14John Doe 6128 Make it Happen155 We Did ItNew YorkManhattan, NY, 10006125505457375854
15John Doe 8130 Make it HappenNYCManhattan, NY, 10008125525457575854
16John Doe 9131 Make it HappenNYCManhattan, NY, 10009125535457675854
17
18
Sheet30
@Fluff I would like for this macro to look at the current region find where column D is NYC remove the three rows above it so now we don’t blank spaces between New York and NYC. Then I need for the macro to select the range where we just removed the blank spaces from in this case “A8:A16” and to sort ONLY those rows by Column A and then G.

I will be back home in about an hour and can post an XL2BB showing The whole range.
 
Upvote 0
Will the 1st row to sort always be row 8, or will it always be from the 2nd block of text?
 
Upvote 0
@Fluff here is the whole picture.

Order Template.xlsx
ABCDEFGHIJ
1Order RecipientPrimary AddressSecondary AddressCityCity, State, Zip CodePrimary OrderSecondary OrderThird Order
2Jane Doe 1123 Lets Do ItGetting ThereNJ 123 Lets Do it, Getting There, NJ456124512345699
3Jane Doe 2124 Lets Do ItGetting ThereNJ 124 Lets Do it, Getting There, NJ456134512345699
4Jane Doe 3125 Lets Do ItGetting ThereNJ 125 Lets Do it, Getting There, NJ456144512345699
5
6
7
8Ann Doe129 Make it HappenNYCManhattan, NY, 10007125515457475854
9Blake Doe124 Make it Happen151 We Did ItNew YorkManhattan, NY, 10002125465456975854
10John Doe 1123 Make it Happen150 We Did ItNew YorkManhattan, NY, 10001125455456875854
11John Doe 3125 Make it Happen152 We Did ItNew YorkManhattan, NY, 10003125475457075854
12John Doe 4126 Make it Happen153 We Did ItNew YorkManhattan, NY, 10004125485457175854
13John Doe 5127 Make it Happen154 We Did ItNew YorkManhattan, NY, 10005125495457275854
14John Doe 6128 Make it Happen155 We Did ItNew YorkManhattan, NY, 10006125505457375854
15John Doe 8130 Make it HappenNYCManhattan, NY, 10008125525457575854
16John Doe 9131 Make it HappenNYCManhattan, NY, 10009125535457675854
17
18
19
Sheet30
 
Upvote 0
Will the 1st row to sort always be row 8, or will it always be from the 2nd block of text?
NYC will always be the last range and New York will always be the second to last range.
 
Upvote 0
So you want to sort from the 2nd block to the end of the sheet?
 
Upvote 0
So you want to sort from the 2nd block to the end of the sheet?
I think a better way of looking at this is that I want to delete the blanks between NYC an New York, which will always be the last two ranges. I might have one that looks like this some day.

Order Template.xlsx
ABCDEFGHIJ
1Order RecipientPrimary AddressSecondary AddressCityCity, State, Zip CodePrimary OrderSecondary OrderThird Order
2Michael Doe 1121 American EagleBostonMA121 American Eagle, Boston, MA451210154045
3Michael Doe 2122 American EagleBostonMA122 American Eagle, Boston, MA451010204010
4Michael Doe 3123 American EagleBostonMA123 American Eagle, Boston, MA451510254085
5
6
7
8Jane Doe 1123 Lets Do ItGetting ThereNJ 123 Lets Do it, Getting There, NJ456124512345699
9Jane Doe 2124 Lets Do ItGetting ThereNJ 124 Lets Do it, Getting There, NJ456134512345699
10Jane Doe 3125 Lets Do ItGetting ThereNJ 125 Lets Do it, Getting There, NJ456144512345699
11
12
13
14Ann Doe129 Make it HappenNYCManhattan, NY, 10007125515457475854
15Blake Doe124 Make it Happen151 We Did ItNew YorkManhattan, NY, 10002125465456975854
16John Doe 1123 Make it Happen150 We Did ItNew YorkManhattan, NY, 10001125455456875854
17John Doe 3125 Make it Happen152 We Did ItNew YorkManhattan, NY, 10003125475457075854
18John Doe 4126 Make it Happen153 We Did ItNew YorkManhattan, NY, 10004125485457175854
19John Doe 5127 Make it Happen154 We Did ItNew YorkManhattan, NY, 10005125495457275854
20John Doe 6128 Make it Happen155 We Did ItNew YorkManhattan, NY, 10006125505457375854
21John Doe 8130 Make it HappenNYCManhattan, NY, 10008125525457575854
22John Doe 9131 Make it HappenNYCManhattan, NY, 10009125535457675854
23
24
25
26
Sheet30


Ideally Still want to leave NJ and MA alone and only focus on New York and NYC so maybe something like if Range("D").value = "New York" or "NYC" then sort? Don't know if it is possible or not?
 
Upvote 0
That goes against what you have said before & is totally different to what you have shown before.
If you cannot give me straight & accurate answers to my questions, then I cannot help.
 
Upvote 0
That goes against what you have said before & is totally different to what you have shown before.
If you cannot give me straight & accurate answers to my questions, then I cannot help.
I really suck at explaining it seems. I will try it once last time and this might help. How can I go from a variable range where New York and NYC will always be at the bottom, delete blank rows between New York and NYC and then Sort.

I want to go from this

Order Template.xlsx
ABCDEFGHI
1Order RecipientPrimary AddressSecondary AddressCityCity, State, Zip CodePrimary OrderSecondary OrderThird Order
2Michael Doe 1121 American EagleBostonMA121 American Eagle, Boston, MA451210154045
3Michael Doe 2122 American EagleBostonMA122 American Eagle, Boston, MA451010204010
4Michael Doe 3123 American EagleBostonMA123 American Eagle, Boston, MA451510254085
5
6
7
8Jane Doe 1123 Lets Do ItGetting ThereNJ 123 Lets Do it, Getting There, NJ456124512345699
9Jane Doe 2124 Lets Do ItGetting ThereNJ 124 Lets Do it, Getting There, NJ456134512345699
10Jane Doe 3125 Lets Do ItGetting ThereNJ 125 Lets Do it, Getting There, NJ456144512345699
11
12
13
14Blake Doe124 Make it Happen151 We Did ItNew YorkManhattan, NY, 10002125465456975854
15John Doe 1123 Make it Happen150 We Did ItNew YorkManhattan, NY, 10001125455456875854
16John Doe 3125 Make it Happen152 We Did ItNew YorkManhattan, NY, 10003125475457075854
17John Doe 4126 Make it Happen153 We Did ItNew YorkManhattan, NY, 10004125485457175854
18John Doe 5127 Make it Happen154 We Did ItNew YorkManhattan, NY, 10005125495457275854
19John Doe 6128 Make it Happen155 We Did ItNew YorkManhattan, NY, 10006125505457375854
20
21
22
23Ann Doe129 Make it HappenNYCManhattan, NY, 10007125515457475854
24John Doe 8130 Make it HappenNYCManhattan, NY, 10008125525457575854
25John Doe 9131 Make it HappenNYCManhattan, NY, 10009125535457675854
26
27
28
Sheet30


To this:

Order Template.xlsx
ABCDEFGHI
1Order RecipientPrimary AddressSecondary AddressCityCity, State, Zip CodePrimary OrderSecondary OrderThird Order
2Michael Doe 1121 American EagleBostonMA121 American Eagle, Boston, MA451210154045
3Michael Doe 2122 American EagleBostonMA122 American Eagle, Boston, MA451010204010
4Michael Doe 3123 American EagleBostonMA123 American Eagle, Boston, MA451510254085
5
6
7
8Jane Doe 1123 Lets Do ItGetting ThereNJ 123 Lets Do it, Getting There, NJ456124512345699
9Jane Doe 2124 Lets Do ItGetting ThereNJ 124 Lets Do it, Getting There, NJ456134512345699
10Jane Doe 3125 Lets Do ItGetting ThereNJ 125 Lets Do it, Getting There, NJ456144512345699
11
12
13
14Ann Doe129 Make it HappenNYCManhattan, NY, 10007125515457475854
15Blake Doe124 Make it Happen151 We Did ItNew YorkManhattan, NY, 10002125465456975854
16John Doe 1123 Make it Happen150 We Did ItNew YorkManhattan, NY, 10001125455456875854
17John Doe 3125 Make it Happen152 We Did ItNew YorkManhattan, NY, 10003125475457075854
18John Doe 4126 Make it Happen153 We Did ItNew YorkManhattan, NY, 10004125485457175854
19John Doe 5127 Make it Happen154 We Did ItNew YorkManhattan, NY, 10005125495457275854
20John Doe 6128 Make it Happen155 We Did ItNew YorkManhattan, NY, 10006125505457375854
21John Doe 8130 Make it HappenNYCManhattan, NY, 10008125525457575854
22John Doe 9131 Make it HappenNYCManhattan, NY, 10009125535457675854
23
24
25
Sheet30


the data needing sorting will always be at the bottom of the excel sheet.

Once again, sorry for the miscommunication on my part. truly appreciate you working with me!
 
Upvote 0
This will sort the last two blocks.
VBA Code:
Sub coyotex()
   Dim UsdRws As Long
   
   UsdRws = Range("A" & Rows.Count).End(xlUp).Row
   With Range("A:A").SpecialCells(xlConstants)
      With Range(.Areas(.Areas.Count - 1), Range("A" & UsdRws)).Resize(, 8)
         .Sort .Range("A1"), xlAscending, .Range("G1"), , xlAscending, Header:=xlNo
      End With
   End With
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,215,061
Messages
6,122,922
Members
449,094
Latest member
teemeren

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