繁体   English   中英

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

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

这是我第一次在这里发帖。 我至今仅在VBA中开发大约6个月。 我没有找到与我的确切需求相似的东西。 我试图修改我在这里和Google找到的其他VBA代码都无济于事。 到目前为止,我已经在此问题上进行了4天的工作,我们将不胜感激。

Workbook1 =模板工作簿,Workbook2 =我的WIP工作簿。 如果需要的话,我需要分析的数据在两个工作簿的Sheet3中。

我的任务是使用模板工作簿来更新我的WIP工作簿。

我想做的是在Workbook1中选择整行,然后在Workbook2中选择整行。 如果第1列和第99列之间的单元格中的任何数据不同,我想自动将Workbook1中的ENTIRE行插入到Workbook2中,该行恰好位于Workbook2中所选行的下方。

这些行中的数据可以包括文本,数字,字母数字和标准键盘符号($,%等)。

很抱歉,我没有任何代码可向您显示,也没有找到任何相关的开始。 由于它是机密性质,因此我也无法共享我的工作簿。

感谢您提供任何帮助。 希望我在这里提供了足够的信息来解释我的问题。

如何逐个单元地循环遍历数据。

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.

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