look up cell values in one column and add corresponding row values in other column

dss28

Board Regular
Joined
Sep 3, 2020
Messages
165
Office Version
  1. 2007
Platform
  1. Windows
My Sheet1 contains data of which column B contains patient IDs, Column C - Date, column D - Contact No, Column E - fees paid on various number of days / visits.
I want to gather data of sheet1 to sheet2 where column B will contain patient ID, Column C- contact No. and Column D - total fees paid by the patient so far in one row.

how to build up the vba code
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
Hi dss,

Is there a reason you can't use VLOOKUP on Sheet 2? Or, better yet, a pivot table? A pivot table would make what you're attempting to accomplish a breeze.
 
Upvote 0
Hi dss,

Is there a reason you can't use VLOOKUP on Sheet 2? Or, better yet, a pivot table? A pivot table would make what you're attempting to accomplish a breeze.
will vlookup be able to add the cell values i intend to have
 
Upvote 0
In sheet2, I would first get a list of distinct patient IDs. Then, use VLOOKUP to get the contact info. Use a SUMIF formula to get the sum of the fees paid IF the ID's match.
 
Upvote 0
is there any way to build up the pivot table through vba code
In sheet2, I would first get a list of distinct patient IDs. Then, use VLOOKUP to get the contact info. Use a SUMIF formula to get the sum of the fees paid IF the ID's match.
 
Upvote 0
Not sure how to create a pivot table via code, to be honest. The pivot table you appear to need wouldn't be hard to build manually, given you only need two columns. I would put ID and Contact as row values and Fees Paid as a Value. Then, format the Fees Paid to be SUM rather than count and viola - you have a piivot table in about 2 minutes of work that will update as you populate your data table.
 
Upvote 0
In sheet2, I would first get a list of distinct patient IDs. Then, use VLOOKUP to get the contact info. Use a SUMIF formula to get the sum of the fees paid IF the ID's match.
I will rather choose a vba based approach over the pivot table one.
1. how to dig out distinct patient ID without repeating / duplicating. Any function in excel which can be used?
 
Upvote 0
You have not said what, if anything is on Sheet2 and if there is anything whether it should get deleted or added to.
I have assumed that Sheet2 exists but has nothing on it.

Just one other issue: Is it possible that the same ID number could have different contact numbers on different rows in Sheet1? If so, what should happen with that ID on Sheet2?
If that can happen, the code below would keep the contact numbers that is last encountered down the rows of Sheet1.

Test with a copy of your workbook.

VBA Code:
Sub CollateFees()
  Dim d As Object
  Dim a As Variant
  Dim i As Long
  
  Set d = CreateObject("Scripting.Dictionary")
  With Sheets("Sheet1")
    a = .Range("B1", .Range("E" & Rows.Count).End(xlUp)).Value2
  End With
  d(a(1, 1)) = a(1, 3) & ";" & a(1, 4)
  For i = 2 To UBound(a)
    d(a(i, 1)) = a(i, 3) & ";" & Val(Mid(d(a(i, 1)), InStr(1, d(a(i, 1)) & ";", ";") + 1)) + a(i, 4)
  Next i
  With Sheets("Sheet2").Range("B1:C1").Resize(d.Count)
    .Value = Application.Transpose(Array(d.Keys, d.Items))
    .Columns(2).TextToColumns DataType:=xlDelimited, Semicolon:=True, Comma:=False, Space:=False, Other:=False
  End With
End Sub
 
Upvote 0
You have not said what, if anything is on Sheet2 and if there is anything whether it should get deleted or added to.
I have assumed that Sheet2 exists but has nothing on it.

Just one other issue: Is it possible that the same ID number could have different contact numbers on different rows in Sheet1? If so, what should happen with that ID on Sheet2?
If that can happen, the code below would keep the contact numbers that is last encountered down the rows of Sheet1.

Test with a copy of your workbook.

VBA Code:
Sub CollateFees()
  Dim d As Object
  Dim a As Variant
  Dim i As Long
 
  Set d = CreateObject("Scripting.Dictionary")
  With Sheets("Sheet1")
    a = .Range("B1", .Range("E" & Rows.Count).End(xlUp)).Value2
  End With
  d(a(1, 1)) = a(1, 3) & ";" & a(1, 4)
  For i = 2 To UBound(a)
    d(a(i, 1)) = a(i, 3) & ";" & Val(Mid(d(a(i, 1)), InStr(1, d(a(i, 1)) & ";", ";") + 1)) + a(i, 4)
  Next i
  With Sheets("Sheet2").Range("B1:C1").Resize(d.Count)
    .Value = Application.Transpose(Array(d.Keys, d.Items))
    .Columns(2).TextToColumns DataType:=xlDelimited, Semicolon:=True, Comma:=False, Space:=False, Other:=False
  End With
End Sub
thanks
tried but giving error (Run time error 13, type mismatch) at line
d(a(i, 1)) = a(i, 3) & ";" & Val(Mid(d(a(i, 1)), InStr(1, d(a(i, 1)) & ";", ";") + 1)) + a(i, 4)
 
Upvote 0
The code worked for my sample data so I presume there must be something different about your data.
Here is mine, perhaps you could provide a small sample of yours with XL2BB?

dss28.xlsm
ABCDE
1IDDateContact NoFees
2ID1Num for ID168
3ID2Num for ID292
4ID1Num for ID160
5ID2Num for ID282
6ID3Num for ID362
7ID4Num for ID491
8ID3Num for ID375
9ID3Num for ID351
10ID3Num for ID381
11ID3Num for ID392
12ID3Num for ID363
13ID4Num for ID469
14
Sheet1


.. and results of the code:

dss28.xlsm
ABCD
1IDContact NoFees
2ID1Num for ID1128
3ID2Num for ID2174
4ID3Num for ID3424
5ID4Num for ID4160
6
Sheet2
 
Upvote 0

Forum statistics

Threads
1,215,459
Messages
6,124,947
Members
449,198
Latest member
MhammadishaqKhan

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