2017-05-24 243 views
0

如果我们能够得到一些帮助,将不胜感激! 因此,我试图从容量列表中的“CNC DEPT”和“列A”(作业编号)中比较“列A”(作业编号)。然后比较“B列”(容量主机)到“K列”(CNC部门当前的WC关闭)。如果这两个都是True,我们希望复制N列(总时间)并将其粘贴到“列P”(无CNC DEPT上的数据)上。我遇到下面列出的代码有问题。语法没问题,但它返回的值为#### VALUE。代码从第二个工作表复制一个值并将其粘贴到当前工作表中。被复制的单元格从其他表单上的VLOOKUP函数派生出其值。我认为这可能会造成问题。有没有办法只复制单元格的数字值 而不是公式?或者只粘贴数值(如仅粘贴特殊值菜单项)?用VBA Excel比较,复制和粘贴

这里是我的代码现在

显式的选项

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 

末次 CNC DEPT CAPACITY

+0

仅供参考 - 与你一样用'Range()'做,每当你使用'Cells()'时,一定要参考你希望运行的工作表。这有点不直观,但是执行'Worksheets(“Sheet1”)。Range(Cells(),Cells())'需要'Cells()'之前的工作表,即使它来自Range()。 – BruceWayne

回答

1

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

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