PDA

View Full Version : VBA - matching and adding value with conditions - please help



Ella63112
12-18-2023, 06:58 AM
I have a single cell reference and I am trying to search another sheet to find the first match in column c and add 1 to the value in column m. However if column m matches the value in column p, I want to find the next match and add one with the same conditions. If I find a match and add 1 to the value in column m, I want the macro to stop. If all matches produce a matched value between column p and m, I want to still add one to the first match.


Can someone help. I currently have something created that finds the first match and adds one to the value in column M, but I am stuck on how to proceed

Note: tried to add a sample of what I have so for, but it will not upload from my phone.

Aussiebear
12-19-2023, 02:09 AM
Welcome to VBAX Ella63112. Can you upload a file to explain which column on which sheet matches what value etc.

At this stage you have stated you would like
a) Find a First Match in Column C on another sheet,
b) If Found then add 1 to Column M value, and then
c) if Column M.Value then matches Column P Value, then
d) move to the next match in Column C.

However, you also want (as written),
e) If I find a Match and Add 1 to Column M Value you want the macro to stop....

Then to complicate it a little further,
f) If all the Matches produce a matched value between Columns P & M, I still want to add 1 to the First Match.

Seriously a sample workbook would be extremely helpful.

Ella63112
12-19-2023, 07:33 AM
Welcome to VBAX Ella63112. Can you upload a file to explain which column on which sheet matches what value etc.

At this stage you have stated you would like
a) Find a First Match in Column C on another sheet,
b) If Found then add 1 to Column M value, and then
c) if Column M.Value then matches Column P Value, then
d) move to the next match in Column C.

However, you also want (as written),
e) If I find a Match and Add 1 to Column M Value you want the macro to stop....

Then to complicate it a little further,
f) If all the Matches produce a matched value between Columns P & M, I still want to add 1 to the First Match.

Seriously a sample workbook would be extremely helpful.

I've attached a sample workbook

Ella63112
12-19-2023, 07:40 AM
I've attached a sample workbook

Correction - referenced cell needs to match column B

Not C!

Aussiebear
12-19-2023, 10:57 AM
Concept only.


Sub FindValues()
Dim lookUpSheet As Worksheet, updateSheet As Worksheet
Dim valueToSearch As String
Dim i As Integer, t As Integer
Dim cKNum As Integer
Dim shpNum As Integer
Set lookUpSheet = Worksheets("Scanned")
Set updateSheet = Worksheets("Shipped")
Set cKNum = updateSheetSheet(t, 12)
Set sHipNum = updateSheet(t, 16)
'get the number of the last row with data in Scanned and in Shtpped
lastRowLookup = lookUpSheet.Cells(Rows.Count, "A").End(xlUp).Row
lastRowUpdate = updateSheet.Cells(Rows.Count, "B").End(xlUp).Row
'for every value in column A of Scanned
For i = 1 To lastRowUpdate
valueToSearch = updateSheet.Cells(i, 1)
'look for the match in column B of Shipped
For t = 2 To lastRowLookup
'if found a match, add I to cKNum value and proceed to the next value
If lookUpSheet.Cells(t, 2) = valueToSearch Then
cKNum = cKNum +1
If cKNum = shipNum Then
‘Look for next match
Next t
Else
Msgbox “Only 1 Match Found”
End If
Exit For
End If
Next t
Next i
End Sub


I'm sure someone will tell me I've got it wrong.

Ella63112
12-22-2023, 05:41 PM
Concept only.


Sub FindValues()
Dim lookUpSheet As Worksheet, updateSheet As Worksheet
Dim valueToSearch As String
Dim i As Integer, t As Integer
Dim cKNum As Integer
Dim shpNum As Integer
Set lookUpSheet = Worksheets("Scanned")
Set updateSheet = Worksheets("Shipped")
Set cKNum = updateSheetSheet(t, 12)
Set sHipNum = updateSheet(t, 16)
'get the number of the last row with data in Scanned and in Shtpped
lastRowLookup = lookUpSheet.Cells(Rows.Count, "A").End(xlUp).Row
lastRowUpdate = updateSheet.Cells(Rows.Count, "B").End(xlUp).Row
'for every value in column A of Scanned
For i = 1 To lastRowUpdate
valueToSearch = updateSheet.Cells(i, 1)
'look for the match in column B of Shipped
For t = 2 To lastRowLookup
'if found a match, add I to cKNum value and proceed to the next value
If lookUpSheet.Cells(t, 2) = valueToSearch Then
cKNum = cKNum +1
If cKNum = shipNum Then
‘Look for next match
Next t
Else
Msgbox “Only 1 Match Found”
End If
Exit For
End If
Next t
Next i
End Sub


I'm sure someone will tell me I've got it wrong.

I can't get it to work at all. Is there commands here that are depenant on the version I'm using?

Aussiebear
12-22-2023, 09:17 PM
Where does it error out?

Aflatoon
12-29-2023, 04:49 AM
Xpost: https://chandoo.org/forum/threads/match-with-conditions-help.55633/