excel - 如何从一个工作簿复制数据并将值仅粘贴到另一个工作簿中并允许宏只运行一次?

标签 excel vba copy-paste

当我不小心运行 VBA 代码多次将数据从一个工作簿复制/粘贴到目标工作簿时,它将在目标工作表中创建具有相同数据的多行。
enter image description here
我希望 VBA 代码能够识别上一行是一样的,以防止数据重复。
此外,我的 VBA 代码会将公式复制到我的目标 Excel 文件中。
我只想复制值而不是公式。我不知道如何使用PasteSpecial在我的 VBA 代码中。

Sub Copy_Paste_Below_Last_Cell()
    
    Dim wsDest As Worksheet
    Dim lDestLastRow As Long
    
    Set wsDest = Workbooks("Destination.xlsx").Worksheets("DataBase")
    lDestLastRow = wsDest.Cells(wsDest.Rows.Count, "C").End(xlUp).Offset(1).Row
    
    ' How to use PasteSpecial Paste:=xlPasteValues here?
    Sheet4.Range("B6:F6").Copy wsDest.Range("C" & lDestLastRow)
    
End Sub
编辑:
Sub Copy_Paste_Below_Last_Cell1()
    
    Dim wsDest As Worksheet
    Dim lDestLastRow As Long
    
    Set wsDest = Workbooks("Destination.xlsx").Worksheets("DataBase")
    lDestLastRow = wsDest.Cells(wsDest.Rows.Count, "C").End(xlUp).Offset(1).Row
    
    If sheetWithVariable.CellWithVariable.Value = False Then
        Sheet4.Range("B6:F6").Copy
        wsDest.Range("C" & lDestLastRow).PasteSpecial Paste:=xlPasteValues
        sheetWithVariable.CellWithVariable.Value = True
    End If
    
End Sub

最佳答案

任务:从主工作簿复制并粘贴到目标工作簿
无需重复数据。
这应该这样做。在尝试之前调整代码的配置部分。

Sub TransferData()

Dim main_wb As Workbook, target_wb As Workbook, main_sheet As String
Dim r As String, target_sheet As String, first_col As Byte, col_n As Byte
Dim next_row As Long, duplicates As Byte, pasted As Byte, last_col As Long

'CONFIG HERE
'------------------------
Set main_wb = ThisWorkbook
main_sheet = "Sheet1"
r = "B6:F6" 'range to copy in the main Workbook

'target workbook path
Set target_wb = _
Workbooks.Open("/Users/user/Desktop/target workbook.xlsm")

target_sheet = "Sheet1"
first_col = 3 'in what column does the data starts in target sheet?
'-------------------------

'turn screen updating off
Application.ScreenUpdating = False

'copy from main
main_wb.Sheets(main_sheet).Range(r).Copy

With target_wb.Sheets(target_sheet)

    'target info
    next_row = _
    .Cells(Rows.Count, first_col).End(xlUp).Row + 1
    
    'paste in target
    .Cells(next_row, first_col).PasteSpecial xlPasteValues
    
    last_col = _
    .Cells(next_row, Columns.Count).End(xlToLeft).Column

End With

pasted = last_col - (first_col - 1)

For col_n = first_col To last_col

    With target_wb.Sheets(target_sheet)

        If .Cells(next_row, col_n) = .Cells(next_row - 1, col_n) Then
             
             duplicates = duplicates + 1
             
        End If

    End With

Next col_n

If duplicates = pasted Then 'if the nº of cells pasted equals duplicates
    
    For col_n = first_col To last_col  'erase pasted range
        target_wb.Sheets(target_sheet).Cells(next_row, col_n).Clear
    Next col_n
    
End If

'turn screen updating back on
Application.ScreenUpdating = True

End Sub

关于excel - 如何从一个工作簿复制数据并将值仅粘贴到另一个工作簿中并允许宏只运行一次?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65804700/

相关文章:

c# 将文件从源文件夹复制到目标文件夹

立即设置文本字段值(无需模拟输入)

excel - 使用过滤和聚合函数查询预先存在的内存中 ADODB 记录集(高级 ADODB 库?)

excel - 如何从单独的 Excel 工作表 VBA 中删除最后一行

vba - 如何对多个复选框执行相同的代码来设置复选框值?

vba - 数据透视表报告 - 获取切片器、图表和过滤器信息

vba - 如何防止 Excel 加载项文件将单元格引用更改为 R1C1/列更改为字母

excel - VBA Excel获取表格的最后一个 child

VBA:Excel循环 - 隐藏/取消隐藏工作表

javascript 寻找阻止用户复制和粘贴文本的方法