2015-06-15 73 views
0

我在sheet1中有3張(sheet1,sheet2,sheet3)擁有所有用戶ID,sheet2具有登錄用戶ID,而sheet3是空的。重點是...我需要不將用戶ID登錄到sheet3,但我的代碼失敗。如果這是一個愚蠢的問題,因爲我是新手用VBA如何在excel中使用VBA插入數據

這裏我的代碼:

Sub NotLog() 

Dim c1 As Range 

Dim c2 As Range 

Dim c3 As Range 

Dim sh1 As Worksheet 

Dim sh2 As Worksheet 

Dim sh3 As Worksheet 

    Set sh1 = ThisWorkbook.Sheets("ALl USer") 
    Set sh2 = ThisWorkbook.Sheets("8_Logon_SOE_by_group") 
    Set sh3 = ThisWorkbook.Sheets("Not Logon") 

    For Each c1 In sh1.Range("A2:A99") 
     For Each c2 In sh2.Range("A3:A99") 
      If c1 <> c2 Then 
       For Each c3 In sh3.Range("A2:A99") 
        If IsEmpty(Range("c3").Value) = True Then 
         c3 = c1 
        ElseIf IsEmpty(Range("c3").Value) = False Then 
         Exit For 
        End If 
       Next c3 
      Else 
       Exit For 
      End If 
     Next c2 
    Next c1 

End Sub 

http://i.stack.imgur.com/2kDEH.png ......這是我的輸出。 http://i.stack.imgur.com/IWSZM.png ......應該是這樣的。

回答

1

試試看。首先刪除Not Logon的內容,然後每行填寫一個未登錄的用戶,稍作修改。如果用戶尚未登錄,則會添加一個計數器以遞增下一個單元格以填充。已經添加了一個布爾變量來跟蹤用戶是否已經登錄。

Sub NotLog() 

    Dim c1 As Range 
    Dim c2 As Range 
    Dim sh1 As Worksheet 
    Dim sh2 As Worksheet 
    Dim sh3 As Worksheet 

    Set sh1 = ThisWorkbook.Sheets("ALl USer") 
    Set sh2 = ThisWorkbook.Sheets("8_Logon_SOE_by_group") 
    Set sh3 = ThisWorkbook.Sheets("Not Logon") 

    ' empty our not logon sheet 
    sh3.Cells.Clear 

    ' used to print into sheet 2 line by line a list of 
    ' users that have not logged in 
    Dim CellCounter As Integer 
    Dim TempFound As Boolean 
    CellCounter = 1 

    For Each c1 In sh1.Range("A2:A99") 
     TempFound = False 

     ' match user with login 
     For Each c2 In sh2.Range("A3:A99") 
      If c1.Value = c2.Value Then 
       TempFound = True 
       Exit For 
      End If 
     Next c2 

     ' if user has not logged in, list the user 
     ' in Not Logon sheet 
     If Not TempFound Then 
      sh3.Cells(CellCounter, 1).Value = c1.Value 
      CellCounter = CellCounter + 1 
     End If 

    Next c1 

End Sub 
0

我想我跟着你在做什麼,是它像一個VLOOKUP相反,如果它在列表中的但不是B名單,然後把它放在C組名單在哪裏?

如果是這樣的問題,那麼問題是,你需要

For Each c2 In sh2.Range("A3:A99") 
    If c1 <> c2 Then 

完成所有行你決定,如果它是在比賽之前,然後將它寫出來的第三片。

所以,如果我理解你正確像這樣的工作:

Sub NotLog() 
    Dim c1 As Range 
    Dim c2 As Range 
    Dim c3 As Range 
    Dim sh1 As Worksheet 
    Dim sh2 As Worksheet 
    Dim isMatch As Boolean 

    Set sh1 = ThisWorkbook.Sheets("ALl USer") 
    Set sh2 = ThisWorkbook.Sheets("8_Logon_SOE_by_group") 
    Set sh3 = ThisWorkbook.Sheets("Not Logon") 

    For Each c1 In sh1.Range("A2:A99") 
     isMatch = False 
     For Each c2 In sh2.Range("A2:A99") 
     If c1 = c2 Then 
      isMatch = True 
      Exit For 
     End If 
     Next c2 
     If Not isMatch Then ' check once you have checked all on second sheet 
     'This is quicker than looping to find the bottom blank row 
     'It basically says go to bottom row, then ctrl+Up then down one 
     sh3.Range("a" & sh3.Rows.Count).End(xlUp).Offset(1, 0) = c1 
     End If 
    Next c1 
End Sub 

好運