无法仅粘贴VBA中的值

时间:2015-02-17 18:39:41

标签: excel vba excel-vba

我正在尝试仅将值粘贴到目标单元格而不是公式,并且在google搜索后没有取得任何成功。这是我第一次真正进入VBA,所以我不确定下一步该去哪里。我目前的代码是:

Function freeCell(r As Range) As Range
' returns first free cell below r
  Dim lc As Range, Tracking As Worksheet     ' last used cell on sheet
  Set lc = r.Offset(40, 6).End(xlUp).Offset(1, 0)
  Set freeCell = lc
End Function


Sub doIt()
  Dim tCell As Range, sh As Worksheet
  Sheets("Bundler").Range("G20").Copy
  Set sh = Sheets(Range("G73").Value)
  Set tCell = freeCell(sh.Range("A3"))
  If tCell.Row < 19 Then Set tCell = tCell.Offset(19 - tCell.Row)
  sh.Paste tCell
End Sub

sh.Paste tCell粘贴到正确的单元格中,但是如何将其转换为仅值?我知道.PasteSpecial xlPasteValues需要使用,但我不知道如何格式化它。

2 个答案:

答案 0 :(得分:2)

Sub doIt()
  Dim tCell As Range, sh As Worksheet
  Sheets("Bundler").Range("G20").Copy
  Set sh = Sheets(Range("G73").Value)
  Set tCell = freeCell(sh.Range("A3"))
  If tCell.Row < 19 Then Set tCell = tCell.Offset(19 - tCell.Row)
  tCell.PasteSpecial Paste:=xlPasteValues
End Sub

答案 1 :(得分:0)

您无需复制和粘贴即可执行此操作:

Sub doIt()
  Dim tCell As Range, sh As Worksheet
  Set sh = Sheets(Range("G73").Value)
  Set tCell = freeCell(sh.Range("A3"))
  If tCell.Row < 19 Then Set tCell = tCell.Offset(19 - tCell.Row)
  tCell.Formula = Sheets("Bundler").Range("G20").Text
End Sub

如果您希望将其作为值

,请将.text更改为.value