• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Need help with macro

klpw

New Member
Hi all,

I would like to compare data in between two workbooks. However, my code doesn't seem to work. It doesn't seem to compare data in between the workbooks. Also, I want all the updated data to be added in the next blank row. However, it seems to add in the position it is instead of in next blank row.

Code:
Sub test()
Dim varSheetA As Variant
Dim varSheetB As Variant
Dim strRangeToCheck As String
Dim strRangeToC As String
Dim iRow As Long
Dim iCol As Long
Dim wbkA As Workbook
Dim eRow As Long
Dim nlin As Integer
Dim ncol As Integer

Set wbkA = Workbooks.Open(Filename:="C:\Users\mary\Desktop\main.xlsx")
strRangeToCheck = "A:C"
strRangeToC = "C:E"
'Debug.Print Now
varSheetA = wbkA.Worksheets("Sheet1").Range(strRangeToCheck)
varSheetB = ThisWorkbook.Worksheets("Sheet1").Range(strRangeToC)
'Debug.Print Now
'Warning:  VarSheetA and VarSheetB result in arrays of the ENTIRE respective workbook
'      using LBound() to UBound() causes the ENTIRE workbook to be processed,
'      regardless of how much is actually used.
'  I'd suggest using something like:
'  thisworkbook.select
'  MaxRow = ActiveCell.SpecialCells(xlLastCell).Row
'  MaxCol = ActiveCell.SpecialCells(xlLastCell).Column
'  For iRow = 1 to MaxRow
'      For iCol = 1 to MaxCol
'--------------------------------------
For iRow = LBound(varSheetA, 1) To UBound(varSheetA, 1)
    For iCol = LBound(varSheetA, 2) To UBound(varSheetA, 2)
        If varSheetA(iRow, iCol) = varSheetB(iRow, iCol) Then
            ' Cells are identical.
            ' Do nothing.
        Else
            eRow = ThisWorkbook.Worksheets("Sheet1").Range("C65536").End(xlUp).Row + 1
            wbkA.Sheets("Sheet1").Range("A" & iRow & ":C" & iRow).Copy Destination:=ThisWorkbook.Sheets("Sheet1").Range("C" & eRow & ":E" & eRow)
            'eRow = ThisWorkbook.Worksheets("Sheet1").Cells(Rows.Count, 3).End(xlUp).Row + 1
            'ThisWorkbook.Sheets("Sheet1").Range("C" & eRow & ":E" & eRow).Value = wbkA.Sheets("Sheet1").Range("A" & iRow & ":C" & iRow).Value
            Exit For
        End If
    Next
Next
wbkA.Close savechanges:=False
End Sub
 

Attachments

  • result.jpg
    result.jpg
    123.2 KB · Views: 6
  • desired result.jpg
    desired result.jpg
    112.9 KB · Views: 6
Hi:

Upload a sample file. I don't think anyone has time to reconstruct the file for you to test the codes.

Thanks
 
Hi:

You will have to upload both files you are comparing , else I won't be able to test your macro and fix.

Thanks
 
Hi Nebu. Please see attached the code for the file for the comparison. Thanks for your help.
 

Attachments

  • tested.xlsm
    20.8 KB · Views: 5
Hi:

Could you tell me which one is your master file . I understand that you want to compare one file with the other and want to add non matching value to your master file. How many no of columns you typically have to compare?

Thanks
 
Hi Nebu,

Thanks for asking. main.xlsx is my master file. I want to add non matching value to another file which is tested.xlsm, not master file. I would like to compare 3 columns which is from column A to column C and if one of them is non-matching, I would like to add entire row to tested.xlsm file. Please see attached picture for more illustration. For instance, first time it prints out from master file to tested.xlsm from row 1 to row 7. The second time it needs to print out all the non-matching values to next blank rows in tested.xlsm which is started from row 8.
 

Attachments

  • wanted result.jpg
    wanted result.jpg
    304.8 KB · Views: 4
Hi:

I ran your macro and it is giving you the desired results not sure what is the issue you are facing.Attached is the code I ran

Thanks
 

Attachments

  • tested.xlsm
    17.5 KB · Views: 5
Hi Nebu,

If you try to add something in between and run second time, then the result is not desired.
 
Hi:

The logic of your macro is very simple. I am not sure why you are making it so complicated. Your macro basically compare 3 columns between your main file and your test file, if the data in the main file is absent in the test file it simply assign the values from the main file to the next available row in your test file. Ideally it should not give you an error or a different result if you add more data into your main file.

Thanks
 
Back
Top