VBA finding matching value in different column

haxor

New Member
Joined
Jul 5, 2020
Messages
19
Office Version
  1. 365
Platform
  1. Windows
Hi All

I need to match column T (20) value and see if it exists in column N where the column A value and the Column I values are equal. if it finds it, it needs to write the value: "502"
in column 7 where it found it

For example for row 7 and 8, column A and column I matches, so it needs to check the value in column T which is 8 in row 8. Column N in row 8 doesn't have the value but row 7 has.

The value for Row 7, column G needs to populate the value 502 because it found a matching value

The same for Row 11, 12 and 13 as their column A and column I matches, so it needs to check the value in column T which is 5 in row 13. Column N in row 13 doesn't have the value but row 12 has.

I have the following code (There is other code as well but I think this is the problem piece):

VBA Code:
 If .Cells(counter, 1).Value = .Cells(counter + 1, 1).Value And .Cells(counter, 9).Value = .Cells(counter + 1, 9).Value Then
      
'.Cells(counter, 20).Value > 0 And
        
       Dim FoundCell As Range
       Dim ws As Worksheet
       Set ws = Sheets("Mini")
       Dim WHAT_TO_FIND As Integer

       WHAT_TO_FIND = .Cells(counter, 20).Value

       Set FoundCell = ws.Range("N:N").Find(What:=WHAT_TO_FIND)

       If Not FoundCell Is Nothing Then
                .Cells(counter, 7).Value = "502"
                MsgBox ("Found")
        Else
                .Cells(counter, 7).Value = "501"
                MsgBox ("Not Found")
                
        
            End If


   End If

Excel.gif
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
I need to match column T (20) value and see if it exists in column N where the column A value and the Column I values are equal. if it finds it, it needs to write the value: "502" in column 7 where it found it

Try this:

VBA Code:
Sub finding_matching()
  Dim a As Variant, i As Long
  Dim dic As Object, s As String
  
  a = Range("A2:T" & Range("A" & Rows.Count).End(3).Row).Value2
  Set dic = CreateObject("Scripting.Dictionary")
  For i = 1 To UBound(a, 1)
    dic(a(i, 1) & "|" & a(i, 9) & "|" & a(i, 14)) = i
  Next i
  
  For i = 1 To UBound(a, 1)
    s = a(i, 1) & "|" & a(i, 9) & "|" & a(i, 20)
    If a(i, 20) <> "" Then If dic.exists(s) Then a(dic(s), 7) = "502"
  Next
  Range("G2").Resize(UBound(a)).Value = Application.Index(a, , 7)
End Sub
 
Upvote 0
Hi Dante

I apologise for only responding now. Had internet issues

Thanks for you reply but it doesn't do anything. It doesn't put 502 where it matched

Do you know perhaps what could be the problem?
 
Upvote 0
Cross posted Match value in different Column and/or Row

While we do allow Cross-Posting on this site, we do ask that you please mention you are doing so and provide links in each of the threads pointing to the other thread (see rule 13 here along with the explanation: Forum Rules). This way, other members can see what has already been done in regards to a question, and do not waste time working on a question that may already be answered.
 
Upvote 0
Good Day Fluff. I do apologise about this

If I do cross post in future I will state it

The other post details are:


Regards
 
Upvote 0
Do you know perhaps what could be the problem?
Maybe the test data does not have the same layout as the image presented in post # 1.

This is my test data:
varios 29jul2020.xlsm
ABCDEFGHIJKLMNOPQRST
1Emp codeBCDEFHDateJKLMHoursOPQRST
235B2C2D2E2F2H201-abrJ2K2L2M29O2P2Q2R2S2
335B3C3D3E3F3H302-abrJ3K3L3M39O3P3Q3R3S3
435B4C4D4E4F4H403-abrJ4K4L4M49O4P4Q4R4S4
535B5C5D5E5F5H504-abrJ5K5L5M59O5P5Q5R5S5
635B6C6D6E6F6H605-abrJ6K6L6M69O6P6Q6R6S6
7104B7C7D7E7F7H706-abrJ7K7L7M78O7P7Q7R7S7
8104B8C8D8E8F8H806-abrJ8K8L8M89O8P8Q8R8S88
9208B9C9D9E9F9H908-abrJ9K9L9M91O9P9Q9R9S9
10208B10C10D10E10F10H1009-abrJ10K10L10M109O10P10Q10R10S10
11283B11C11D11E11F11H1106-abrJ11K11L11M117O11P11Q11R11S11
12283B12C12D12E12F12H1206-abrJ12K12L12M125O12P12Q12R12S12
13283B13C13D13E13F13H1306-abrJ13K13L13M1314O13P13Q13R13S135
14290B14C14D14E14F14H1413-abrJ14K14L14M149O14P14Q14R14S14
15422B15C15D15E15F15H1530-marJ15K15L15M151O15P15Q15R15S15
16422B16C16D16E16F16H1630-marJ16K16L16M167O16P16Q16R16S16
17422B16C16D16E16F16H1630-marJ16K16L16M161O16P16Q16R16S161
Hoja6


