簡體   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