Multiple look up in column to give a result from a choice of 3 variables

Mpotten

New Member
Joined
Apr 2, 2015
Messages
12
HI All i am relatively new to VBA but have been an excel use for many years

I need to write a macro that will search a specific column of data labelled customer and under a separate column input the reps name


CustomerSales Rep
Customer 1
Rep A
Customer 2
Rep B
Customer 3Rep C
Customer 4Rep A
Customer 5Rep C
Customer 6Rep B
and so onand so on

<tbody>
</tbody>


<colgroup><col><col></colgroup><tbody>
</tbody>
The example above can be as long as 500 lines some time longer. I would like to associate the right customer with the right Sales Rep so that when I run my clean up macro the data is peeped quickly for inputing into my Monthly sales report.

Currently I am using data validation and Lookup to do this but have to rest the data table each month. A simple active worksheet macro would be much more efficient.

Many thanks

Mark

Below is the macro I am in the process of putting together by virtue of finding something suitable online and then adapting for my needs as I am just starting to learn VBA. I am sure that with time this can be cleaned mu and made to be much more eloquent :)

Sub Sales_Data_Cleanup()


ActiveSheet.UsedRange.Select
'Deletes the entire row within the selection if the ENTIRE row contains no data.

Dim i As Long
'Turn off calculation and screenupdating to speed up the macro.

With Application
.Calculation = xlCalculationManual
.ScreenUpdating = False
'Work backwards because we are deleting rows.

For i = Selection.Rows.Count To 1 Step -1
If WorksheetFunction.CountA(Selection.Rows(i)) = 0 Then
Selection.Rows(i).EntireRow.Delete
End If

Next i
.Calculation = xlCalculationAutomatic
.ScreenUpdating = True
End With
End Sub


Sub Delete_Lotnumber_Evaluation()


Dim ws As Worksheet
Dim rng As Range
Dim lastRow As Long


Set ws = ActiveWorkbook.ActiveSheet


lastRow = ws.Range("C" & ws.Rows.Count).End(xlUp).Row


Set rng = ws.Range("C1:C" & lastRow)
' filter and delete all but header row

With rng
.AutoFilter Field:=1, Criteria1:=".Lot Number"
.Offset(1, 0).SpecialCells(xlCellTypeVisible).EntireRow.Delete
End With
' turn off the filters

ws.AutoFilterMode = False

Set ws = ActiveWorkbook.ActiveSheet


lastRow = ws.Range("C" & ws.Rows.Count).End(xlUp).Row


Set rng = ws.Range("C1:C" & lastRow)
' filter and delete all but header row

With rng
.AutoFilter Field:=1, Criteria1:=".Evaluation"
.Offset(1, 0).SpecialCells(xlCellTypeVisible).EntireRow.Delete
End With
' turn off the filters

ws.AutoFilterMode = False

End Sub

Sub Add_Rep_Name()


Dim customer As Range, result As String

Set customer = Range("B1:B63")

If customer = Array("Adelaide Pathology Partners", "Alfred Health", "Austin Health", "Clinipath Pathology", "Department of Health Government of WA", "Dorevitch Pathology Primary Health", "Healthscope Pathology", "Melbourne Health", "Monash Health", "Melbourne Path Services Pty Ltd", "SA Pathology", "St Vincents Hospital Melbourne Ltd", "The Royal Children's Hospital", "Western Diagnostic Pathology", "Western Regional Central Supply") Then
result = "Victoria"




End If


Range("c2").Value = result

