[英]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.