使用VBA Excel进行比较,复制和粘贴

如果我们能得到一些帮助,将不胜感激! 因此,我试图比较“容量列表”中“CNC DEPT”和“A列”(作业编号)的“A列”(作业编号)。 然后比较“B列”(容量主机)到“K列”(CNC部门当前closures)。 如果这两个都是真的,我们想复制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 

End Sub CNC DEPT CAPACITY

您可以直接将一个单元格的值分配给另一个单元格:

 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