2015-06-15 119 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 

好运