vba loop with conditional ifs - help needed

PAULGOLD

New Member
Joined
Jun 21, 2004
Messages
15
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
cheque book Bank Data
Date cheque no amount matching cheque in bank date cheque no amount
01/10/2009 692201 40000 no match 01/10/2009 692203 23000
02/10/2009 692202 50000 no match 05/10/2009 692201 40000
03/10/2009 692203 23000 no match 06/10/2009 692205 51000
04/10/2009 692204 29000 no match 09/10/2009 692202 50000
05/10/2009 692205 51000 no match 08/10/2009 692204 29000
06/10/2009 692206 35000 no match 11/10/2009 692206 35000
07/10/2009 692207 15000 no match 08/10/2009 692208 1
08/10/2009 692208 20010 no match
09/10/2009 692209 5130 no match 07/10/2009 692207 15000
10/10/2009 692210 12 no match 13/10/2009 692213 1500
11/10/2009 692211 1500 no match
12/10/2009 692212 1500 no match
13/10/2009 692213 1500 no match


hi I am trying to write code for column D using a loop by comparing column B to column F. where the two do not match i want to insert " not match", if the two match but the amounts in columns C and G I want to give Ref number and state that the "refs match" .
if they refs in column B and F and amounts match in columns C and G I want to write the reference only.


my VBA code so far is below but is not quite working - and is only producing "no match":

Sub bankreconciliation()
Dim x, y As Integer

For x = 3 To Cells(Rows.Count, 1).End(xlUp).Row
For y = 3 To Cells(Rows.Count, 1).End(xlUp).Row

If Cells(x, 2) <> Cells(y, 6) Then
Cells(x, 4) = " no match"

ElseIf Cells(x, 2) = Cells(y, 6) Then
Cells(x, 4) = Cells(y, 6) & " REFS MATCH"

ElseIf Cells(x, 2) = Cells(y, 6) And Cells(x, 3) = Cells(y, 7) Then
Cells(x, 4) = Cells(y, 6)

End If

Next y
Next x

End Sub
 

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.
the problem is not clear.

the data is like this from row no. 2 and not row no. 3

Excel Workbook
ABCDEFG
1Datecheque noamountmatching cheque in bankdatecheque noamount
21/10/200969220140000no match1/10/200969220323000
32/10/200969220250000no match5/10/200969220140000
43/10/200969220323000no match6/10/200969220551000
54/10/200969220429000no match9/10/200969220250000
65/10/200969220551000no match8/10/200969220429000
76/10/200969220635000no match11/10/200969220635000
87/10/200969220715000no match8/10/20096922081
98/10/200969220820010no match
109/10/20096922095130no match7/10/200969220715000
1110/10/200969221012no match13/10/20096922131500
1211/10/20096922111500no match
1312/10/20096922121500no match
1413/10/20096922131500no match
Sheet1


quote
column B to column F. where the two do not match i want to insert " not match
if the two match but the amounts in columns C and G(????????) I want to give Ref number and state that the "refs match" .
if they refs in column B and F and amounts match in columns C and G I want to write the reference only.unquote


The second and third conditions are not clear. I am rephrasing themn


if column B and F match but the columns C and G DO NOT MATCH then in col D write
value in column B and add a string "refs match"


if column B and F match and colcumns C and G also match then in col D write value of column B


x is enough y is not necessary if you are doing match in EACH row .if each row is checked with all the rows revert back to newsgroup. that is a different problem

try this macro "test" and if necessary modify. before testings copy the data somewhere safely so that original data can be retrieved.

in code dim y is not necessary
Code:
Sub bankreconciliation()
Dim x, y As Integer


For x = 2 To Cells(Rows.Count, 1).End(xlUp).Row
'For y = 3 To Cells(Rows.Count, 1).End(xlUp).Row
If Cells(x, 2) <> Cells(x, 6) Then Cells(x, 4) = "no match"
'ElseIf Cells(x, 2) = Cells(y, 6) Then
'Cells(x, 4) = Cells(y, 6) & " REFS MATCH"
If Cells(x, 2) = Cells(x, 6) Then
If Cells(x, 3) <> Cells(x, 7) Then
Cells(x, 4) = Cells(x, "B") & " " & "REFS MATCH"
Else
Cells(x, 4) = Cells(x, "B")
End If
End If
Next x
End Sub
 
Upvote 0
hi venkat
i tried the code and it is not quite what i was trying to say.
case 1
if the references in column B and F do not match then I want to put in column D " no match"
case 2
if the references in column B and F match but their corresponding amounts do not match I want to state " reference match but not amounts, "and give the reference.
case 3

if the references in column B and F match and corresponding amount match then I want to state " references and amounts match)
I want the vba to loop through the references in column B and F. it is not necessary and not expected that reference eg 692201 be on the same line but if they match wherever they are in column B and F I want the code to state theat given the three conditions above.
thanks for any help in rectifying my code.
Paul
 
Upvote 0
Do you really need a macro?
Assuming that a cheque number will not occur more than once in either column B or column F, then does this formula, copied down, do what you want?

Excel Workbook
ABCDEFG
1cheque bookBank Data
2Datecheque noamountmatching cheque in bankdatecheque noamount
31/10/0969220140000match1/10/0969220323000
42/10/0969220250000match5/10/0969220140000
53/10/0969220323000match6/10/0969220551000
64/10/0969220429000match9/10/0969220250000
75/10/0969220551000match8/10/0969220429000
86/10/0969220635000match11/10/0969220635000
97/10/0969220715000match8/10/096922081
108/10/0969220820010reference match but not amounts 692208
119/10/096922095130no match7/10/0969220715000
1210/10/0969221012no match13/10/096922131500
1311/10/096922111500no match
1412/10/096922121500no match
1513/10/096922131500match
16
Match Cheques




If you do need a macro, then perhaps try this adaptation.
Rich (BB code):
Sub Check_Matches()
  Dim lr As Long
  
  Const f As String = "=IF(COUNTIFS(F$3:F$#,B3,G$3:G$#,C3)>0,""match"",IF(COUNTIF(F$3:F$#,B3)>0,""reference match but not amounts "" & B3,""no match""))"
  
  lr = Range("F" & Rows.Count).End(xlUp).Row
  With Range("D3:D" & Range("C" & Rows.Count).End(xlUp).Row)
    .Formula = Replace(f, "#", lr, 1, -1, 1)
    .Value = .Value
  End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,374
Messages
6,124,571
Members
449,173
Latest member
Kon123

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