从源列表中工作表的行(SLW)个列(1,2 & 3)需要粘贴到主列表的工作表(MLW)列(3,4 & 5)[相同的顺序]如果ID号码(SLW1 = MLW3)确实不是已经存在于“主列表”(同一工作簿)中。 我的第一个Excel VBA项目。所以任何建议/建议/更正/捷径都会很棒。这段代码是我摸索创建的。如你所知,它不工作。添加从源工作表唯一的数据来掌握工作表
Sub Transfer()
Dim SLR As Integer 'SourceList's Woksheets Last Row
Dim MLR As Integer 'MasterList's Woksheets Last Row
Dim SC As Integer 'SourceList Counting through the loop (ROW NUMBER)
Dim SR As Range 'SourceList A-C Row data
'(Source information 3 rows to be transfered)
Dim ID As Integer 'Unique code of Projects
Dim Found As Range
Sheets("SourceList").Activate
SLR = Cells(Rows.Count, "A").End(xlUp).Row
'Start loop to go through SourceList unique ID numbers
For SC = 2 To SLR
'Copy SourceList ID number into Variable "ID"
ID = Sheets("SourceList").Range(1, SC)
'Also, Save Range into Variable so it doesn't have to
'go back and forth between Worksheets
Set SR = Range(Cells(1, SC), Cells(3, SC))
Sheets("MasterList").Activate
Found = Columns("C:C").Find(What:=ID, After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate
If Found Is Nothing Then
MLR = Cells(Rows.Count, "C").End(xlUp).Row + 1
Range(Cells(3, MLR)) = SR
SR.ClearContents
End If
Sheets("SourceList").Activate
Next SC
End Sub
[Check this out](http://stackoverflow.com/questions/10714251/how-to-avoid-using-select-in-excel-vba-macros)作为开始。一旦你明确地引用了所有的对象,你就会更接近你想要的东西。还强制变量声明。您可以在* VBE>工具>选项*中启用它,或者您可以简单地在代码最上面添加以下代码:'Option Explicit' – L42 2015-03-24 22:54:05