2017-07-31 43 views
1

我在VBA中有点新。我想要做的是在一行(n)中找到单词“材料”,从上面复制它的所有单元格,并将它们粘贴到列A中的另一个表单中。arr2 - 列将使用相同的功能但具有不同的话。 从我的代码,我不断收到错误。你能帮我解决代码吗?查找“材质”,复制它的单元格并粘贴到另一个工作表中

Dim t As Range 
    n = InputBox("Row number of FIRST MATERIAL") 
    arr2 = Array("A", "D", "E", "O", "P", "S", "W", "Y", "AB") 
    Set t = Sheets("Project Parts Requisitioning").Rows(n).Find("*Material*", LookAt:=xlWhole) 
     If t Is Nothing Then 
     MsgBox ("Material was not found") 
     End If 

     If Not t Is Nothing Then 
     Sheets("Project Parts Requisitioning").Rows(n).Find("*Material*", LookAt:=xlWhole).End(xlDown).Copy 
     Sheets("GCC1").Column("A").PasteSpecial xlPasteValues 

     End If 
+2

什么线你得到的错误?什么是错误? – Zac

+1

为什么你创建一个数组而不是在代码中引用它? – Luuklag

回答

1

的问题如下:

这一行:

Sheets("Project Parts Requisitioning").Rows(n).Find("*Material*", _ 
LookAt:=xlWhole).End(xlDown).Copy 

您复制的最后一个单元在给定的表。例如。行1048576或更低的单元格,从找到的一个。但是你只复制一个单元格。 然后用下一行

Sheets("GCC1").Column("A").PasteSpecial xlPasteValues 

您尝试将此单元格粘贴到列中。这不会发生。


一般来说,尝试将代码重写成某个东西,任何人都可以轻松地重现。那么错误会更明显。就像这样:

Option Explicit 
Public Sub TestMe() 

    Dim n    As Long 
    Dim t    As Range 
    Dim arr2   As Variant 
    Dim strToLookFor As String: strToLookFor = "*Material*" 

    n = 11 'you do not need an input box for testing purposes 

    'How do you use this array? 
    arr2 = Array("A", "D", "E", "O", "P", "S", "W", "Y", "AB") 

    Set t = Worksheets(1).Rows(n).Find(strToLookFor, LookAt:=xlWhole) 

    If t Is Nothing Then 
     Debug.Print ("Material was not found") 'No msgbox when testing 
    End If 

    If Not t Is Nothing Then 
     'you copy only one cell here 
     Worksheets(3).Rows(n).Find(strToLookFor, LookAt:=xlWhole).End(xlDown).Copy 

     'but you try to paste it in a column? 
     Worksheets(4).Column("A").PasteSpecial xlPasteValues 
    End If 

End Sub 
1

试试这个

Sub testso1() 

Dim t As Range 
    n = InputBox("Row number of FIRST MATERIAL") 
    arr2 = Array("A", "D", "E", "O", "P", "S", "W", "Y", "AB") 
    Set t = Sheets("Project Parts Requisitioning").Rows(n).Find("*Material*", LookAt:=xlWhole) 

    If Not t Is Nothing Then 
     Sheets("GCC1").Columns("A") = t.EntireColumn.Value 
    Else 
     MsgBox ("Material was not found") 
    End If 

End Sub 
相关问题