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
 
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
Good morning,

I realized my mistake as I had altered the file, Your code is working fine with these columns from A to E. Thanks....

I modified the code as I had to include other details of the patient and now the columns I need to capture are:
B - Patient ID
C -
D -
E -
F -
U - fees paid

I tried to modify the code given by you as follows. It captures all the data in the columns B to E, it also captures the fees paid from column U but where ever the patient ID is repeating the total fees paid is not summed up, but picks only the last row details:

the code is :

VBA Code:
Sub CollateFees2()
  Dim d As Object
  Dim a As Variant
  Dim i As Long
  
  Set d = CreateObject("Scripting.Dictionary")
  With Sheets("Sheet1")
    a = .Range("B1", .Range("U" & Rows.Count).End(xlUp)).Value2
  End With
  d(a(1, 1)) = a(1, 2) & ";" & a(1, 3) & ";" & a(1, 4) & ";" & a(1, 5) & ";" & a(1, 20)
  
  For i = 2 To UBound(a)
    
    d(a(i, 1)) = a(i, 2) & ";" & a(i, 3) & ";" & a(i, 4) & ";" & a(i, 5) & ";" & Val(Mid(d(a(i, 1)), InStr(1, d(a(i, 1)) & ";", ";") + 1)) + a(i, 20)
    
  Next i
  With Sheets("Sheet2").Range("B2:G2").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

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
So, as I mentioned in my last post ..

.. and include the expected results
The code given by perfectly worked which summed up the fees paid by a patient on number of occasions, however the code i modified further had the issue as said above.

I am attaching the files as suggested. thanks again for patiently helping me.


Book1
ABCDEFGHIJKLMNOPQRSTU
1Sr.No.Patient IDNameAddressContact No.Alternate No.G headingH headingIheadingJ headingk headingL headingM headingN headingO headingP headingQ headingR headingS headingT headingFees Paid
212021-001ABCBD1111111100
322021-002CDECD2222222250
432021-003DEFDE3333333250
542021-004EFGEF4444444100
652021-005FGHFG555555575
762021-006GHIGH6666666175
872021-007HIJHI7777777200
982021-002CDECD2222222300
1092021-006GHIGH6666666200
11102021-008IJKIJ8888888300
12112021-009JKLJK9999999250
13122021-005FGHFG5555555250
14132021-001ABCBD1111111300
15142021-002CDECD2222222250
16152021-009JKLJK9999999150
17162021-006GHIGH6666666150
18172021-008IJKIJ8888888200
19182021-009JKLJK9999999125
20192021-007HIJHI7777777125
21202021-010LMNLM1010101010150
22212021-010LMNLM1010101010200
23222021-001ABCBD1111111200
24232021-009JKLJK999999975
25242021-002CDECD2222222400
26252021-008IJKIJ888888875
27262021-007HIJHI777777775
28272021-006GHIGH6666666100
29282021-007HIJHI7777777200
30292021-005FGHFG5555555300
31302021-003DEFDE3333333200
32
Sheet1



sheet 2 - end result expected


Book1
BCDEFGH
1Sr.No.Patient IDNameAddressContact No.Alternate No.Fees Paid
212021-001ABCBD1111111600
322021-002CDECD22222221200
432021-003DEFDE3333333450
542021-004EFGEF4444444100
652021-005FGHFG5555555625
762021-006GHIGH6666666625
872021-007HIJHI7777777600
9102021-008IJKIJ8888888575
10112021-009JKLJK9999999600
11202021-010LMNLM1010101010350
Sheet2
 
Upvote 0
The code given by perfectly worked which summed up the fees paid by a patient on number of occasions, however the code i modified further had the issue as said above.

I am attaching the files as suggested. thanks again for patiently helping me.


Book1
ABCDEFGHIJKLMNOPQRSTU
1Sr.No.Patient IDNameAddressContact No.Alternate No.G headingH headingIheadingJ headingk headingL headingM headingN headingO headingP headingQ headingR headingS headingT headingFees Paid
212021-001ABCBD1111111100
322021-002CDECD2222222250
432021-003DEFDE3333333250
542021-004EFGEF4444444100
652021-005FGHFG555555575
762021-006GHIGH6666666175
872021-007HIJHI7777777200
982021-002CDECD2222222300
1092021-006GHIGH6666666200
11102021-008IJKIJ8888888300
12112021-009JKLJK9999999250
13122021-005FGHFG5555555250
14132021-001ABCBD1111111300
15142021-002CDECD2222222250
16152021-009JKLJK9999999150
17162021-006GHIGH6666666150
18172021-008IJKIJ8888888200
19182021-009JKLJK9999999125
20192021-007HIJHI7777777125
21202021-010LMNLM1010101010150
22212021-010LMNLM1010101010200
23222021-001ABCBD1111111200
24232021-009JKLJK999999975
25242021-002CDECD2222222400
26252021-008IJKIJ888888875
27262021-007HIJHI777777775
28272021-006GHIGH6666666100
29282021-007HIJHI7777777200
30292021-005FGHFG5555555300
31302021-003DEFDE3333333200
32
Sheet1



