簡體   English   中英

如何從一個工作簿復制數據並將值僅粘貼到另一個工作簿中並允許宏只運行一次?

[英]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 代碼多次將數據從一個工作簿復制/粘貼到目標工作簿時,它將在目標工作表中創建具有相同數據的多行。
在此處輸入圖像描述

我想讓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

任務:從主工作簿復制並粘貼到目標工作簿中,而不復制數據。

這應該這樣做。 在嘗試之前調整代碼的配置部分。

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

Go “開發人員選項卡”然后按“錄制宏”或在 Excel 左下方有一個小按鈕“錄制宏”。 然后你按下它,它會為你的每次點擊、按下等自動創建代碼,所以 go 只復制和粘貼值,停止錄制宏。 您將擁有 Module1 以及如何“粘貼值”的代碼。

對於PasteSpecial function,復制和粘貼被定義為不同的操作(也就是說,不應為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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM