如何比较工作表中的数据并在另一个工作表中进行操作?

时间:2019-06-06 14:01:49

标签: excel vba

我正在处理一个宏,该宏将数据分类为不同的组(为简化起见,每一种情况(除了一个例外)由两行组成),当更改组的第一行时,我希望宏以识别我最初处理的案件,并将数据调整为“新”案件的格式,但是,有关更改的信息可以在一个工作表(LogDetails)中找到,而我需要确定的信息是原始案例在另一个工作表中(20190328)

问题在于IF条件下的比较语句中,因为我想知道两个不同工作表中两个不同单元格的内容以便确定操作

  • 我已经有一些代码来跟踪更改并将其保存在工作表中

  • 我编写的用于使数据适应新的案例格式的代码应该可以解决这个问题(目前还无法实际测试)


Option Explicit

Sub apply_case_change()
    Dim lRow As Integer
    Dim k1 As Integer
    Dim c1 As Integer
    Dim row_counter As Integer

    lRow = Worksheets("LogDetails").Cells(Rows.Count, 1).End(xlUp).Row 'Find the last non-blank cell in column A(1)

    For k1 = 2 To lRow
    row_counter = Worksheets("LogDetails").Cells(k1, 8).Value 'Counter for visible sheet
        If Worksheets("LogDetails").Cells(k1, 9).Value = 28 Then
            '----Case 1
            If Worksheets("LogDetails").Cells(k1, 2).Value = "FB" And Worksheets("20190328").Cells(row_counter, 37).Value = "Kaufteil mit Beistellung" Then
                Worksheets("LogDetails").Cells(k1, 10).Value = 1
                Worksheets("LogDetails").Cells(k1, 11).Value = 2
            ElseIf Worksheets("LogDetails").Cells(k1, 2).Value = "FB" And IsEmpty(Worksheets("20190328").Cells(row_counter, 37).Value) = False Then
                Worksheets("LogDetails").Cells(k1, 10).Value = 1
                Worksheets("LogDetails").Cells(k1, 11).Value = 3

            ElseIf Worksheets("LogDetails").Cells(k1, 2).Value = "FB" And IsEmpty(Worksheets("20190328").Cells(row_counter + 1, 28).Value) = True Then
                Worksheets("LogDetails").Cells(k1, 10).Value = 1
                Worksheets("LogDetails").Cells(k1, 11).Value = 4
            End If
        End If
    Next k1


'_____________________________________ CASE 4


    For k1 = 3 To lRow
        c1 = Worksheets("LogDetails").Cells(k1, 8).Value 'ROW Counter for visible sheet
        If Worksheets("LogDetails").Cells(k1, 10).Value = 1 And Worksheets("LogDetails").Cells(k1, 11).Value = 4 Then
            'CHANGE FROM 1 to 4
            Worksheets("20190328").Cells(c1 + 1, 39).Value = Worksheets("20190328").Cells(c1, 28).Value 'Copy xxx number
            Worksheets("20190328").Cells(c1 + 1, 28).Value = "HZ" 'Update Typ Column
            Worksheets("20190328").Cells(c1 + 1, 53).Value = 4 'Update tag
            c1 = c1 + 1 'So we skip the whole set (EACH CASE 1 CONTAINS 2 LINES, one after the other)

        ElseIf Worksheets("LogDetails").Cells(k1, 10).Value = 2 And Worksheets("LogDetails").Cells(k1, 11).Value = 4 Then
            'CHANGE FROM 2 to 4
            Worksheets("20190328").Cells(c1 + 1, 39).Value = Worksheets("20190328").Cells(c1, 28).Value 'Copy xxx number
            Worksheets("20190328").Cells(c1, 37).ClearContents 'Clears Koop Column
            Worksheets("20190328").Cells(c1 + 1, 28).Value = "HZ" 'Update Typ Column
            Worksheets("20190328").Cells(c1 + 1, 53).Value = 4 'Update tag
            c1 = c1 + 1 'So we skip the whole set (EACH CASE 1 CONTAINS 2 LINES, one after the other)

        ElseIf Worksheets("LogDetails").Cells(k1, 10).Value = 3 And Worksheets("LogDetails").Cells(k1, 11).Value = 4 Then
            'CHANGE FROM 3 to 4
            Worksheets("20190328").Cells(c1 + 1, 39).Value = Worksheets("20190328").Cells(c1, 28).Value 'Copy xxx number
            Worksheets("20190328").Cells(c1 + 1, 28).Value = "HZ" 'Update Typ Column
            Worksheets("20190328").Cells(c1, 37).ClearContents 'Clears Koop Column
            Worksheets("20190328").Cells(c1 + 1, 53).Value = 4 'Update tag
            c1 = c1 + 1 'So we skip the whole set (EACH CASE 1 CONTAINS 2 LINES, one after the other)
        End If
    Next k1
End sub

我希望以下几种比较有效:

If Worksheets("LogDetails").Cells(k1, 3).Value = "FB" And Worksheets("20190328").Cells(row_counter + 1, 28).Value = "HZ Beistellung" Then

到目前为止没有出现错误

0 个答案:

没有答案