sheet 2 - end result expected


Book1
BCDEFGH
1Sr.No.Patient IDNameAddressContact No.Alternate No.Fees Paid
212021-001ABCBD1111111600
322021-002CDECD22222221200
432021-003DEFDE3333333450
542021-004EFGEF4444444100
652021-005FGHFG5555555625
762021-006GHIGH6666666625
872021-007HIJHI7777777600
9102021-008IJKIJ8888888575
10112021-009JKLJK9999999600
11202021-010LMNLM1010101010350
Sheet2
please refer this sheet 2

Book1
BCDEFG
1Patient IDNameAddressContact No.Alternate No.Fees Paid
22021-001ABCBD1111111600
32021-002CDECD22222221200
42021-003DEFDE3333333450
52021-004EFGEF4444444100
62021-005FGHFG5555555625
72021-006GHIGH6666666625
82021-007HIJHI7777777600
92021-008IJKIJ8888888575
102021-009JKLJK9999999600
112021-010LMNLM1010101010350
Sheet2
 
Upvote 0
I am attaching the files as suggested.
Thanks, that makes it much clearer. (y)
With the extra columns, I'd take a modified approach. See if this is closer to the mark.

VBA Code:
Sub CollateFees_v2()
  Dim d As Object
  Dim a As Variant, aRws As Variant
  Dim i As Long
  Dim s As String
  
  Set d = CreateObject("Scripting.Dictionary")
  With Sheets("Sheet1")
    aRws = Evaluate("Row(1:" & .Range("A" & Rows.Count).End(xlUp).Row & ")")
    a = Application.Index(.Cells, aRws, Array(2, 3, 4, 5, 6, 21))
  End With
  For i = 1 To UBound(a)
    s = Join(Application.Index(a, i, Array(1, 2, 3, 4, 5)), ";")
    If d.exists(s) Then
      d(s) = d(s) + a(i, 6)
    Else
      d(s) = a(i, 6)
    End If
  Next i
  With Sheets("Sheet2")
    With .Range("B1").Resize(d.Count)
      .Value = Application.Transpose(d.Keys)
      .TextToColumns DataType:=xlDelimited, Semicolon:=True, Comma:=False, Space:=False, Other:=False
    End With
    .Range("G1").Resize(d.Count).Value = Application.Transpose(d.Items)
    .UsedRange.Columns.AutoFit
  End With
End Sub
 
Upvote 0
Solution
Thanks, that makes it much clearer. (y)
With the extra columns, I'd take a modified approach. See if this is closer to the mark.

VBA Code:
Sub CollateFees_v2()
  Dim d As Object
  Dim a As Variant, aRws As Variant
  Dim i As Long
  Dim s As String
 
  Set d = CreateObject("Scripting.Dictionary")
  With Sheets("Sheet1")
    aRws = Evaluate("Row(1:" & .Range("A" & Rows.Count).End(xlUp).Row & ")")
    a = Application.Index(.Cells, aRws, Array(2, 3, 4, 5, 6, 21))
  End With
  For i = 1 To UBound(a)
    s = Join(Application.Index(a, i, Array(1, 2, 3, 4, 5)), ";")
    If d.exists(s) Then
      d(s) = d(s) + a(i, 6)
    Else
      d(s) = a(i, 6)
    End If
  Next i
  With Sheets("Sheet2")
    With .Range("B1").Resize(d.Count)
      .Value = Application.Transpose(d.Keys)
      .TextToColumns DataType:=xlDelimited, Semicolon:=True, Comma:=False, Space:=False, Other:=False
    End With
    .Range("G1").Resize(d.Count).Value = Application.Transpose(d.Items)
    .UsedRange.Columns.AutoFit
  End With
End Sub
Excellent..... so quick ......
you are truly genius ...

will have to go word by word to understand and learn this amazing coding....

thanks a lot Peter_SSs
 
Upvote 0

Forum statistics

Threads
1,223,886
Messages
6,175,196
Members
452,616
Latest member
intern444

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