VBA 将数据复制到另一张表(如果尚未在表中)

问题描述 投票:0回答:1

我对 VBA 很陌生。

我的编码当前将报告从一张纸(Rufus Unal)复制到另一张纸(UTM Unal Cash Report),我希望它只复制另一张纸上没有的数据。使用 A 列作为标准,因为它包含唯一的引用。因此,如果 A 列中的值位于工作表 (Rufus Unal) 中而不是工作表 (UTM Unal Cash Report) 中,则将范围复制到最后一行数据下方。

这是我当前的代码:

Sub RunUac()

Dim LR1 As Long
Dim LR2 As Long
Dim LR3 As Long
Dim LR4 As Long
Dim wb1 As Workbook
Dim wb2 As Workbook
Set wb1 = ThisWorkbook
Dim cell As Range

Application.ScreenUpdating = False

Worksheets("Rufus Unal").Visible = True


Workbooks.Open "K:\Finance\Unallocated Cash\Reconciliations\Templates/Rufus_Unal.xlsx", ReadOnly:=True


Workbooks("Rufus_Unal.xlsx").Activate

Sheets("UCRR001x").Activate

LR1 = Cells(Rows.Count, "a").End(xlUp).Row

Range("A2:O" & LR1).Copy

    wb1.Activate
    Sheets("Rufus Unal").Select
    Range("A11").PasteSpecial Paste:=xlPasteValues
    Application.CutCopyMode = False
        
   
Workbooks("Rufus_Unal.xlsx").Activate

ActiveWorkbook.Close SaveChanges:=False

Sheets("GL").Select
    Range("Sheet16[[#Headers],[Accnt.]]").Select
    Selection.ListObject.QueryTable.Refresh BackgroundQuery:=False
    
Sheets("Rufus Unal").Select
    LR1 = Cells(Rows.Count, "b").End(xlUp).Row

Range("A11:A" & LR1).Copy
    Sheets("UTM Unal Cash Report").Select
    Range("A10").PasteSpecial Paste:=xlPasteValues
    Application.CutCopyMode = False

Sheets("Rufus Unal").Select
Range("I11:I" & LR1).Copy
    Sheets("UTM Unal Cash Report").Select
    Range("B10").PasteSpecial Paste:=xlPasteValues
    Application.CutCopyMode = False
    
Sheets("Rufus Unal").Select
Range("D11:D" & LR1).Copy
    Sheets("UTM Unal Cash Report").Select
    Range("C10").PasteSpecial Paste:=xlPasteValues
    Application.CutCopyMode = False

Sheets("Rufus Unal").Select
Range("B11:B" & LR1).Copy
    Sheets("UTM Unal Cash Report").Select
    Range("D10").PasteSpecial Paste:=xlPasteValues
    Application.CutCopyMode = False

Sheets("Rufus Unal").Select
Range("C11:C" & LR1).Copy
    Sheets("UTM Unal Cash Report").Select
    Range("E10").PasteSpecial Paste:=xlPasteValues
    Application.CutCopyMode = False
    

Sheets("Sign Off Form").Select
     Range("C31").Value = Application.UserName
     Application.ScreenUpdating = True

End Sub

我尝试复制范围,然后删除重复项,但无法使其正常工作,并想知道是否可以仅使用 A 列作为签入条件来复制“UTM Unal Cash Report”表中尚未存在的数据每张。

excel vba copy criteria paste
1个回答
0
投票

您可以尝试将此作为代码的起始方法:

Sub RunUac()


'Declarions
Dim LR1 As Long
Dim wb1 As Workbook
Dim wb2 As Workbook
Dim ws1 As Worksheet

''' Clearly set the workbooks used below like

Set wb1 = ThisWorkbook
Set wb2 = Workbooks.Open("K:\Finance\Unallocated Cash\Reconciliations\Templates/Rufus_Unal.xlsx", ReadOnly:=True)

'' Similarly set the worksheets used below

Set ws1 = wb2.Sheets("Name of the sheet")

'''Check the rows in first sheet

LR1 = Cells(Rows.Count, "a").End(xlUp).Row

'''' See if data is present in other sheet, if yes, you can copy the data

loop comes here



End Sub
© www.soinside.com 2019 - 2024. All rights reserved.