Excel 2007宏来计算和总计未定义的行数和列数

时间:2015-02-01 09:51:16

标签: excel excel-vba vba

我将此声明作为原始数据(未格式化)从我们的核心系统生成到excel表。遗憾的是,在生成过程中几乎不可能对其进行格式化。因此,我尝试使用录制的宏表格式化它来计算行数并对列中的值求和。请记住,行数未定义,这意味着宏应该适用于任何给定数量的行。换句话说,我试图创建一个宏表来将表(1)格式化为表(2) 请检查图像和Excel表格。

注意: Visa卡号以数字开头(4)万事达卡以(5)开头。

UnFormatted Sheet(1)+ Sheet(2)

enter image description here

1 个答案:

答案 0 :(得分:0)

使用此代码启动操作,您必须编辑代码以添加格式和其他文本,但您应该能够执行此操作。

我认为以4开头的数字是Visa,而5是MC

Sub Do_ItDo_It_Good()
    Dim ws As Worksheet, sh As Worksheet
    Dim Rws As Long, Rng As Range, c As Range
    Dim Vrw As Long, MCrw As Long
    Dim vsum1 As Range, vsum2 As Range, vsum3 As Range
    Dim Mcsum1 As Range, Mcsum2 As Range, MCsum3 As Range

    Set ws = Sheets("Sheet1")
    Set sh = Sheets("Sheet2")
    sh.Cells.Delete
    With ws
        Rws = .Cells(Rows.Count, "D").End(xlUp).Row
        Set Rng = .Range(.Cells(1, "D"), .Cells(Rws, "D"))
    End With

    With sh
        .Range("A1") = "Something 1"
        .Range("A2") = "Something 2"
        .Range("A3") = "Something 3"
        .Range("A4") = "Something 4"
        .Range("A5") = "Something 5"
        .Range("A6") = "Something 6"
        .Range("A9") = "Visa"

        For Each c In Rng.Cells
            If c Like "4*" Then c.Offset(0, -3).Range("A1:I1").Copy .Cells(Rows.Count, "A").End(xlUp).Offset(1, 0)
        Next c
        Vrw = .Cells(Rows.Count, "A").End(xlUp).Row
        Set vsum1 = .Cells(Vrw + 3, "E")
        Set vsum2 = .Cells(Vrw + 3, "G")
        Set vsum3 = .Cells(Vrw + 3, "H")
        vsum1 = Application.Sum(.Range(.Cells(11, "E"), .Cells(Vrw, "E")))
        vsum2 = Application.Sum(.Range(.Cells(11, "G"), .Cells(Vrw, "G")))
        vsum3 = Application.Sum(.Range(.Cells(11, "H"), .Cells(Vrw, "H")))
        .Cells(Vrw + 7, 1) = "Master Card"

        For Each c In Rng.Cells
            If c Like "5*" Then c.Offset(0, -3).Range("A1:I1").Copy sh.Cells(Rows.Count, "A").End(xlUp).Offset(1, 0)
        Next c
        MCrw = .Cells(Rows.Count, "A").End(xlUp).Row
        Set Mcsum1 = .Cells(MCrw + 3, "E")
        Set Mcsum2 = .Cells(MCrw + 3, "G")
        Set MCsum3 = .Cells(MCrw + 3, "H")
        Mcsum1 = Application.Sum(.Range(.Cells(Vrw + 8, "E"), .Cells(MCrw, "E")))
        Mcsum2 = Application.Sum(.Range(.Cells(Vrw + 8, "G"), .Cells(MCrw, "G")))
        MCsum3 = Application.Sum(.Range(.Cells(Vrw + 8, "H"), .Cells(MCrw, "H")))



    End With

End Sub