2014-01-18 182 views
0

我有這個代碼,但它複製公式,我只有值。 我對VBA不太瞭解。複製並粘貼值

Dim sh4 As Worksheet, sh5 As Worksheet, lr As Long, rng As Range 

Set sh4 = Sheets("Transfer_New") 
Set sh5 = Sheets("Closed_Loans") 
lr = sh4.Cells(Rows.Count, 1).End(xlUp).Row 
Set rng = sh4.Range("A2:A" & lr) 

Application.Cursor = xlHand 

iReply = MsgBox(Prompt:="Are you sure you want to transfer client to CLOSED_LOANS?", _ 
    Buttons:=vbYesNo, Title:="Document Production") 
     If iReply = vbYes Then 
      Application.Cursor = xlWait: rng.EntireRow.Copy sh5.Cells(Rows.Count, 1).End(xlUp)(2): Application.Cursor = xlNorthwestArrow      
     End If 
End Sub 

回答

0

更改此:

If iReply = vbYes Then 
     Application.Cursor = xlWait: rng.EntireRow.Copy sh5.Cells(Rows.Count, 1).End(xlUp)(2): Application.Cursor = xlNorthwestArrow      
    End If 

爲了這個(沒有很好的理由 - 當然不是爲了易讀性/透明度 - 把多行代碼在同一行(這是什麼:分離器一樣)

If iReply = vbYes Then 
     Application.Cursor = xlWait 
     rng.EntireRow.Copy sh5.Cells(Rows.Count, 1).End(xlUp)(2) 
     Application.Cursor = xlNorthwestArrow      
    End If 

然後改成這樣:

Dim destRng as Range 
    Set destRng = sh5.Cells(Rows.Count, 1).End(xlUp).Offset(1,0) 
    If iReply = vbYes Then 
     Application.Cursor = xlWait 
     destRng.Value = rng.Value 
     Application.Cursor = xlNorthwestArrow      
    End If 
0

如果要粘貼值,試試這個:

rng.Entirerow.Copy: sh5.Cells(Rows.Count, 1).End(xlUp).Offset(1,0).PasteSpecial xlPasteValues 

我認爲是發生了什麼只有在你的代碼失蹤。