VBA VLOOKUP复制/粘贴

时间:2014-05-29 00:52:03

标签: vba vlookup

我有一个VBA代码,可以复制和粘贴页面并正确复制所有内容,除了任何具有vlookup的单元格。我不知道问题是什么,或者是否有一些滞后时间。任何想法都会有所帮助。

Dim thisSheet As Worksheet
Set thisSheet = Application.ActiveSheet

Dim templateSheet As Worksheet
Set templateSheet = Worksheets("Template ")

Dim baseSheet As Worksheet
Set baseSheet = Worksheets("Base")

Dim wrkBook As Workbook
Set wrkBook = ActiveSheet.Parent

Dim wrkSheet As Worksheet

Dim sNewSheetName As String
Dim nRow As Integer

Application.ScreenUpdating = False

Dim rRunRange As Range
Set rRunRange = Range("Cohorts_to_Run")

Dim n As Integer

For nRow = 1 To rRunRange.Rows.Count

    sNewSheetName = rRunRange.Cells(nRow, 2).Value

    Set wrkSheet = Worksheets.Add(, Worksheets(Worksheets.Count))
    wrkSheet.Name = sNewSheetName

    'Set parameters to pickup data....
    Dim zZoom As Double
    zZoom = ActiveWindow.Zoom

    templateSheet.Activate
    templateSheet.Range(baseSheet.Range("TEMPLATE_COPY_RANGE").Value).Copy

    wrkSheet.Activate
    wrkSheet.PasteSpecial xlPasteColumnWidths
    wrkSheet.Paste
    ActiveWindow.Zoom = baseSheet.Range("ZOOM_LEVEL").Value

    wrkSheet.Range("A1").RowHeight = 10

    Application.CutCopyMode = False


    wrkSheet.Range("B2").Value = Range("Cohorts_to_Run").Cells(nRow, 1).Value

    Dim sTitleRangeName As String
    sTitleRangeName = sNewSheetName & "_Title"

    wrkSheet.Range(baseSheet.Range("TEMPLATE_COPY_RANGE").Value).Select
    Selection.Copy
    wrkSheet.Range(baseSheet.Range("TEMPLATE_COPY_RANGE").Value).PasteSpecial         
    xlPasteValuesAndNumberFormats
 Next nRow

1 个答案:

答案 0 :(得分:0)

我认为你的困惑是最后一行只粘贴值并删除任何公式。

wrkSheet.Range(baseSheet.Range("TEMPLATE_COPY_RANGE").Value).PasteSpecial xlPasteValuesAndNumberFormats

为什么不粘贴? xlPasteValuesAndNumberFormats每次都会移除您的公式。这才是重点。

Dim copyRange as Range, pasteRange as Range
Set copyRange = wrkSheet.Range(baseSheet.Range("TEMPLATE_COPY_RANGE").Value)
Set pasteRange = wrkSheet.Range(baseSheet.Range("TEMPLATE_COPY_RANGE").Value)
copyRange.Copy pasteRange

你的wrkSheet.Range(baseSheet.Range("TEMPLATE_COPY_RANGE").Value)语法令人困惑。您的指定范围TEMPLATE_COPY_RANGE中是否有其他范围的名称?