我的宏代码出了什么问题?

时间:2015-11-11 20:32:28

标签: excel vba excel-vba

我尝试在Excel宏中为我的工作编写比较。不知何故,它的工作方式与我想要的输出方式不同。我想要的是比较两列并显示它们之间的差异。如果一列上为空字段,程序应跳过一行。这是我的代码:

Sub run_compare_main()

    Application.Calculation = xlCalculationManual
    Application.ScreenUpdating = False

    Dim last_row As Integer
    Dim input_array As Variant
    Dim output_aray() As String
    Dim a_counter As Integer
    Dim b_counter As Integer

    last_row = get_last_row("INPUT", "A")

    ReDim output_array(1 To (last_row * 2), 1 To 5) '(last_row * 2)

    input_array = Range("A7:D7" & (last_row * 2)).Value2

    a_counter = 1
    b_counter = 1

    For i = 1 To (last_row * 2)

        If input_array(a_counter, 1) = input_array(b_counter, 3) Then

            output_array(i, 1) = input_array(a_counter, 1)
            output_array(i, 2) = input_array(a_counter, 2)
            output_array(i, 3) = input_array(b_counter, 3)
            output_array(i, 4) = input_array(b_counter, 4)

            a_counter = a_counter + 1
            b_counter = b_counter + 1


        ElseIf input_array(a_counter, 1) = input_array(a_counter - 1, 1) Then

            output_array(i, 1) = input_array(a_counter, 1)
            output_array(i, 2) = input_array(a_counter, 2)

            a_counter = a_counter + 1


        ElseIf input_array(b_counter, 3) = input_array(b_counter - 1, 3) Then

            output_array(i, 3) = input_array(b_counter, 3)
            output_array(i, 4) = input_array(b_counter, 4)

            b_counter = b_counter + 1

        End If
        'find smaller value
        If input_array(a_counter, 1) < input_array(b_counter, 3) Or input_array(b_counter, 1) = "" Then

                output_array(i, 1) = input_array(a_counter, 1)
                output_array(i, 2) = input_array(a_counter, 2)
                a_counter = a_counter + 1

            Else

                output_array(i, 3) = input_array(b_counter, 3)
                output_array(i, 4) = input_array(b_counter, 2)
                b_counter = b_counter + 1


        End If

        If a_counter = last_row - 5 Or b_counter = last_row - 5 Then

            Exit For

        End If

    Next

    Call newtab("OUTPUT")

    Range("A7").Resize(last_row, 4).Value = output_array

    Sheets("INPUT").Range("A5:D6").Copy
    Sheets("OUTPUT").Range("A5").Select
    ActiveSheet.Paste
    Columns("B:B").ColumnWidth = 80
    Columns("D:D").ColumnWidth = 80


    Dim LastCol As Long
    Dim LastRow As Long

  LastCol = ActiveSheet.UsedRange.Columns.Count
  LastRow = ActiveSheet.UsedRange.Rows.Count

  FilePath = "D:\Try\support.txt"
  Open FilePath For Output As #2

  CellData = ""
  For i = 1 To LastRow
      For j = 1 To LastCol
      CellData = "The Value at location (" & i & "," & j & ") " & Trim(ActiveCell(i, j).Value)
      Write #2, CellData
      Next j
  Next i

  Close #2
  MsgBox ("Job Done")

End Sub

Sub newtab(sheetname As String)

    Application.DisplayAlerts = False
    On Error Resume Next
    Sheets(sheetname).Delete
    Application.DisplayAlerts = True

    Sheets.Add After:=Sheets(Sheets.Count)
    Sheets(Sheets.Count).Activate
    Sheets(Sheets.Count).Name = sheetname

End Sub

Function get_last_row(ByVal sheetname As String, column As String) As Integer

    With Sheets(sheetname)
        If Application.WorksheetFunction.CountA(.Cells) <> 0 Then
            LastRow = .Cells.Find(What:="*", _
                  After:=.Range(column & "1"), _
                  Lookat:=xlPart, _
                  LookIn:=xlFormulas, _
                  SearchOrder:=xlByRows, _
                  SearchDirection:=xlPrevious, _
                  MatchCase:=False).Row
        End If
    End With

    get_last_row = LastRow

End Function

和我的worksample

worksample

0 个答案:

没有答案
相关问题