简体   繁体   English

如何将Workbook1 / Sheet3中的选定行与Workbook2 / Sheet3中的选定行进行比较

[英]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. 我至今仅在VBA中开发大约6个月。 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. 我试图修改我在这里和Google找到的其他VBA代码都无济于事。 I have been working on this problem for 4 days so far and any help would be greatly appreciated. 到目前为止,我已经在此问题上进行了4天的工作,我们将不胜感激。

Workbook1 = the template workbook, Workbook2 = my WIP workbook. Workbook1 =模板工作簿,Workbook2 =我的WIP工作簿。 The data I need to analyse is on Sheet3 in both workbooks, if it matters. 如果需要的话,我需要分析的数据在两个工作簿的Sheet3中。

My task is to use the template workbook to update my WIP workbook. 我的任务是使用模板工作簿来更新我的WIP工作簿。

What I would like to do is select an entire row in Workbook1 and select an entire row in Workbook2. 我想做的是在Workbook1中选择整行,然后在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. 如果第1列和第99列之间的单元格中的任何数据不同,我想自动将Workbook1中的ENTIRE行插入到Workbook2中,该行恰好位于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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将数据从工作簿 1 的工作表 1 复制到工作簿 2 的工作表 2? - How to copy data from sheet1 of workbook1 to sheet 2 of Workbook2? 是否要将数据从所有工作簿的Sheet3导入到主工作簿? - Want to import data from Sheet3 of all workbooks to master workbook? 比较sheet1和sheet2中的单元格值,然后在sheet3中输出具有相同值的整行单元格 - Compare cells value in sheet1 and sheet2 then output entire row of the cells with same value in sheet3 从Sheet2查找名称并将行复制到Sheet3 - Lookup Name From Sheet2 and Copy Row to Sheet3 如何在workbook1更新时将workbook1中的数据添加到workbook2和workbook2更新中? - How to add data from workbook1 into workbook2 and workbook2 updates when workbook1 updates? 从 Workbook1、Column1 中查找与 Workbook2、Column 1 中的值不同的值并在新工作表中显示 - Find Values from Workbook1, Column1 that are unique from values in Workbook2, Column 1 and display in new sheet 如何将数据从一个已经打开的Excel文件(Workbook1,Sheet1,单元格A11)复制到另一个已经打开的Excel文件(workbook2,sheet1,A11) - How to copy data from one already opened excel file (Workbook1,Sheet1,cell A11) to another already opened excel file(workbook2,sheet1,A11) 如何将第一行从 Sheet1 移动到 Sheet2,将 Sheet1 上的第二行移动到 Sheet3,将 Sheet1 上的第三行移动到 Sheet4 然后重复? - How to move first row from Sheet1 to Sheet2, move second row on Sheet1 to Sheet3, move third row on Sheet1 to Sheet4 then repeat? 从Sheet3复制一行,粘贴到Sheet1,另存为文件,然后转到Sheet 3的下一行 - Copy a row from Sheet3, paste into Sheet1, save as file, then go to the next row of Sheet 3 将工作表1中的所有单元格从A + AC复制到工作表3,然后在工作表1中删除行 - Copy All cells from A + AC in sheet1 to Sheet3 then delete row in Sheet1
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM