【发布时间】:2017-05-24 22:51:17
【问题描述】:
如果我们能得到一些帮助,我们将不胜感激! 因此,我正在尝试比较“CNC DEPT”中的“A 列”(作业编号)和“容量列表”中的“A 列”(作业编号)。然后将“B 列”(来自容量的 MainMachine)与“K 列”(CNC 部门的当前 WC)进行比较。如果这两个都是 True,我们想复制 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
【问题讨论】:
-
仅供参考 - 正如您使用
Range()所做的那样,无论何时使用Cells(),请务必参考您希望它运行的工作表。这有点不直观,但是做Worksheets("Sheet1").Range(Cells(),Cells())需要Cells()之前的工作表,即使它来自Range()。