所以我有两张纸。一个是第1行(Sheet1)中带有标题的数据,另一个是第9行(Sheet2)中的标题。我需要使用特定标头从Sheet1复制数据,并将其放在Sheet2中,并带有相应的标头。例如,如果Sheet1中的标题是“bob”,我想复制标题为“bob”的列中的所有数据,并将其放在Sheet2中的硬编码列中。
我的代码目前过滤了Sheet1中的标题,当它选择我需要复制到另一张表的标题时,它会复制整个列并将其粘贴到第一列的Sheet2中。此列将永远是相同的,但我希望信息粘贴到I9而不是I1。我在网上发现了一些有助于修改的代码。
但是,我不能这样做,因为复制整个列会在我尝试粘贴时产生问题,错误1004.所以我只能粘贴到I1中,否则会出现溢出。如何更改我的复制方法,以便只复制带有标题“bob”的列中的单元格,然后粘贴?
代码如下:
Dim ws As Worksheet
Dim aCell As Range, Rng As Range
Dim col As Long, lRow As Long
Dim colName As String
Dim i As Integer
'activate temp worksheet
Set ws = ThisWorkbook.Sheets("Sheet1")
Set res = ThisWorkbook.Sheets("Sheet2")
With ws
Set a = .Range("A1:ZZ1").Find(What:="bob", LookIn:=xlValues, LookAt:=xlWhole, _
MatchCase:=False, SearchFormat:=False)
'if found
If Not a Is Nothing Then
'copy column
a.EntireColumn.Copy
'insert column at I9
Cells("I9").PasteSpecial Paste:=xlPasteValues
Else
MsgBox "Parameter (bob) not found in (Sheet1)"
End If
End With
谢谢!
答案 0 :(得分:1)
尝试:
Dim ws As Worksheet
Dim aCell As Range, Rng As Range, a As Range`
Dim col As Long, lRow As Long
Dim colName As String
Dim i As Integer
'activate temp worksheet
Set ws = ThisWorkbook.Sheets("Sheet1")
Set res = ThisWorkbook.Sheets("Sheet2")
With ws
Set a = .Range("A1:ZZ1").Find(What:="bob", LookIn:=xlValues, LookAt:=xlWhole, _
MatchCase:=False, SearchFormat:=False)
'if found
If Not a Is Nothing Then
'copy column
lRow = .Cells(.Rows.Count, a.Column).End(xlUp).Row
.Range(.Cells(1, a.Column), .Cells(lRow, a.Column)).Copy
'insert column at I9
res.Range("I9").PasteSpecial Paste:=xlPasteValues
Else
MsgBox "Parameter (bob) not found in (Sheet1)"
End If
End With