VBA - lookup two values

MartinL

Well-known Member
Joined
Oct 16, 2008
Messages
1,141
Office Version
  1. 365
Platform
  1. Windows
Here we go again!
I have a sheet that has been created by a database and looks ugh!
I am trying to run some vba over it daily so it is more meaningful.

This is the db export:
Excel Workbook
ABCDEFGHI
1A&B Sales Orders
2PREMIUM
3
425-Nov-0926-Nov-0927-Nov-0928-Nov-09Total
5Trays90.0041.0098.0096.00325.00
6A&B (CUMBERNAUTrays412030091
7A&B (CREWE)Trays282133082
8A&B (BARNSLEY)Trays411837096
9A&B (THATCHAM)Trays361837091
10A&B (BRISTOL)Trays22822052
11A&B (FAVERSHAMTrays6222580142
12A&B (HEMEL)Trays6025620147
13A&B (IRELAND)Trays10813031
14ActualTotalTrays300140292-732
15A&B (CUMBERNAUTrays0003434
16A&B (CREWE)Trays0002323
17A&B (BARNSLEY)Trays0003333
18A&B (THATCHAM)Trays0002626
19A&B (BRISTOL)Trays0002222
20A&B (FAVERSHAMTrays0004747
21A&B (HEMEL)Trays0004545
22A&B (IRELAND)Trays0001010
23ProgramTotalTrays---240240
24AB Extra Fine Asp 125g X008TotalTrays300.00140.00292.00240.00972.00
253617TotalTrays300140292240972
26Trays300.00140.00292.00240.00972.00
27A&B (MAYLANDS)Trays7900079
28ActualTotalTrays79---79
29AB Fine Beans 165g X012TotalTrays79.000.000.000.0079.00
Sheet1



sorry its quite big!

This is what I want to produce from that data:
Excel Workbook
MNOPQRSTUVW
8UID25-Nov-0926-Nov-0927-Nov-0928-Nov-0925-Nov-0926-Nov-0927-Nov-0928-Nov-09
93617Actual300140292-Program---240
1010541Actual79---
Sheet1


As you can see the db puts the dates in the wrong column but I rectify this in my end output

Thsi is what I have so far until i realised I was pulling in the wron row of data, now I am stuck!

Code:
Sub GetSalesData()
Dim TestForUID As Range
Dim TestForSaleType As Range
Dim UID As Range
Dim SaleType As Range
Dim Flag As Boolean
Dim HostInfo As Range
Dim Host As String
Dim wbThis As Workbook
Dim Lastrow1 As Long
Dim Lastrow2 As Long
Dim a As Long
Dim b As Long
Dim c As Long
Dim d As Long
Dim ArrivalDate As String
Dim txtString As String
Dim TestRange As String

Set wbThis = ThisWorkbook
a = 1
b = 1
'unhide worksheet
Sheets("SALESDATA").Visible = True
'Set start of paste range
Sheets("SALESDATA").Select
Range("A" & a).Select
'calculate last row for clean up
With ActiveSheet
    Lastrow1 = .Cells(Rows.Count, "A").End(xlUp).Row
End With

Workbooks.Open Filename:="L:\Commercial Sales Summary\ab.xls"

'find last row of Sales
With ActiveSheet
    Lastrow2 = .Cells(Rows.Count, "A").End(xlUp).Row
End With
'Get UID
With ActiveSheet
    Set TestForUID = .Range("A1:A" & Lastrow2 - 4)
End With
Range("A" & b).Select
For Each UID In TestForUID.Cells
    If UID.Value <> "" And IsNumeric(UID.Value) Then
    
        'Get Sales Data
        Intersect(ActiveCell.EntireRow, Columns("A:H")).Copy
        wbThis.Activate
        Sheets("SALESDATA").Select
        Selection.PasteSpecial Paste:=xlPasteValues
        
        'Get Description
        Range("B" & a).Select
        ActiveWindow.ActivatePrevious
        Range("A" & b - 1).Copy
        wbThis.Activate
        Sheets("SALESDATA").Select
        Selection.PasteSpecial Paste:=xlPasteValues
        
        'set cursor to next line
        a = a + 1
        Range("A" & a).Select
        ActiveWindow.ActivatePrevious
    End If
b = b + 1
Range("A" & b).Select
Next UID
ActiveWorkbook.Close False
'cleanup
Range("A" & a).Select
For c = a To Lastrow1
    Intersect(ActiveCell.EntireRow, Columns("A:AJ")).ClearContents
    a = a + 1
    Range("A" & a).Select
Next c
'Hide worksheet
Sheets("SALESDATA").Visible = False
End Sub

anyone interested in getting their hands dirty
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
OK heres what I have done, it now works but if there is a neater way cool...
Code:
Sub GetSalesData()
Dim TestForUID As Range
Dim TestForSaleType As Range
Dim UID As Range
Dim SaleType As Range
Dim Flag As Boolean
Dim HostInfo As Range
Dim Host As String
Dim wbThis As Workbook
Dim Lastrow1 As Long
Dim Lastrow2 As Long
Dim a As Long
Dim b As Long
Dim c As Long
Dim d As Long
Dim ArrivalDate As String
Dim txtString As String
Dim TestRange As String
 
Set wbThis = ThisWorkbook
a = 2
b = 1
d = 1
 
'unhide worksheet
Sheets("SALESDATA").Visible = True
'Set start of paste range
Sheets("SALESDATA").Select
Range("A" & a).Select
'calculate last row for clean up
With ActiveSheet
    Lastrow1 = .Cells(Rows.Count, "A").End(xlUp).Row
End With
 
Workbooks.Open Filename:="L:\Commercial Sales Summary\ms.xls"
'Get date headers
Range("D4").Select
Intersect(ActiveCell.EntireRow, Columns("D:G")).Copy
wbThis.Activate
Sheets("SALESDATA").Select
Range("C1").Select
Selection.PasteSpecial Paste:=xlPasteValues
ActiveWindow.ActivatePrevious
 
'find last row of Sales
With ActiveSheet
    Lastrow2 = .Cells(Rows.Count, "A").End(xlUp).Row
End With
'Get UID
With ActiveSheet
    Set TestForUID = .Range("A1:A" & Lastrow2 - 4)
End With
Range("A" & b).Select
For Each UID In TestForUID.Cells
    If UID.Value <> "" And IsNumeric(UID.Value) Then
            Range("A" & b).Copy
            wbThis.Activate
            Sheets("SALESDATA").Select
            Range("A" & a).Select
            Selection.PasteSpecial Paste:=xlPasteValues
            ActiveWindow.ActivatePrevious
        'Get Active Sales Data
            With ActiveSheet
                Set TestForSaleType = .Range("B" & d & ":B" & b)
            End With
            For Each SaleType In TestForSaleType.Cells
            If SaleType.Value = " Actual" Then
                Range("B" & d).Select
                Intersect(ActiveCell.EntireRow, Columns("E:H")).Copy
                wbThis.Activate
                Sheets("SALESDATA").Select
                Range("C" & a).Select
                Selection.PasteSpecial Paste:=xlPasteValues
                ActiveWindow.ActivatePrevious
        'Get Program Sales Data
            Else
                If SaleType.Value = " Program" Then
                Range("B" & d).Select
                Intersect(ActiveCell.EntireRow, Columns("E:H")).Copy
                wbThis.Activate
                Sheets("SALESDATA").Select
                Range("G" & a).Select
                Selection.PasteSpecial Paste:=xlPasteValues
                ActiveWindow.ActivatePrevious
            Else
                'nothing
                End If
            End If
            d = d + 1
        Next SaleType
 
        'Get Description
            wbThis.Activate
            Sheets("SALESDATA").Select
            Range("B" & a).Select
            ActiveWindow.ActivatePrevious
            Range("A" & b - 1).Copy
            wbThis.Activate
            Sheets("SALESDATA").Select
            Selection.PasteSpecial Paste:=xlPasteValues
 
        'set cursor to next line
        a = a + 1
        Range("A" & a).Select
        ActiveWindow.ActivatePrevious
    End If
b = b + 1
Range("A" & b).Select
Next UID
ActiveWorkbook.Close False
'cleanup
Range("A" & a).Select
For c = a To Lastrow1
    Intersect(ActiveCell.EntireRow, Columns("A:AJ")).ClearContents
    a = a + 1
    Range("A" & a).Select
Next c
'Hide worksheet
Sheets("SALESDATA").Visible = False
End Sub
 
Upvote 0
Hi Martin

I tried to help but this is difficult to unfathom for an outsider and I gave up before I got far. Could I make a couple of general coding suggestions?

As far as I can see, you are working with the Rawdata input and then moving your cleaned up data to a Summary sheet. You seem to be using a worksheet "SALESDATA". These may not be the best names but try something this:

Dim ShRawData as worksheet
Dim ShSummary as worksheet
Dim ShSalesData as worksheet

set ShRawData = ActiveSheet
workbooks("X").Open
Set ShSummary = Workbooks("X").worksheets("Sheet1")

Then you can move data backwards and forwards without flipping between active/previous windows (which is really confusing to read).

ShSalesData.cells(x,y) = ShRawData.cells(x,z)

Note: no copy/paste and flipping windows.

Another note: I see that at the bottom of the code, you are varying 'a' but above that a,b,c etc are constant, yet you repeatedly use "A" & b rather than "A1", "A2". The latter is far easier to read.

Hope this is helpful.
 
Upvote 0

Forum statistics

Threads
1,214,965
Messages
6,122,500
Members
449,090
Latest member
RandomExceller01

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