Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel Vlookup执行后的VBA复制和粘贴_Excel_Vba - Fatal编程技术网

Excel Vlookup执行后的VBA复制和粘贴

Excel Vlookup执行后的VBA复制和粘贴,excel,vba,Excel,Vba,在执行vlookup代码后,是否有更有效的方法来复制和粘贴值?这个想法是,在vlookup在将近10000多个单元格中执行之后,我想复制并粘贴这些值,因为它使excel文件更加稳定 Sub MakeFormulas() Dim SourceLastRow As Long Dim OutputLastRow As Long Dim sourceSheet As Worksheet Dim outputSheet As Worksheet Dim X As Long Dim Z As Long '

在执行vlookup代码后,是否有更有效的方法来复制和粘贴值?这个想法是,在vlookup在将近10000多个单元格中执行之后,我想复制并粘贴这些值,因为它使excel文件更加稳定

Sub MakeFormulas()
Dim SourceLastRow As Long
Dim OutputLastRow As Long
Dim sourceSheet As Worksheet
Dim outputSheet As Worksheet
Dim X As Long
Dim Z As Long

'What are the names of our worksheets?
 Set sourceSheet = Worksheets("Sheet1")
 Set outputSheet = Worksheets("Sheet2")


'Determine last row of source
 With sourceSheet
 SourceLastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
End With
With outputSheet

 'Determine last row in col C
  OutputLastRow = .Cells(.Rows.Count, "C").End(xlUp).Row
 For Y = 17 To 28 'Q to AB
  For X = 2 To OutputLastRow
   If InStr(1, .Range("C" & X), "PO Materials") + InStr(1, .Range("C" & X), "PO Labor") > 0 And    Cells(2, Y) = "Forecast" Then
 'Apply  formula
 .Cells(X, Y).Formula = _
   "=VLOOKUP($E" & X & ",'" & sourceSheet.Name & "'!$A$2:$L$" & SourceLastRow & ",Match(" & Cells(1, Y).Address & ",'" & sourceSheet.Name & "'!$A$1:$AD$1,0),0)"
   .Cells(X, Y).Select
   .Cells(X, Y).Copy
   .Cells(X, Y).PasteSpecial Paste:=xlPasteValues
End If
 Next
Next

End With
End Sub

将.Formula更改为.Value,以便VBA直接执行vlookup,然后粘贴该值

  .Cells(X, y).Value = _
  Evaluate("=VLOOKUP($E" & X & ",'" & sourceSheet.Name & "'!$A$2:$L$" & SourceLastRow & ",Match(" & Cells(1, y).Address & ",'" & sourceSheet.Name & "'!$A$1:$AD$1,0),0)")

为了提高效率,为什么不在for循环的末尾执行复制/粘贴操作呢。那将是一次性操作。是的,没错。但是,我可以让VBA执行vlookup操作,然后将值粘贴到指定的cell.True。谢谢但是,使用.Cells(X,Y).Value=“=VLOOKUP($E”&X&“,”&sourceSheet.Name&“!$A$2:$L$”&SourceLastRow&“,”Match(&Cells(1,Y).Address&“,”&sourceSheet.Name&“!$A$1:$AD$1,0),也可以实现同样的效果。根据您的代码,请注意,您可能需要使用
Application.Evaluate()
(您也可以使用方括号[],它是Application.Evaluate的简写形式)。或者,您可以使用VBAyup中内置的实际Vlookup()函数。您的权限必须为VBA添加一个修饰符才能进行计算。谢谢
  .Cells(X, y).Value = _
  Evaluate("=VLOOKUP($E" & X & ",'" & sourceSheet.Name & "'!$A$2:$L$" & SourceLastRow & ",Match(" & Cells(1, y).Address & ",'" & sourceSheet.Name & "'!$A$1:$AD$1,0),0)")