粘贴行值而不是公式

时间:2018-05-24 11:12:59

标签: excel vba excel-vba

您好我一直在谷歌搜索一段时间,但目前无法找到我特定问题的解决方案。所以问题很简单我想选择并复制和粘贴值而不是生成值的公式。

我检查过: Copy Entire Row (Values not formulas) VBA

https://msdn.microsoft.com/en-us/library/aa195818(v=office.11).aspx

我正在使用的当前代码是下面我目前正在使用“OutputWorksheet.Paste”,它正在粘贴行但是它还带有公式并尝试了“OutputWorksheet.PasteSpecial.xlPasteValues”但它不断抛出“编译错误” :预期函数或变量“

任何帮助都会非常感激,或者更好的方法是在一张工作表中根据cboMADropDown值找到一行,复制行并粘贴到另一张只有值没有公式的工作表。

With InputWorksheet                                                     ' Set sheet we want to search
Set FindRow = .Range("O:O").Find(What:=cboMADropDown, LookIn:=xlValues) ' Set FindRow container by searching Column F with cboMADropDown Value and look for the cell value to compare
End With                                                                ' End the With Statement
FindRowNumber = FindRow.Row                                             ' Set the FindRow row value to a number


InputWorksheet.Rows(FindRowNumber).EntireRow.Copy                       'Grab contents of entire row which is going to be updated
LastRow = OutputWorksheet.Range("A" & Rows.Count).End(xlUp).Row         'Finds the last blank Row on the MA tracking history sheet

OutputWorksheet.Activate                                                'Activate the worksheet so the .select command works with no errors
OutputWorksheet.Rows(LastRow).Select                                    'Select last blank row from MA tracking history
OutputWorksheet.Paste                        'Paste contents of the clipboard to backup
OutputWorksheet.Range("O" & LastRow).Value = TimeStamp                  'Add time stamp for the update

1 个答案:

答案 0 :(得分:0)

这里只有我的2美分......你的lastrow给你最后一排,而不是第一排......如果我没错?所以你可能需要这样的东西来实际得到第一个空行:

LastRow = OutputWorksheet.Range("A" & Rows.Count).End(xlUp).Row + 1

然后,您不需要复制/粘贴值,只需执行rangeB.Value = rangeA.Value - 这只会复制值,而不是公式。在您的情况下,这将是这样的:

With InputWorksheet                                                     ' Set sheet we want to search
    Set FindRow = .Range("O:O").Find(What:=cboMADropDown, LookIn:=xlValues) ' Set FindRow container by searching Column F with cboMADropDown Value and look for the cell value to compare
End With                                                                ' End the With Statement
FindRowNumber = FindRow.Row                                             ' Set the FindRow row value to a number

With OutputWorksheet
    LastRow = .Range("A" & Rows.Count).End(xlUp).Row                 'Finds the last blank Row on the MA tracking history sheet
    .Rows(LastRow).Value = InputWorksheet.Rows(FindRowNumber).Value
    .Range("O" & LastRow).Value = TimeStamp                  'Add time stamp for the update
End With

值得一提的是,鉴于你并不真正需要整行(我可能在这里错了),你可以,更好地更清楚地指定范围(除非你有超出列“O”的更多值:

FindRowNumber = InputWorksheet.Range("O:O").Find(What:=cboMADropDown, LookIn:=xlValues).Row 'assign the row number directly to the variable (as with the LastRow, unless you need differently for other reasons)
With OutputWorksheet
    LastRow = .Range("A" & Rows.Count).End(xlUp).Row
    .Range("A" & LastRow & ":N" & LastRow).Value = InputWorksheet.Range("A" & FindRowNumber & ":N" & FindRowNumber).Value
    .Range("O" & LastRow).Value = TimeStamp
End With