End Sub
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
CustomerSales RepDim n(6), m(2000)
Customer 1Rep A tot = 29
Customer 2Rep B k = 1
Customer 3Rep C n(1) = "Rep A"
Customer 4Rep A n(2) = "Rep B"
Customer 5Rep C n(3) = "Rep C"
Customer 6Rep B n(4) = "Rep D"
Customer 7Rep D n(5) = "Rep E"
Customer 8Rep A50 For j = 2 To 26
Customer 9Rep B If Cells(j, 2) = n(k) Then Sum = Sum + 1: m(Sum) = Cells(j, 1)
Customer 10Rep C Next j
Customer 11Rep A tot = tot + 1
Customer 12Rep C Cells(tot, 1) = n(k)
Customer 13Rep B For x = 2 To Sum + 1
Customer 14Rep D Cells(tot, x) = m(x - 1)
Customer 15Rep E Next x
Customer 16Rep A k = k + 1
Customer 17Rep C z = z + 1
Customer 18Rep B If z > 6 Then GoTo 100
Customer 19Rep D Sum = 0
Customer 20Rep E GoTo 50
Customer 21Rep A100 End Sub
Customer 22Rep B
Customer 23Rep C
Customer 24Rep Athis simple macro lists the customers associated with each rep
Customer 25Rep C(there is a subscript out of range message after it runs so needs a small tweak0
Rep ACustomer 1Customer 4Customer 8Customer 11Customer 16Customer 21Customer 24
Rep BCustomer 2Customer 6Customer 9Customer 13Customer 18Customer 22
Rep CCustomer 3Customer 5Customer 10Customer 12Customer 17Customer 23Customer 25
Rep DCustomer 7Customer 14Customer 19
Rep ECustomer 15Customer 20

<colgroup><col><col><col><col><col span="2"><col><col><col><col></colgroup><tbody>
</tbody>
 
Upvote 0
HI oldbrewer and thanks for the reply

I am not wanting to reorganise the layout of the data as there is other columns associated with the customers such as product and value of sale. sorry for any misunderstanding

I need the macro to just add the sales reps name in a new column at the end of the row.

CustomerDateItem NumberQuantityDescriptionPriceSales Rep

<colgroup><col><col><col><col><col><col span="2"></colgroup><tbody>
</tbody>


Each of these is a column heading and under the customer there will be multiple entries for the same customer i.e. 10 for customer A 30 for customer B etc

I need the macro to look at the customer name and under the column heading Sales rep add the appropriate rep name who looks after that account.
 
Upvote 0
customerquantityprice
cust1172
cust2253this table is "mytable"
cust3334
cust1415cust1RepA
cust2496cust2RepB
cust3577cust3RepC
cust1658cust4RepD
cust2739
cust38110
cust18911
cust29712
cust310513
cust411314
customerquantityprice
cust1172RepA########
cust2253RepB
cust3334RepC
cust1415RepA
cust2496RepB#######
cust3577RepC
cust1658RepA=VLOOKUP(A20,mytable,2)
cust2739RepB
cust38110RepC
cust18911RepA
cust29712RepB
cust310513RepC
cust411314RepD

<colgroup><col width="64" span="15" style="width:48pt"> </colgroup><tbody>
</tbody>
 
Upvote 0
How do I add a file to a thread this would make things much easier to discuss what I am trying to do by showing real data
 
Upvote 0
How do I add a file to a thread this would make things much easier to discuss what I am trying to do by showing real data

This first macro I found on line and adapted so that the blank rows in the raw data are removed making it easier to work with
Code:
Sub Sales_Data_Cleanup()

    ActiveSheet.UsedRange.Select
    'Deletes the entire row within the selection if the ENTIRE row contains no data.
    
    Dim i As Long
    'Turn off calculation and screenupdating to speed up the macro.
    
    With Application
    .Calculation = xlCalculationManual
    .ScreenUpdating = False
    'Work backwards because we are deleting rows.
    
    For i = Selection.Rows.Count To 1 Step -1
    If WorksheetFunction.CountA(Selection.Rows(i)) = 0 Then
    Selection.Rows(i).EntireRow.Delete
    End If
    
    Next i
    .Calculation = xlCalculationAutomatic
    .ScreenUpdating = True
    End With
End Sub

This Second Macro then removes the line items that have .LOT Number and .Evaluation in the item description
Code:
Sub Delete_Lotnumber_Evaluation()


    Dim ws As Worksheet
    Dim rng As Range
    Dim lastRow As Long


    Set ws = ActiveWorkbook.ActiveSheet


    lastRow = ws.Range("C" & ws.Rows.Count).End(xlUp).Row


    Set rng = ws.Range("C1:C" & lastRow)
    ' filter and delete all but header row
    
    With rng
        .AutoFilter Field:=1, Criteria1:=".Lot Number"
        .Offset(1, 0).SpecialCells(xlCellTypeVisible).EntireRow.Delete
    End With
    ' turn off the filters
    
    ws.AutoFilterMode = False
    
    Set ws = ActiveWorkbook.ActiveSheet


    lastRow = ws.Range("C" & ws.Rows.Count).End(xlUp).Row


    Set rng = ws.Range("C1:C" & lastRow)
    ' filter and delete all but header row
    
    With rng
        .AutoFilter Field:=1, Criteria1:=".Evaluation"
        .Offset(1, 0).SpecialCells(xlCellTypeVisible).EntireRow.Delete
    End With
    ' turn off the filters
    
    ws.AutoFilterMode = False
    
    End Sub

AS I mentioned right at the beginning of my thread I am not good at VBA and I am only just starting to learn by finding appropriate code and modifying it to my needs. Is there a need way to combine the two macros above into a nice single macro?

I then need to work on a another macro which i would like to add into the ones above so that each customer is assigned to the correct sales rep. Each of my 3 reps has around 15 customers that they look after. My data tables can be as much at 1000 lines with multiple purchases for each customer thought the month.
 
Upvote 0

Forum statistics

Threads
1,215,472
Messages
6,125,004
Members
449,203
Latest member
Daymo66

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