Macro to sort two different tables in same sheet

ExlN00b

New Member
Joined
Nov 6, 2021
Messages
5
Office Version
  1. 365
Platform
  1. Windows
I have a sheet with two separate tables - need to sort them by Column B, separately. The VBA I have combines the two tables and sorts them into one table.

Need:
  1. Sort Column B from "1" to the last number in the set (example: Table 1 from 1-6; Table 2 from 1-4)
  2. The only constant is "1" to indicate the start of the table
Any assistance or pointing to the right direction would be great!

Sub PO_CSV_Multi()
'
' PO_CSV_Multi Macro
'
Columns("E:F").Select
Selection.Delete Shift:=xlToLeft
Columns("I:J").Select
Selection.ColumnWidth = 11.38

Range("B18", Range("J" & Rows.count).End(xlUp).Address).Sort Key1:=[b3], _
Order1:=xlAscending, Header:=xlNo

End Sub
 

Attachments

  • MacroPO.JPG
    MacroPO.JPG
    163.5 KB · Views: 47

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
you need to paste the data that can be copied here for users to assist you further , Its difficult to give solution where data is in image
 
Upvote 0
I may be wrong but I see many postings where users refer to "Tables"
But in this post and the image I see no reference to Tables.

A Table from my understand is a Range on a sheet.
And the Table has a name.
Are you sure you have a Table here?
 
Upvote 0
I may be wrong but I see many postings where users refer to "Tables"
But in this post and the image I see no reference to Tables.

A Table from my understand is a Range on a sheet.
And the Table has a name.
Are you sure you have a Table here?
Never mind . From the picture it seems like a range to me . but you are referring to table . Maybe someone else can assist you
 
Upvote 0
Sorry! The "table" I was referring to was the label to indicate two different sets of ranges to sort.

I'm uploading the mini-sheet and attempting better details:
  1. "Order Number: 1" and "Order Number: 2" are separate, but are exported to the same CSV file
    1. "Order Number: 1" and "Order Number: 2" are found in G1 and G35
  2. Under "Order Number: 1" I would like to sort by the numerals in Column B in order to condense and remove extraneous lines for easy copy and paste
    1. This will bring 1; 2; 3; and 4 together under "Order Number: 1"
  3. Separately, I would like to do the same as #2 for "Order Number: 2"
    1. Bringing 1; 2; 3; 4; 5; and 6 together under "Order Number: 2"
  4. Is it possible to create a Macro that does both #2 and #3 consecutively so I don't have to do it for each separate order?
    1. Something like start in B18; sort rows 18 - 25
    2. Search for next instance of "1" in Column B; then sort rows 52 - 65
    3. Search for next instance of "1" in Column B; if none; stop

My current VBA allows me to do #2 only if I have one "Order Number" in the sheet. If I have more than one "Order Number", it will sort both "Order Numbers" together and consolidate into a singular sorted range. I would like to keep both "Orders" separate.

Details:
  1. The one constant is B18 is always the first instance of "1" in Column B
  2. The number of rows to be sorted within each "Order Number" is not constant - it changes depending on the number of items under the "Order Number"
  3. There is no other information in Column B except for the numbers to be sorted
Hoping this helps - thank you!

Sort_Different_Ranges.csv
ABCDEFGHIJ
1Order Number :1
2Delivery Date :10/3/2022
3
4Order Date: 10/21/2021
5Phone :
6Fax :
7
8PO Created By :
9Email :
10Bill To :F.O.B :
11Ship Via :
12Payment Terms :
13Supplier A/C No. :
14
15Phone :
16Fax :
17Line Product #Product DescriptionBrandVendor Prd. No.Order QtyPurchase UnitPriceCost
181101034TEST PRODUCT ABRAND A12345649CASE [18 Each]$10.00$490.00
19Detail a
20Detail b
21278275Freight ChargesFreight1Each$0.00$0.00
22Each
23Freight Charge
24310238171DEPOSIT1Each$2,033.76$2,033.76
25410238172DEPOSIT OFFSET-1Each$2,033.76($2,033.76)
26Net Purchase Order Amount$490.00
27Sales Tax | TAX$31.85
28Discount$0.00
29NO RATE| MISCELLANEOUS$0.00
30PO Total Amount$521.85
31
32
33
34
35Order Number :2
36Delivery Date :3/7/2022
37
38Order Date: 10/27/2021
39Phone :
40Fax :
41
42PO Created By :
43Email :
44Bill To :F.O.B :
45Ship Via :
46Payment Terms :
47Supplier A/C No. :
48
49
50
51Line Product #Product DescriptionBrandVendor Prd. No.Order QtyPurchase UnitPriceCost
521187244TEST PRODUCT BBRAND B1234566Each$10.00$60.00
53Detail c
54Detail d
552181196TEST PRODUCT CBRAND C12345620Each$10.00$200.00
56Detail e
57Detail f
583181196TEST PRODUCT DBRAND D12345620Each$10.00$200.00
59Detail g
60Detail h
61478275Freight ChargesFreight1Each$245.40$245.40
62Each
63Freight Charge
64510239495PRE-PAYMENT1Each$1,995.92$1,995.92
65610239496PRE-PAYMENT OFFSET-1Each$1,995.92($1,995.92)
66Net Purchase Order Amount$705.40
67Sales Tax | TAX$45.85
68Discount$0.00
69NO RATE| MISCELLANEOUS$0.00
70PO Total Amount$751.25
Sort_Different_Ranges
Cell Formulas
RangeFormula
J18,J58,J55,J52J18=G18*I18
J26J26=SUM(J18:J23)
J27,J67J27=J26*0.065
J30,J70J30=SUM(J26:J29)
J66J66=SUM(J52:J63)
 
Upvote 0
VBA is more difficult for this type . I think formula approach is much easier . How many max order numbers can be there on sheet.
 
Upvote 0
VBA is more difficult for this type . I think formula approach is much easier . How many max order numbers can be there on sheet.
There is no max order numbers. I’m interested in what you mean by “formula”. There wouldn’t be a VBA to find the next “1” in Column B and sort that set of lines? Anything that will make this an easier process than selecting each set of lines for each order number separately. Thank you so much!
 
Upvote 0
I can create template from where you can dump and and then get result from another sheet.
 
Upvote 0
That would be so great! Thank you so so much for your help!!
 
Upvote 0
I can create template from where you can dump and and then get result from another sheet.
Hello! Checking if you’re able to assist me with the template to be able to format and sort different sections separately? Thank you so much!
 
Upvote 0

Forum statistics

Threads
1,215,633
Messages
6,125,929
Members
449,274
Latest member
mrcsbenson

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