macro to add a dispatch number to the relevant rows in a column

tonywatsonhelp

Well-known Member
Joined
Feb 24, 2014
Messages
3,194
Office Version
  1. 365
  2. 2019
  3. 2016
Platform
  1. Windows
Hi Everyone,
hope i can make this clear :)
what i need is a macro to assign dispatch number from one sheet into column AN of another.

So in Sheet "Numbers" column A i have a list of values (Dispatch numbers), each value is unique, for simplicity lets say they are A,B,C,D etc

now in sheets "overviews" Column K i have my order numbers.
now what i want is a macro that can start at Sheet "overviews" Column AN find first empty cell, look at column K and see what the order number is, goto sheet "Numbers" cell B2 to get the dispatch number and add it to Column AN
now the tricky bit, there are often but not always more than one row with the same Order Number, so now i need the macro to go down column K find every row that has that same order number and place the dispatch number in column AN of that row.
once they are all done delete sheet "Numbers" cell B2 moving the next dispatch number up into b2, then do it again and again until all rows have dispatch numbers
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
It would be easier to help if you could use the XL2BB add-in (icon in the menu) to attach a screenshot (not a picture) of both sheets. Explain in detail what you want to do referring to specific cells, rows, columns and sheets using a few examples from your data (de-sensitized if necessary). Alternately, you could upload a copy of your file to a free site such as www.box.com or www.dropbox.com. Once you do that, mark it for 'Sharing' and you will be given a link to the file that you can post here.
 
Upvote 0
Hi Mumps,
My office computer wont let me do anything like that, i've tried before.
amybe its easier if if put it like this,

Maybe i've made it more complex than it needs be?

Ignore the words above how about this, All I require is a macro to do this

for every order number in sheet "overviews" Column K, assign a code from Sheet "Numbers" column A starting in cell A2 and put it in column AN,

so it looks like this:

rowcolumnsKANAN
Sheet Overvieworder numbersdispatch numbers dispatch numbers to be filled innotes
1112345AA1so
1212345AA1starting with sheet "Numbers" CELL B2" GIVE A DISPATCH NUMBER to every
1312345AA1order number in column K
1413BB2the dispach numbers should be the same for the same order numbers
1513BB2
1615CC3
1712DD4
1815CC3
1911EE5
2011EE5
2111EE5
22123FF6
23123FF6if i can just get a way to fill in like this i can do the rest.

sheet Numbersdispatch numbers
ABCD
2AA1
3BB2
4CC3
5DD4
6EE5
7 etcFF6
 
Upvote 0
The order numbers in column K of Overview don't match the order numbers in column A of Numbers.
 
Upvote 0
Could you try this?

VBA Code:
Sub Macro2()

    Sheets("Overviews").Select
    
' Cell AN2 is always the first route, so always 1.
    Range("AN2").Select
    ActiveCell.FormulaR1C1 = "1"
    
' From here on a formula will calculate the sequence number.
' It first checks if the order number was already used previously.
' If so it will use a VLOOKUP to determine the previously assigned
' sequence number.
' If the order number is new, the next sequence number is allocated.
' It does to by taking the last previous sequence number (MAX formula)
' and adding +1 to it.
    Dim lastRow As Long
    With Sheets("Overviews")
        lastRow = .Cells(.Rows.Count, "K").End(xlUp).Row
        .Range("AN3:AN" & lastRow).Formula = "=IF(COUNTIFS(R1C11:R[-1]C11,RC11)=0,MAX(R1C40:R[-1]C40)+1,VLOOKUP(RC11,R1C11:R[-1]C40,30,FALSE))"
    End With

' The formula is removed by copy/paste as values. This way you are left
' with sequence numbers 1, 2, 3 etc.
    Range("AN2").Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.Copy
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
    Application.CutCopyMode = False

' Now the sequence numbers are replaced using a INDEX formula.
' Note that you did no specify if the contents of worksheet NUMBERS
' contains a header row. This formula assumes NO header row is present.
' If there IS a header row, then add "+1" between [DESPATCH] and [&].

    Dim cell As Range
    For Each cell In Selection
    Dim despatch As Long
    despatch = ActiveCell
            cell = "=Index(Numbers!$B:$B, " & despatch & ", 1)"
    ActiveCell.Offset(1, 0).Select
    Next cell

' The formula is removed by copy/paste as values. This way you are left
' with the desired despatch numbers.
    Range("AN2").Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.Copy
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
    Application.CutCopyMode = False

 
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,020
Messages
6,122,709
Members
449,093
Latest member
Mnur

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