繁体   English   中英

如何在Excel中使用VBA插入数据

[英]How to insert data by using VBA in excel

我在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 ......应该是这样的。

试试看 通过首先删除“未登录”的内容,然后为尚未登录的每一行填充一个用户,对它进行了轻微的修改。已添加了一个计数器,以在用户未登录时增加下一个要填充的单元格。 添加了一个布尔变量来跟踪该用户是否登录。

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

我想我会按照您的操作,是否像vlookup的相反,如果它在列表a中而不是列表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

祝好运

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM