[英]Compare, Copy and Paste with VBA Excel
如果能在此方面獲得幫助,將不勝感激! 因此,我正在嘗試比較容量列表中“ CNC DEPT”中的“ A列”(作業編號)和“ CNC DEPT”中的“ A列”(作業編號)。 然后將“ B列”(容量的主要機器)與“ K列”(CNC部門的當前WC關閉)進行比較。 如果這兩個都是True,我們想復制N列(總時間)並將其粘貼到“ P列”(CNC DEPT上沒有數據)。 我下面列出的代碼有問題。 語法不錯,但是它返回的值是#### VALUE。代碼從第二個工作表中復制一個值並將其粘貼到當前工作表中。 要復制的單元格從另一張紙上的VLOOKUP函數獲得其值。 我認為這可能是問題所在。 有沒有辦法只復制一個單元格的數值而不是公式? 還是僅粘貼值(例如僅粘貼特殊值菜單項)?
這是我的代碼
顯式期權
Sub transfer()
Dim i As Long
Dim j As Long
Dim lastrow1 As Long
Dim lastrow2 As Long
Dim jobnum As String
Dim mainmachine As String
Dim WBT As Workbook ''''This Workbook
Dim WBC As Workbook ''''CapacitySummary workbook
Set WBT = Workbooks("CNC TEST.xlsx")
Set WBC = Workbooks("CapacitySummary.xlsx")
lastrow1 = WBT.Worksheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Row
lastrow2 = WBC.Worksheets("DATA").Range("A" & Rows.Count).End(xlUp).Row
WBT.Worksheets("sheet1").Activate
For i = 2 To lastrow1
jobnum = WBT.Sheets("Sheet1").Cells(i, "A").Value
mainmachine = WBT.Sheets("Sheet1").Cells(i, "K").Value
WBC.Worksheets("DATA").Activate
For j = 2 To lastrow2
If WBC.Worksheets("DATA").Cells(j, "A").Value = jobnum And WBC.Worksheets("DATA").Cells(j, "B").Value = mainmachine Then '' CapacitySummary workbook
WBC.Worksheets("DATA").Activate
WBC.Worksheets("DATA").Range(Cells(i, "N"), Cells(i, "N")).Copy
''''Choosing Range to copy
WBT.Worksheets("Sheet1").Activate
WBT.Worksheets("Sheet1").Range(Cells(j, "P"), Cells(j, "P")).Select
'Range.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
''''Choosing Range to paste
End If
Next j
Application.CutCopyMode = False
Next i
'Application.ScreenUpdating = True
'Stoptime = Time
'elapsedTime = (Stoptime - StartTime) * 24 * 60
'MsgBox "List Complete " & elapsedTime & " minutes. " & Chr(13)
'findConn.Close
End SubBC As Workbook ''''CapacitySummary workbook
Set WBT = Workbooks("CNC TEST.xlsx")
Set WBC = Workbooks("CapacitySummary.xlsx")
lastrow1 = WBT.Worksheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Row
lastrow2 = WBC.Worksheets("DATA").Range("A" & Rows.Count).End(xlUp).Row
WBT.Worksheets("sheet1").Activate
For i = 2 To lastrow1
jobnum = WBT.Sheets("Sheet1").Cells(i, "A").Value
mainmachine = WBT.Sheets("Sheet1").Cells(i, "K").Value
WBC.Worksheets("DATA").Activate
For j = 2 To lastrow2
If WBC.Worksheets("DATA").Cells(j, "A").Value = jobnum And WBC.Worksheets("DATA").Cells(j, "B").Value = mainmachine Then '' CapacitySummary workbook
WBC.Worksheets("DATA").Activate
WBC.Worksheets("DATA").Range(Cells(i, "N"), Cells(i, "N")).Copy
''''Choosing Range to copy
WBT.Worksheets("Sheet1").Activate
WBT.Worksheets("Sheet1").Range(Cells(j, "P"), Cells(j, "P")).Select
'Range.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
''''Choosing Range to paste
End If
Next j
Application.CutCopyMode = False
Next i
'Application.ScreenUpdating = True
'Stoptime = Time
'elapsedTime = (Stoptime - StartTime) * 24 * 60
'MsgBox "List Complete " & elapsedTime & " minutes. " & Chr(13)
'findConn.Close
您可以將一個單元格中的值直接分配給另一個單元格:
If WBC.Worksheets("DATA").Cells(j, "A").Value = jobnum And _
WBC.Worksheets("DATA").Cells(j, "B").Value = mainmachine Then
WBT.Worksheets("Sheet1").Cells(j, "P").Value = _
WBC.Worksheets("DATA").Cells(i, "N").Value
End If
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.