有条件地将数据从一张纸复制到另一张工作簿

时间:2020-03-15 16:15:37

标签: excel vba

我需要按照以下条件将数据从一张纸复制到另一张纸:

1)用户应该选择/浏览一个文件,从打开的文件中,它将数据从sheet1的A列复制到D列

2)A列具有唯一编号,而C和D列具有重复编号,则应忽略重复项。它仅复制A列中的唯一编号以及B,C和D列中的相应数据

3)通过查看上次使用的行,将数据粘贴在工作表RawData的仪表板文件(主文件)中的第AH列到AH列 图片:如图所示,它应复制突出显示的数据并忽略重复

我已经写了下面的代码,但是它不是按照我的要求,请帮助我

Private Sub copy()
Dim wsCopy As Worksheet
Dim wsDest As Worksheet
Dim lCopyLastRow As Long
Dim lDestLastRow As Long
Dim FileToOpen As Variant
Dim OpenBook As Workbook

Application.ScreenUpdating = False

FileToOpen = Application.GetOpenFilename(Title:="Browse for Berkhund File & Import", 
FileFilter:="Excel Files (*.xls*),*xls*")
If FileToOpen <> False Then
Set OpenBook = Application.Workbooks.Open(FileToOpen)

'Set variables for copy and destination sheets
Set wsCopy = OpenBook.Sheets(1)
Set wsDest = ThisWorkbook.Worksheets(1)

'1. Find last used row in the copy range based on data in column A
 lCopyLastRow = wsCopy.Cells(wsCopy.Rows.Count, "A").End(xlUp).Row

'2. Find first blank row in the destination range based on data in column AH
 'Offset property moves down 1 row
 lDestLastRow = wsDest.Cells(wsDest.Rows.Count, "AH").End(xlUp).Offset(1).Row

'3. Copy & Paste Data
wsCopy.Range("A2:D" & lCopyLastRow).copy _
wsDest.Range("AH" & lDestLastRow)
OpenBook.Close False
'Optional - Select the destination sheet
wsDest.Activate

'End Sub
End If
Application.ScreenUpdating = True

Sheets("RawData").Activate

End Sub

enter image description here

0 个答案:

没有答案