2014-09-24 112 views
0

我正在嘗試創建一個審計跟蹤數據庫,並設法制定一些代碼將每行代碼分配到第2張表格,但是我已倒在最後,並且無法計算出如何粘貼值?粘貼特殊值 - 僅限值

這是我的代碼到目前爲止;任何幫助非常感謝

Private Sub Worksheet_Change(ByVal Target As Range) 
    Dim nxtRow As Integer, b As Boolean 
    'Determine if change was to Column I (9) 
    If Target.Column = 9 Then 
     'If Yes, Determine if cell >= 1 
     If IsError(Target.Value) Then 
     b = True 
    Else 
     If Target.Value >= 1 Then 
      b = True 
     Else 
      b = False 
     End If 
     End If 
     If b Then 
     'If Yes, find next empty row in Sheet 2 
     nxtRow = Sheets(2).Range("A" & Rows.Count).End(xlUp).Row + 1 
     'Copy changed row and paste into Sheet 2 
     Target.EntireRow.Copy _ 
     Destination:=Sheets(2).Range("A" & nxtRow) 
      End If 
    End If 
End Sub 

感謝 馬特

+0

我注意到在你的代碼中的一些嚴重的錯誤,但我不認爲如果我糾正它們可以解決您的問題。無論如何,要我把它作爲答案? – 2014-09-24 11:14:05

+0

是的,越穩定越好,從谷歌的工作中把這個拉到一起 – Matt 2014-09-24 11:18:47

回答

0

這可能不會解決問題,但它會提高你的代碼。
你打開一些If語句,但你不關閉一些會讓你的代碼做別的事情比你想要的。

Private Sub Worksheet_Change(ByVal Target As Range) 
    Dim nxtRow As Integer, b As Boolean 
    'Determine if change was to Column I (9) 
    If Target.Column = 9 Then 
     'If Yes, Determine if cell >= 1 
     If IsError(Target.Value) Then ' You open this If but you don't close it 
     b = True 
     'if you don't close it here the next line (else) will be the else of this if 
     End If 
    Else 
     If Target.Value >= 1 Then 
      b = True 
     Else 
      b = False 
     End If 
     'this line had an else if as well. which would just stop your main if statement 
     If b = True Then 
     'you say 'if b then' on the line above, which basically does nothing 
     'If you want to check if b = True for example, do what I did above 

     'If Yes, find next empty row in Sheet 2 
      nxtRow = Sheets(2).Range("A" & Rows.Count).End(xlUp).Row + 1 
      'Copy changed row and paste into Sheet 2 
      Target.EntireRow.Copy _ 
      Destination:=Sheets(2).Range("A" & nxtRow) 
     End If 
    End If 
End Sub 
1

要粘貼值,您可以複製到剪貼板中,然後用PasteSpecial方法,例如:

Target.EntireRow.Copy 
Sheets(2).Range("A" & nxtRow).PasteSpecial Paste:=xlPasteValues 
+0

我在這個上得到一個編譯錯誤 - 無效的外部程序。我是否將其粘貼到此工作簿或添加到我的代碼? – Matt 2014-09-24 12:15:48

+0

對不起,我沒有仔細閱讀你的代碼!你可以使用這個,但你必須通過剪貼板複製 - 參見編輯答案。 – nekomatic 2014-09-24 12:24:10

+0

非常感謝 – Matt 2014-09-24 12:52:03