You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用VBA粘贴值而非公式?含指定VLOOKUP公式取值需求

How to Paste Values (Not Formulas) with VBA, Including Your VLOOKUP Scenario

Let’s break this down into two parts: first, the general approach to pasting values instead of formulas in VBA, then a tailored solution for your specific VLOOKUP formula use case.

1. General Methods to Paste Values in VBA

There are two efficient ways to do this—one mirrors the Excel UI’s "Paste Values" action, and the other skips copy-paste entirely for faster performance:

Method 1: Using PasteSpecial

This is great if you’re copying from one range to another, just like you’d do manually:

' Example: Copy B2:B100 and paste values to D2:D100
Range("B2:B100").Copy
Range("D2:D100").PasteSpecial Paste:=xlPasteValues
Application.CutCopyMode = False ' Clear the copy buffer to free memory

Method 2: Assign Values Directly (Faster for Large Data)

Skip the copy-paste step by setting the target range’s value to match the source’s value directly:

' Example: Set D2:D100 to the values of B2:B100
Range("D2:D100").Value = Range("B2:B100").Value

2. Specific Solution for Your VLOOKUP Formula

You want to calculate the result of this formula and paste the value into a target cell (I’ll assume you mean cell A6318 based on the formula’s lookup value—adjust the target cell as needed):

=IFERROR(VLOOKUP($A6318,'https://-/-/-/-/-/-/-/[-.xlsx]-'!$A:$BO,2,FALSE),"")

Instead of inserting the formula then converting it to a value, we can compute the result directly in VBA and assign it to the cell. Here’s the code:

Sub PasteVLookupResultAsValue()
    Dim targetCell As Range
    Dim lookupValue As Variant
    Dim sourceWorkbook As Workbook
    Dim sourceRange As Range
    Dim result As Variant
    
    ' Set your target cell (update sheet name and cell reference to match your needs)
    Set targetCell = ThisWorkbook.Sheets("YourSheetName").Range("A6318")
    
    ' Get the lookup value from cell A6318 (matches your formula's $A6318)
    lookupValue = targetCell.Value
    
    On Error Resume Next ' Handle cases where the external workbook isn't accessible
    ' Open the external workbook (replace the URL/path with your actual file location)
    Set sourceWorkbook = Workbooks.Open("https://-/-/-/-/-/-/-/-.xlsx")
    On Error GoTo 0
    
    If Not sourceWorkbook Is Nothing Then
        ' Define the source range from the external workbook (update sheet name)
        Set sourceRange = sourceWorkbook.Sheets("ExternalSheetName").Range("A:BO")
        
        ' Perform the VLOOKUP calculation in VBA
        result = Application.VLookup(lookupValue, sourceRange, 2, False)
        
        ' Handle errors (equivalent to the IFERROR in your formula)
        targetCell.Value = IIf(IsError(result), "", result)
        
        ' Close the external workbook without saving changes
        sourceWorkbook.Close SaveChanges:=False
    Else
        ' If the workbook couldn't open, set target cell to empty and notify
        targetCell.Value = ""
        MsgBox "Failed to open the external workbook. Check permissions or file path.", vbExclamation
    End If
End Sub

Quick Adjustments You’ll Need to Make:

  • Replace YourSheetName with the name of the sheet where your target cell lives.
  • Replace ExternalSheetName with the actual sheet name from the external workbook.
  • Double-check the external workbook’s URL/path to ensure VBA can access it (for cloud files, confirm you’re signed in with proper permissions).

If the formula is already in a cell and you just want to convert it to a value, use this simple one-liner:

' Replace "YourFormulaCell" with the cell containing your VLOOKUP formula
Range("YourFormulaCell").Value = Range("YourFormulaCell").Value

内容的提问来源于stack exchange,提问作者Lim Yi Long

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.12 05:13:17