[VBA ] 關於for . next 邏輯問題
最近在寫一個比對資料的小程式
由於有點時間沒寫程式
有點退步....
Excel Raw Data:
A B C
1
2 22 1 22
3 33 2 22
4 44 3 33
5 55 4 44
6 66 5 55
7 55 6 23
8 55 3
9 77 7
目的:
將A欄位中每一筆資料對C欄位對比
只要A欄位->Cells(x, 1) 都沒有與任何一個C欄位值相同
就將A欄位與B欄位(ex. 22 1 )一起貼在其他活頁簿
我的code:
Sub Com()
Dim t As Integer
Dim x As Integer
Dim y As Integer
y = 1
Application.ScreenUpdating = False
Sheets("test").Select
For x = 2 To 9 'A欄位loop
For t = 2 To 7 'C欄位loop
If Cells(x, 1).Value = Cells(t, 3).Value Then '如果值相同就離開for loop
Exit For
ElseIf t > 6 Then '如果比對超過六個(因為C欄位僅有六比資料)
y = y + 1
Range(Cells(x, 1), Cells(x, 2)).Select '選擇A ,B欄位
Selection.Copy
Sheets("NewWords").Select '點選另外一個活頁簿
Range(Cells(y, 1), Cells(y, 2)).PasteSpecial xlPasteValues '貼上值
End If
Next t
Next x
Application.ScreenUpdating = True
End Sub
執行結果:
A B
1 66 5
2
此時僅能印出第一筆比對到完全沒有相同的資料
往下就印不出來了....
感覺for loop被中斷
請問一下我的程式碼是哪個地方的邏輯不合裡呢
謝謝
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 220.132.93.102
※ 編輯: Wilsondenq 來自: 220.132.93.102 (11/17 02:02)
※ 編輯: Wilsondenq 來自: 220.132.93.102 (11/17 02:03)
→
11/17 02:55, , 1F
11/17 02:55, 1F
→
11/17 23:58, , 2F
11/17 23:58, 2F