我有四个工作簿,其中三个用于数据输入,后者用于存储在其他工作簿中输入的所有数据;事实上,它是一本主要的工作簿。我需要代码来复制在工作表1中的每个工作簿中输入的数据,以便复制到主工作簿的工作表1上的下一个可用行,但如果要从这些单独的工作簿复制的数据是重复的,则应弹出一个消息框显示它的重复。确切地说,我希望用户在列j指示进程中键入p,然后单击电子表格中的按钮将数据复制到主表。
我很乐意提供任何帮助。万分感谢您的帮助。
答案 0 :(得分:0)
Public Sub CopyData()
' To use Dictionary, add Microsoft.Script.Runtime in Tools->Reference
Dim dataDict As New Dictionary
Dim ws As Worksheet, i As Integer, r As Long, lastR As Long, c As Long, keyString As String
lastR = 2
For i = 1 To 3
Set ws = Workbooks.Open("Your file").Worksheets(1)
' Step 1. Loop through all the rows
r = 2 ' Assume data starts at row 2
Do While ws.Cells(r, 1) <> ""
' Step 2. Check duplicates
' Step 2.1 Generate keys
c = 1
keyString = ""
Do While ws.Cells(r, c) <> ""
keyString = keyString & ws.Cells(r, c)
c = c + 1
Loop
' Step 2.2 Now check duplicates in dictionary
If dataDict.Exist(keyString) = False Then
' Copy data here
' Increment last row in the master workbook
lastR = lastR + 1
Else
' Duplicate found - Highlight or do whatever here
End If
r = r + 1
Loop
Next i
End Sub