![](/img/trans.png)
[英]Copy cells and paste by overwriting cells in another sheet based on a criteria
[英]Find “Material”, copy it's cells and paste into another sheet
我對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
問題如下:
用這行:
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.