Vba 查找“;“材料”;,复制它';s单元格并粘贴到另一张表中
我对VBA有点陌生。我想做的是在一行(n)中找到单词“Material”,从上面复制它的所有单元格,并将它们粘贴到a列的另一张表中。arr2-是使用相同功能但具有不同单词的列。 从我的代码中,我不断收到错误。你能帮我修一下密码吗Vba 查找“;“材料”;,复制它';s单元格并粘贴到另一张表中,vba,excel,Vba,Excel,我对VBA有点陌生。我想做的是在一行(n)中找到单词“Material”,从上面复制它的所有单元格,并将它们粘贴到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
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
问题如下: 这一行:
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
试试这个
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
你在哪一行收到错误?错误是什么?为什么要创建一个数组而不在代码中引用它?