Add additional check

pastorkc

Board Regular
Joined
Jan 29, 2020
Messages
125
Office Version
  1. 2010
Platform
  1. Windows
I have a VBA code that looks at spreadsheet 1 and compares to spreadsheet 2. If it finds a match it then grabs the check# from the next cell down. How do I get it to look at the cell directly below the check# to see if the check was voided and then grab the next check below that? I have attached the code I already have.
VBA Code:
' Assuming worksheet 1 is the first worksheet and worksheet 2 is the second worksheet. Comparing data between sheets.
    Set ws1 = ThisWorkbook.Worksheets(1)
    Set ws2 = ThisWorkbook.Worksheets(2)
    
    lastRow1 = ws1.Cells(ws1.Rows.Count, "A").End(xlUp).Row
    lastRow2 = ws2.Cells(ws2.Rows.Count, "A").End(xlUp).Row
    
    For i = 2 To lastRow1 ' Assuming data starts from row 2
        For j = 2 To lastRow2 ' Assuming data starts from row 2
            If ws1.Cells(i, "A").Value = ws2.Cells(j, "A").Value And _
               ws1.Cells(i, "F").Value = ws2.Cells(j, "C").Value Then
               ws1.Cells(i, "J").Value = ws2.Cells(j + 1, "E").Value ' Value from worksheet 2 column E, down one cell
            Exit For ' No need to continue checking if a match is found
            End If
        Next j
    Next i
    Application.ScreenUpdating = True
    Application.Calculation = xlCalculationAutomatic
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
Please post samples of your two data sheets, and show us a situation in which you are describing (I think it may be clearer to us if we can see what your data looks like).

MrExcel has a tool called “XL2BB” that lets you post samples of your data that will allow us to copy/paste it to our Excel spreadsheets, so we can work with the same copy of data that you are. Instructions on using this tool can be found here: XL2BB Add-in

Note that there is also a "Test Here” forum on this board. This is a place where you can test using this tool (or any other posting techniques that you want to test) before trying to use those tools in your actual posts.
 
Upvote 0
Transit Invoice Sheet.xlsm
ABCD
15621802WAKM AM-950Marketing and Advertising350.00
GL Transit Report



Transit Invoice Sheet.xlsm
ABCDEFG
495WAKMWAKM AM-950137-00002-0217Invoice137-00002-02177/31/23700.00
496WAKMWAKM AM-950Payment218028/7/23-700.00
497WAKMWAKM AM-950Payment21802V8/7/23700.00
498WAKMWAKM AM-950Payment218038/7/23-350.00
499WAKMWAKM AM-950Payment08968/7/23-350.00
5000.00
501WAKM0.00
Vendor Transaction History


If you notice the top is sheet 1 and it found the matching Invoice on sheet 2 and grabbed the check number, but the check was then voided and replaced but it didn't see that and just placed the first check number. I hope this helps.
 
Upvote 0
Your images do not seem to match your code.

You seem to be comparing column A from Sheet1 to column A of Sheet2
and column F from Sheet1 to column C of Sheet2.

But in your images, your column A values do not match at all, so I don't see how that would work.
Also, you are not showing column F from Sheet1 in your image, so we have no idea of what is in there.
 
Upvote 0
Oh yes, because later in the code I have to rearrange the columns to match the format the end person wants. Let me upload the version before that part of the code hits.
 
Upvote 0
Transit Invoice Sheet.xlsm
ABCDEFG
766
767WAKMWAKM AM-950137-00002-0217Invoice137-00002-02177/31/23700.00
768WAKMWAKM AM-950Payment218028/7/23-700.00
769WAKMWAKM AM-950Payment21802V8/7/23700.00
770WAKMWAKM AM-950Payment218038/7/23-350.00
771WAKMWAKM AM-950Payment08968/7/23-350.00
Vendor Transaction History




Transit Invoice Sheet.xlsm
ABCDEFG
468WAKMWAKM AM-9507/31/235090.14.T20.T.GRadio Advertising/Web137-00002-0217350.00
4695090.14.T20.T.GRadio Advertising/Web350.00
GL Transit Report


Here it is in raw form before any of the macro has ran.
 
Upvote 0
See if this works. It seems to worked on the sample you posted:
VBA Code:
' Assuming worksheet 1 is the first worksheet and worksheet 2 is the second worksheet. Comparing data between sheets.
    Set ws1 = ThisWorkbook.Worksheets(1)
    Set ws2 = ThisWorkbook.Worksheets(2)
   
    lastRow1 = ws1.Cells(ws1.Rows.Count, "A").End(xlUp).Row
    lastRow2 = ws2.Cells(ws2.Rows.Count, "A").End(xlUp).Row
   
    For i = 2 To lastRow1 ' Assuming data starts from row 2
        For j = 2 To lastRow2 ' Assuming data starts from row 2
            If ws1.Cells(i, "A").Value = ws2.Cells(j, "A").Value And _
               ws1.Cells(i, "F").Value = ws2.Cells(j, "C").Value Then
'              Check to see if 2 rows below contains a "V" for void
               If Right(ws2.Cells(j + 2, "E"), 1) = "V" Then
'                  Pull value from three rows down
                   ws1.Cells(i, "J").Value = ws2.Cells(j + 3, "E").Value
               Else
'                  Pull value from one row down
                   ws1.Cells(i, "J").Value = ws2.Cells(j + 1, "E").Value ' Value from worksheet 2 column E, down one cell
               End If
            Exit For ' No need to continue checking if a match is found
            End If
        Next j
    Next i
    Application.ScreenUpdating = True
    Application.Calculation = xlCalculationAutomatic
 
Upvote 0
Solution
See if this works. It seems to worked on the sample you posted:
VBA Code:
' Assuming worksheet 1 is the first worksheet and worksheet 2 is the second worksheet. Comparing data between sheets.
    Set ws1 = ThisWorkbook.Worksheets(1)
    Set ws2 = ThisWorkbook.Worksheets(2)
  
    lastRow1 = ws1.Cells(ws1.Rows.Count, "A").End(xlUp).Row
    lastRow2 = ws2.Cells(ws2.Rows.Count, "A").End(xlUp).Row
  
    For i = 2 To lastRow1 ' Assuming data starts from row 2
        For j = 2 To lastRow2 ' Assuming data starts from row 2
            If ws1.Cells(i, "A").Value = ws2.Cells(j, "A").Value And _
               ws1.Cells(i, "F").Value = ws2.Cells(j, "C").Value Then
'              Check to see if 2 rows below contains a "V" for void
               If Right(ws2.Cells(j + 2, "E"), 1) = "V" Then
'                  Pull value from three rows down
                   ws1.Cells(i, "J").Value = ws2.Cells(j + 3, "E").Value
               Else
'                  Pull value from one row down
                   ws1.Cells(i, "J").Value = ws2.Cells(j + 1, "E").Value ' Value from worksheet 2 column E, down one cell
               End If
            Exit For ' No need to continue checking if a match is found
            End If
        Next j
    Next i
    Application.ScreenUpdating = True
    Application.Calculation = xlCalculationAutomatic
Worked perfect, thank you
 
Upvote 0
You are welcome.
Glad I was able to help!
 
Upvote 0

Forum statistics

Threads
1,215,095
Messages
6,123,072
Members
449,093
Latest member
ripvw

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