VBA Code that uses CustomOrder to Sort in other Worksheet in the same Workbook

Veni11

New Member
Joined
Oct 20, 2023
Messages
22
Office Version
  1. 365
Platform
  1. Windows
Hii
I have a difficult issue for me as I'm still a bit new to VBA coding.
Here's what I'm trying to solve. I have an Excel template with different sheets. One of these sheets is called "MASTER" and it contains all the article numbers that are available and they are entered in a defined order. In the "Material" sheet there are various item numbers that are randomly distributed in a column, but these item numbers are all contained in the "MASTER" sheet. My goal is to sort the "Material" sheet based on the order in the "MASTER". The code should be structured in such a way that it doesn't matter which article numbers appear in the "Material" sheet, they are then sorted in the column as they are in the "Master". The range is C3:C4000 in the "MASTER" sheet.
In the end, the structure would be the same for each new "Material" sheet, even if not all item numbers are included.

I hope I described it understandable and that someone has an idea :D
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
Hi, two questions:

1. Where (in which column) are the item numbers placed in "Material" sheets?
2. How to differentiate Material sheets? Do you use a distinctive naming scheme?
 
Upvote 0
Hi, two questions:

1. Where (in which column) are the item numbers placed in "Material" sheets?
2. How to differentiate Material sheets? Do you use a distinctive naming scheme?
Hii

1. the item numbers are placed in the column C, further information such as length, width and height is entered in other columns.
2. Yes I'm using a distinctive naming scheme. To perhaps explain it in more detail. The sheets always remain the same, including the name, the only thing that changes are the workbook names and thus the number and different item numbers in the "Material" sheet.

I hope that makes sense to you. I'm sorry because english is not my native language. Thank you for your response!
 
Upvote 0
2. Yes I'm using a distinctive naming scheme. To perhaps explain it in more detail. The sheets always remain the same, including the name, the only thing that changes are the workbook names and thus the number and different item numbers in the "Material" sheet.
I mean do you have distinctive names for material sheets? I mean your tab names look like:
MASTER, Material1, Material2, Material3, Report, RawData..... etc..

I mean is it possible to differentiate them from the other sheets?
I said too many I means :)
 
Upvote 0
I mean do you have distinctive names for material sheets? I mean your tab names look like:
MASTER, Material1, Material2, Material3, Report, RawData..... etc..

I mean is it possible to differentiate them from the other sheets?
I said too many I means :)
Oh yes the tab names look like this:
Material, Temp, Stock, Special sizes, MASTER

So basically the sheets "Temp, Stock, Special sizes" are just filled with some items that you can copy the row and paste it to the sheet "Material" if you need them.

For example the "material" sheet contains the following data:

Column A(number of pieces) Column B(additional) Column C(Item Number)
4 0 S1885-500
7 3 S1885-300
2 2 S1885-2000
5 1 S1885-150
3 3 S2555-750

In the sheet "MASTER" there are plenty more of this item Numbers as like:
S2555-550
S2555-600
S2555-750
S2555-800
S1885-100
S1885-150
S1885-200
S1885-300

and so on. So that in the example above the final screen in the sheet "Material" should look like this after the sortation:
3 3 S2555-750
5 1 S1885-150
7 3 S1885-300
4 0 S1885-500
2 2 S1885-2000

:biggrin:
 
Upvote 0
Aahhh.. I thought you have more than 1 Materials sheet. Then this should work OK:
VBA Code:
Sub test()
  Dim masterArray As Variant, materialArray As Variant, tempArray As Variant
  Dim masterNumbers As Object, a As Object
  Dim i As Long, j As Long, k As Long, c As Long
  Set masterNumbers = CreateObject("Scripting.Dictionary")
  Set a = Application
 
  With Worksheets("MASTER")
  masterArray = a.Transpose(Intersect(.UsedRange, .Columns("C")))
  End With
 
  For i = 2 To UBound(masterArray)
    If Not masterNumbers.Exists(masterArray(i)) Then
      c = c + 1
      masterNumbers.Add masterArray(i), c
    End If
  Next

  With Worksheets("Material")
  materialArray = .UsedRange
  For i = 2 To UBound(materialArray, 1) - 1
    For j = i + 1 To UBound(materialArray, 1)
      If masterNumbers(materialArray(j, 3)) < masterNumbers(materialArray(i, 3)) Then
        tempArray = a.Index(materialArray, i, 0)
        For k = 1 To UBound(materialArray, 2)
          materialArray(i, k) = materialArray(j, k)
          materialArray(j, k) = tempArray(k)
        Next
      End If
    Next
  Next

  .UsedRange.Value = materialArray
End Sub
 
Upvote 0
Aahhh.. I thought you have more than 1 Materials sheet. Then this should work OK:
VBA Code:
Sub test()
  Dim masterArray As Variant, materialArray As Variant, tempArray As Variant
  Dim masterNumbers As Object, a As Object
  Dim i As Long, j As Long, k As Long, c As Long
  Set masterNumbers = CreateObject("Scripting.Dictionary")
  Set a = Application
 
  With Worksheets("MASTER")
  masterArray = a.Transpose(Intersect(.UsedRange, .Columns("C")))
  End With
 
  For i = 2 To UBound(masterArray)
    If Not masterNumbers.Exists(masterArray(i)) Then
      c = c + 1
      masterNumbers.Add masterArray(i), c
    End If
  Next

  With Worksheets("Material")
  materialArray = .UsedRange
  For i = 2 To UBound(materialArray, 1) - 1
    For j = i + 1 To UBound(materialArray, 1)
      If masterNumbers(materialArray(j, 3)) < masterNumbers(materialArray(i, 3)) Then
        tempArray = a.Index(materialArray, i, 0)
        For k = 1 To UBound(materialArray, 2)
          materialArray(i, k) = materialArray(j, k)
          materialArray(j, k) = tempArray(k)
        Next
      End If
    Next
  Next

  .UsedRange.Value = materialArray
End Sub

Do I have to pay attention to anything else if I have headers? Or does the code also work with this, since the article numbers only start from C3 and the title is in C1 & C2. I'll try it in a few minutes.
 
Upvote 0
Then start only i indexes from 3. This should be enough.
Excel has hung up and is still loading "no respond" I will give you feedback on whether it worked as soon as it is finished😅 but anyway thank you very much for your support!
 
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,871
Members
452,363
Latest member
merico17

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