简体   繁体   中英

How to compare a selected row in Workbook1/Sheet3 with a selected row in Workbook2/Sheet3

This is my first time posting here. I have only been developing in VBA for about 6 months now. I have not found anything similar to my exact needs. I have tried modifying other VBA code I found here and Google to no avail. I have been working on this problem for 4 days so far and any help would be greatly appreciated.

Workbook1 = the template workbook, Workbook2 = my WIP workbook. The data I need to analyse is on Sheet3 in both workbooks, if it matters.

My task is to use the template workbook to update my WIP workbook.

What I would like to do is select an entire row in Workbook1 and select an entire row in Workbook2. If any of the data in the cells between columns 1 and 99 are different, I want to automatically insert the ENTIRE row from Workbook1 into Workbook2, just below the selected row in Workbook2.

The data in these rows can include text, numeric, alphanumeric and standard keyboard symbols ($, %, etc.,)

I'm sorry that I don't have any code to show you, I have not found anything relevant to start with. I also can not share my workbook due to it's confidential nature.

Thank you for any help you can provide. Hopefully I provided enough information here to explain my problem.

Looping through the data cell by cell is how this would get done.

x = 1 'first row of data
n = 10 'last row of data, could be 100 for all I know
y = 1 'first column of data
z = 10 'last column of data, could be 100 for all I know

Do While x <= n
y = 1 'reset the value of y
    Do While y <= z

    If Workbooks(a).Sheets("Sheet3").cells(x, y) <> Workbooks(b).Sheets("Sheet3").cells(x, y) Then
    MsgBox "Found the error in row " & x & " and column " & y & ".", VbOKOnly
    'Do something else...
    else
    End If
    y = y + 1 'go to the next column
    Loop
x = x + 1 'go to the next row
Loop
sub compareandcopy()
dim source as worksheet
dim target as worksheet
set source = workbooks("nameoftemplate").worksheets(3)
set target = workbooks("my wip file").worksheets(3)
dim x as integer
dim y as integer
dim i as integer
' source.Activate
' x = Selection.Row
' target.Activate
' y = Selection.Row

'Using InputBox
x = cint(inputbox("enter row number in source"))
y = cInt(InputBox("Enter row number in target")) 'input box returns variant - y expects integer
for i = 1 to 99
if source.cells(x,i)<>target.cells(y,i) then
    target.rows(y+1).insert
    source.rows(x).copy target.cells(y+1,1)
    exit for
end if
next i
end sub
Sub CompareRowsAndCopy1()

Dim source As Worksheet
Dim target As Worksheet

Set source = Workbooks("Template.xlsm").Worksheets("Sheet3")
Set target = Workbooks("WIP Workbook.xlsm").Worksheets("Sheet3")
Dim x As Integer
Dim y As Integer
Dim i As Integer

source.Activate
x = CInt(source.Application.InputBox(Prompt:="Select row to compare in 
template ", Type:=1))
'MsgBox x


target.Activate
y = CInt(target.Application.InputBox(Prompt:="Select row to compare in WIP 
Workbook ", Type:=1))
'MsgBox y

For i = 1 To 99 ' This indicates which columns to compare.
    If source.Cells(x, i) <> target.Cells(y, i) Then
        target.Rows(y + 1).Insert
        source.Rows(x).Copy target.Cells(y + 1, 1)
    Else
    MsgBox ("No differences found in row " & y)

Exit For
    End If
Next i
End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM