VBA look for match between cell and column then in row add amount to specific cell else copy data in new row

kaasplank

New Member
Joined
Jun 3, 2020
Messages
10
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
Hey all, I am looking for help on a macro. What i want to do:
see if value in B3 matches any value in column d. If it does than add one to specific cell in that row if not copy data in new row.
This is what i have now:
I figures strcomp doesn't work with ranges so tried entering the ranges as strings but this is not working either… Any ideas? (maybe with match or lookup or something else completely?)

Private Sub CommandButton1_Click()
Dim lastrow As Long, Lresult As Integer
With Lresult = StrComp(Range("B3").Text, Range("D2:D65536").Text, vbTextCompare)
If Lresult = 0 Then
lastrow = Range("D65536").End(xlUp).Row
Cells(lastrow, 8).Value = Cells(lastrow, 8) + 1
Else
Range("B3:B6").Copy
lastrow = Range("D65536").End(xlUp).Row
Sheets("Blad1").Activate
Cells(lastrow + 1, 4).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=True
Cells(lastrow + 1, 8).Value = 1
End If
End With

Application.CutCopyMode = False

End Sub

Thanks in advance!
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
Could you use the XL2BB add-in (icon in menu) to post a screen shot of your data? Explain in detail what you want to do referring to specific cells, rows, column and sheets. Replace any confidential data with generic data.
 
Upvote 0
123124515.xlsm
ABCDEFGH
1artikel nummernaamprijsvoorraadaantal
2IN0005
3artiel: IN0005
4naam:Artikel 5
5prijs75
6voorraad62
7
8
9
10
11
Blad1


In detail:
I have a sheet "voorraadlijst" and blad1. Voorraadlijst contains data of products. On blad1, I can search for product number on b2. Product details are shown below b2. I want a button that adds the product to the range on the right (d1 - h1). If a product matches a product already in the range than i want to "+1" in column h for the specific row in which the match is found. If the product isn't listed yet, copy the data in a new row in the range.

So basicaly what i want is to check for a match between b3 and column D. If there is a match +1 to the row in which the match has been found otherwise copy data in the next empty row.

Does that make sense now?
 
Upvote 0
So basicaly what i want is to check for a match between b3 and column D. If there is a match +1 to the row in which the match has been found otherwise copy data in the next empty row.
Using the data you posted, there is no match for IN0005 in column D so B3:B6 should be copied to D2:D5. Is this correct? Let's assume that D2 contained IN0005, then you want to add 1 to whatever value is in H2. Is this correct?
 
Upvote 0
Using the data you posted, there is no match for IN0005 in column D so B3:B6 should be copied to D2:D5. Is this correct? Let's assume that D2 contained IN0005, then you want to add 1 to whatever value is in H2. Is this correct?
this exactly!
 
Upvote 0
Try:
VBA Code:
Private Sub CommandButton1_Click()
    Dim lastrow As Long, fnd As Range
    lastrow = Range("B" & Rows.Count).End(xlUp).Row
    Set fnd = Range("D:D").Find(Range("B3").Value, LookIn:=xlValues, lookat:=xlWhole)
    If Not fnd Is Nothing Then
        Range("H" & fnd.Row) = Range("H" & fnd.Row) + 1
    Else
        Range("B3:B" & lastrow).Copy Cells(Rows.Count, "D").End(xlUp).Offset(1)
    End If
    Application.CutCopyMode = False
End Sub
 
Upvote 0
123124515.xlsm
ABCDEFGH
1artikel nummernaamprijsvoorraadaantal
2IN0005Beschr. 1
3artiel: IN000551
4naam:Artikel 51275
5prijs7529
6voorraad62Beschr. 10
750
88750
9283
10Beschr. 10
1150
128750
13283
Blad1
Cell Formulas
RangeFormula
D2:D13,B3:B6D2=INDEX(Voorraadlijst!E3:Voorraadlijst!N27,XMATCH(D1,Voorraadlijst!E3:Voorraadlijst!E27,1),{1;2;4;5})
Dynamic array formulas.


this is the result. Maybe it works if it transposes the copied data?
 
Upvote 0
This is what I got when I ran the macro on the data you posted:
kassplant.xlsm
ABCDEFGH
1artikel nummernaamprijsvoorraadaantal
2IN0005IN0005
3artiel:IN0005Artikel 5
4naam:Artikel 575
5prijs7562
6voorraad62
Sheet1
 
Upvote 0
maybe it has something to do with the code under B3?
=INDEX(Voorraadlijst!C4:Voorraadlijst!L28;X.VERGELIJKEN(B2;Voorraadlijst!C4:Voorraadlijst!C28;1);{1;2;4;5})
 
Upvote 0
If what I posted is the correct result, try:
VBA Code:
Private Sub CommandButton1_Click()
    Dim lastrow As Long, fnd As Range
    lastrow = Range("B" & Rows.Count).End(xlUp).Row
    Set fnd = Range("D:D").Find(Range("B3").Value, LookIn:=xlValues, lookat:=xlWhole)
    If Not fnd Is Nothing Then
        Range("H" & fnd.Row) = Range("H" & fnd.Row) + 1
    Else
        Range("B3:B" & lastrow).Copy
        Cells(Rows.Count, "D").End(xlUp).Offset(1).PasteSpecial xlPasteValues
    End If
    Application.CutCopyMode = False
    Application.CutCopyMode = False
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,911
Messages
6,122,194
Members
449,072
Latest member
DW Draft

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