2014-09-24 107 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