我根据列标题复制和粘贴代码时出现问题(本帖底部包含的代码)。
宏应搜索列标题,然后复制该标题下的数据并将数据粘贴到新位置(在新工作簿的不同标题下)。代码适用于大多数情况,因为我用它来复制某些列中的数据(例如A,B,C都在同一个工作表中),并且代码工作正常。我认为粘贴目标也很好,因为我可以运行宏来复制这些列中的数据(例如A,B,C)并粘贴到我的目的地。但是,出于某种原因,当我尝试从F列复制数据并粘贴到新工作表中时,没有任何反应。我甚至没有收到错误消息。因为我可以从其他列复制,并粘贴到目标列,我的想法是错误是与marco的副本部分。
我已经检查了很明显,我的代码中的列标题与文档中的列标题完全匹配,等等。我的代码是否有问题,或者是否有可能存在问题阻止某些数据被复制的源工作表?
Sub ProjectCopy()
Dim sourceWS As Worksheet, targetWS As Worksheet
Dim lastCol As Long, lastRow As Long, srcRow As Range
Dim found1 As Range, found2 As Range
Set sourceWS = Workbooks("Workbook1.xlsx").Worksheets("Sheet1") 'Needs to be open
Set targetWS = Workbooks("Workbook2.xlsm").Worksheets("Sheet1") 'Needs to be open
With sourceWS
lastCol = .Cells(1, Columns.count).End(xlToLeft).Column
Set srcRow = .Range("A1", .Cells(1, lastCol))
Set found1 = srcRow.Find(What:="Project", LookAt:=xlWhole, MatchCase:=False)
If Not found1 Is Nothing Then
lastCol = targetWS.Cells(1, Columns.count).End(xlToLeft).Column
Set srcRow = targetWS.Range("A1", targetWS.Cells(1, lastCol))
Set found2 = srcRow.Find(What:="Activity", LookAt:=xlWhole, MatchCase:=False)
If Not found2 Is Nothing Then
lastRow = .Cells(Rows.count, found1.Column).End(xlUp).Row
.Range(.Cells(3, found1.Column), .Cells(lastRow, found1.Column)).Copy
found2.Offset(1, 0).PasteSpecial xlPasteAll
End If
End If
End With
End Sub