简体   繁体   中英

Compare, Copy and Paste with VBA Excel

It would be much appreciated if we could get some help with this! So, I'm trying to compare "Column A"(Job Number) from the "CNC DEPT" and "Column A" (Job Number)from the Capacity List. Then compare "Column B" (MainMachine from capacity) to "Column K" (Current WC off of CNC Dept). If those are both True, we would like to Copy Column N (Total Time) and paste it onto "Column P" (No data on CNC DEPT). I am having problems with the code listed below. The syntax is fine, but it returns a value of ####VALUE.The code copies a value from a second sheet and pastes it into the current sheet. The cell being copied derives its value from a VLOOKUP function on a different sheet. I think this may be creating the problem. Is there a way to copy a cell's numeric value only and not the formula? Or maybe paste the value only (like the Paste Special Values Only menu item)?

Here's my CODE NOW

Option Explicit

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

You can directly assign a value from one cell to another cell:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM