2017-05-24 244 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