我在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 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
答案 0 :(得分: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 :(得分: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