And this is the result:
varios 29jul2020.xlsm
ABCDEFGHIJKLMNOPQRST
1Emp codeBCDEFHDateJKLMHoursOPQRST
235B2C2D2E2F2H201-abrJ2K2L2M29O2P2Q2R2S2
335B3C3D3E3F3H302-abrJ3K3L3M39O3P3Q3R3S3
435B4C4D4E4F4H403-abrJ4K4L4M49O4P4Q4R4S4
535B5C5D5E5F5H504-abrJ5K5L5M59O5P5Q5R5S5
635B6C6D6E6F6H605-abrJ6K6L6M69O6P6Q6R6S6
7104B7C7D7E7F7502H706-abrJ7K7L7M78O7P7Q7R7S7
8104B8C8D8E8F8H806-abrJ8K8L8M89O8P8Q8R8S88
9208B9C9D9E9F9H908-abrJ9K9L9M91O9P9Q9R9S9
10208B10C10D10E10F10H1009-abrJ10K10L10M109O10P10Q10R10S10
11283B11C11D11E11F11H1106-abrJ11K11L11M117O11P11Q11R11S11
12283B12C12D12E12F12502H1206-abrJ12K12L12M125O12P12Q12R12S12
13283B13C13D13E13F13H1306-abrJ13K13L13M1314O13P13Q13R13S135
14290B14C14D14E14F14H1413-abrJ14K14L14M149O14P14Q14R14S14
15422B15C15D15E15F15H1530-marJ15K15L15M151O15P15Q15R15S15
16422B16C16D16E16F16H1630-marJ16K16L16M167O16P16Q16R16S16
17422B16C16D16E16F16502H1630-marJ16K16L16M161O16P16Q16R16S161
Hoja6



____________________________________
It would help if you can put your sample data using the XL2BB tool, download it from my signature.

____________________________________
 
Upvote 0
Thank you Dante :)

Here it is using the Xl2BB from your signature:

Forum_Excel.xlsx
ABCDEFGHIJKLMNOPQRST
1Emp CodeSurnameNameJC CodesDateDate workedJC Level2JC Level1HoursRate
235GANICE101/04/2020FPLANTINGKONDI923,329
335GANICE202/04/2020FPLANTINGKONDI923,3218
435GANICE303/04/2020FPLANTINGKONDI923,3227
535GANICE430/03/2020FWEEDINGKONDI923,3236
635GANICE51531/03/2020FPICKINGKONDI923,3245
7104ANENWABISA130/03/2020FKONDI821,878
8104ANENWABISA2330/03/2020FSame dateKONDI921,8717178
9208BAZN130/03/2020FANNUAL LEAVEKONDI123,321
10208BAZN2331/03/2020FANNUAL LEAVEKONDI923,3210
11283MENIN130/03/2020FSIPH723,327
12283MENIN230/03/2020FSIPH523,3212
13283MENIN3630/03/2020FSame dateSIPH1423,3226265
14290LIBIN1130/03/2020FCANE SUPPORTALTUS923,329
15422LAMBIN130/03/2020FSIPH323,323
16422LAMBIN2330/03/2020FSame dateSIPH723,3210101
17439MEMN130/03/2020FSIPH223,322
18439MEMN230/03/2020FSame dateSIPH223,3244
19439MEMN3631/03/2020FSICK LEAVESIPH923,3213
Before
 
Upvote 0
With your data afeter run my macro:

varios 29jul2020.xlsm
ABCDEFGHIJKLMNOPQRST
1Emp CodeSurnameNameJC CodesDateDate workedJC Level2JC Level1HoursRate
235GANICE101/04/2020FPLANTINGKONDI923.39
335GANICE202/04/2020FPLANTINGKONDI923.318
435GANICE303/04/2020FPLANTINGKONDI923.327
535GANICE430/03/2020FWEEDINGKONDI923.336
635GANICE51531/03/2020FPICKINGKONDI923.345
7104ANENWABISA150230/03/2020FKONDI821.98
8104ANENWABISA2330/03/2020FSame dateKONDI921.917178
9208BAZN130/03/2020FANNUAL LEAVEKONDI123.31
10208BAZN2331/03/2020FANNUAL LEAVEKONDI923.310
11283MENIN130/03/2020FSIPH723.37
12283MENIN250230/03/2020FSIPH523.312
13283MENIN3630/03/2020FSame dateSIPH1423.326265
14290LIBIN1130/03/2020FCANE SUPPORTALTUS923.39
15422LAMBIN130/03/2020FSIPH323.33
16422LAMBIN2330/03/2020FSame dateSIPH723.310101
17439MEMN130/03/2020FSIPH223.32
18439MEMN230/03/2020FSame dateSIPH223.344
19439MEMN3631/03/2020FSICK LEAVESIPH923.313
Hoja4
 
Upvote 0
Thanks a lot Dante!

It works when I run it separately. There is something wrong with my other code that I need to fix

Regards
 
Upvote 0

Forum statistics

Threads
1,214,839
Messages
6,121,887
Members
449,057
Latest member
Moo4247

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