简体   繁体   English

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

[英]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. 因此,我正在尝试比较容量列表中“ CNC DEPT”中的“ A列”(作业编号)和“ CNC DEPT”中的“ A列”(作业编号)。 Then compare "Column B" (MainMachine from capacity) to "Column K" (Current WC off of CNC Dept). 然后将“ B列”(容量的主要机器)与“ K列”(CNC部门的当前WC关闭)进行比较。 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). 如果这两个都是True,我们想复制N列(总时间)并将其粘贴到“ P列”(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. 语法不错,但是它返回的值是#### VALUE。代码从第二个工作表中复制一个值并将其粘贴到当前工作表中。 The cell being copied derives its value from a VLOOKUP function on a different sheet. 要复制的单元格从另一张纸上的VLOOKUP函数获得其值。 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 结束小组CNC DEPT 能力

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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