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

How to copy the data from one Workbook and paste the value only in another Workbook and allow macro to run only one time?

当我不小心 运行 VBA 多次将一个工作簿中的数据 copy/paste 编码到目标工作簿时,它会在目标工作表中创建具有相同数据的多行。

我希望 VBA 代码识别上一行是相同的,以防止数据重复。

此外,我的 VBA 代码会将公式复制到我的目标 Excel 文件。
我只想复制值而不是公式。我不确定如何在我的 VBA 代码中使用 PasteSpecial

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

转到“开发人员选项卡”,然后按“录制宏”或在 Excel 左下方有一个小按钮“录制宏”。然后你按下它,它会为你的每次点击、按下等自动创建代码,所以只复制和粘贴值,停止录制宏。您将拥有 Module1,其中包含如何“粘贴值”的代码。

对于PasteSpecial函数,复制和粘贴被定义为不同的操作(也就是说,Copy不应该使用Destination选项):

Sheet4.Range("B6:F6").Copy
wsDest.Range("C" & lDestLastRow).PasteSpecial _ 
  Paste:=xlPasteValues

如果您希望代码为 运行 一次,请在工作簿中的某处添加一个变量,该变量将指定代码已经 运行。类似的东西:

Sub Copy_Paste_Below_Last_Cell()
  If sheetWithVariable.CellWithVariable.Value = False Then
    ' Put your code here
    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