從源列表中工作表的行(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