VBA Help to compare data and return result if match found

Emmily

Well-known Member
Joined
Oct 5, 2008
Messages
676
Hi

I have a work task which I want to automate via VBA. I want the code to first clear the data in Col K "ControlSheet" and then do a lookup by looking at Col I and J in Sheet "ControlSheet" and compare this to the concatenated data in Col D in Sheet "HiportData" if match found then return Holding amount in Col K based on the holding shown in Col I "HiportData". If match not found then return 0 rather than #N/A. Please note there will be trailing spaces in the data, so the code will need to remove these first.

Real data will be around 1500 rows.

Excel Workbook
IJK
1Security CodePfolio CodeHiport Holding
2EQI0005HRISII15636.36
3AMPSCFCSFAUP
4MFAFS004N4S15X
5MFAFS005WSRAPP
ControlSheet


Excel Workbook
DI
1UUTIDHolding
2MFAFS004N4S15X 1,000.00
3EQI0005HRISII 15,636.36
4MFAFS005WSRAPP 98,784.00
HiportData





Thanks in advance :)
 
Emmily,

Now we have all the bugs out.

After the macro:


Excel 2007
IJK
1Security CodePfolio CodeHiport Holding
2EQI0005 HRISII 15,636.36
3AMPSCFCSFAUP0
4MFAFS004N4S15X1,000.00
5MFAFS005WSRAPP98,784.00
6
ControlSheet


Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).

Code:
Sub GetHiportHolding_V3()
' hiker95, 11/15/2014, ME82838
Dim wc As Worksheet, wh As Worksheet
Dim c As Range, u As Range, h As String, lr As Long
Application.ScreenUpdating = False
Set wc = Sheets("ControlSheet")
Set wh = Sheets("HiportData")
With wc
  lr = .Cells(Rows.Count, "K").End(xlUp).Row
  If lr > 1 Then .Range("K2:K" & lr).ClearContents
  For Each c In .Range("I2", .Range("I" & Rows.Count).End(xlUp))
    h = Trim(c.Value) & Trim(c.Offset(, 1).Value)
    Set u = wh.Columns(4).Find(h, LookAt:=xlWhole)
    If u Is Nothing Then
      With c.Offset(, 2)
        .Value = 0
        .NumberFormat = "0"
        .IndentLevel = 0
      End With
    ElseIf Not u Is Nothing Then
      With c.Offset(, 2)
        .Value = wh.Cells(u.Row, 9).Value
        .NumberFormat = "#,##0.00"
        .IndentLevel = 0
      End With
      Set u = Nothing
    End If
  Next c
End With
wc.Activate
Application.ScreenUpdating = True
End Sub

Before you use the macro with Excel 2007 or newer, save your workbook, Save As, a macro enabled workbook with the file extension .xlsm

Then run the GetHiportHolding_V3 macro.
 
Upvote 0

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
Emmily,

Thanks for the feedback.

You are very welcome. Glad I could help.

And, come back anytime.
 
Upvote 0
Certainly your choice about which code does the job and perhaps which one you might feel you could later best maintain if needed, but just noting that provided mine gives the correct values* it is about 10 times faster (as well as being considerably shorter).

* Mine adds all values in col I of HiportData where the col D value matches the two joined values from ControlSheet. So, if there are multiple matching rows, mine would give a different result to hikers code.
If there could be multiple matching rows and you only want the value from the first such matching row, I could suggest alternative code nearly as fast as the existing code.

At least you have choices. :)
 
Upvote 0
Hi Peter,

I have used your code due to being it much shorter and faster.

When you say multiple matching rows, do you mean duplicates?
 
Upvote 0
When you say multiple matching rows, do you mean duplicates?
I mean if HiportData was like below, then a "MFAFS004N4S15X" row on ControlSheet would show 3,000 in col K. Hiker's code would show 1,000.

Excel Workbook
DEFGHI
1UUTIDHolding
2MFAFS004N4S15X1,000.00
3EQI0005HRISII15,636.36
4MFAFS005WSRAPP98,784.00
5MFAFS004N4S15X2,000.00
HiportData
 
Upvote 0

Forum statistics

Threads
1,215,477
Messages
6,125,031
Members
449,205
Latest member
Eggy66

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