我在sheet1中有3张(sheet1,sheet2,sheet3)具有所有用户ID,sheet2具有登录用户ID,而sheet3为空。重点是......我需要将不登录用户ID放入sheet3,但我的代码失败了。如果这是一个愚蠢的问题因为我是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 ......应该是这样的。
答案 0 :(得分: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
答案 1 :(得分: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
祝你好运