首页 > 解决方案 > 比较列然后在最后一行之后添加文本

问题描述

所以我试图将一个工作表上的 g1:g51 与 m11:m15 进行比较,如果找到匹配项,则跳过“M”中的下一个空白并插入“G”中的下一个文本,它在 wsDest.Range("M" ) = 不匹配2。我究竟做错了什么?

    Dim wsCopy As Worksheet
    Dim wsCopy3 As Worksheet
    Dim i2 As Integer
    Dim inrow2 As Integer
    Dim inmatch2 As String
    Dim k2 As Integer
    Dim outrow2 As Integer
    Dim outmatch2 As String


'Set variables for copy and destination sheets
    Set wsCopy = Worksheets("Sheet1")   
    Set wsCopy3 = Worksheets("Sheet3")

    If wsCopy3.Range("b1") > 0 Then     

    inrow2 = 10     
        For i2 = 1 To inrow2
            inmatch2 = wsCopy.Range("a" & i2)

        If inmatch2 = "" Then Exit For

    outrow2 = 25
        For k2 = 1 To outrow2
            outmatch2 = wsCopy3.Range("b" & k2)

            If outmatch2 = inmatch2 Then
                Exit For
            End If

           If outmatch2 = "" Then
                wsCopy3.Range("b" & k2) = inmatch2
                Exit For
            End If
        Next

            If outmatch2 = "" Then
                Exit For
            End If
        Next
    End If
End Sub

标签: excelvba

解决方案


Sub ADDING() Dim wsCopy As Worksheet Dim wsCopy3 As Worksheet Dim i2 As Integer Dim inrow2 As Integer Dim inmatch2 As String Dim k2 As Integer Dim outrow2 As Integer Dim outmatch2 As String

'Set variables for copy and destination sheets
Set wsCopy = Worksheets("Sheet1")

Set wsCopy3 = Worksheets("Sheet3")


If wsCopy3.Range("b1") > 0 Then


    inrow2 = 10


    For i2 = 1 To inrow2
        inmatch2 = wsCopy.Range("a" & i2)
        If inmatch2 = "" Then Exit For

        outrow2 = 25
        For k2 = 1 To outrow2
            outmatch2 = wsCopy3.Range("b" & k2)
            If outmatch2 = inmatch2 Then
            Exit For
            End If

            If outmatch2 = "" Then
            wsCopy3.Range("b" & k2) = inmatch2
                Exit For
            End If
        Next

        If outmatch2 = "" Then
                Exit For
        End If
    Next

End If

结束子


推荐阅读