Excel:将列中的行计数到最后一个活动单元格

时间:2018-01-30 10:02:10

标签: excel vba excel-vba

我有一个完美无缺的代码,但每当我遇到一个空单元格时,计数就会停在那里并覆盖下面的所有内容。

因此,例如,有字符串row1,row2,row3(空单元格),row4(有字符串)。宏将在第2行停止,然后在第4行及以后键入结果,并将覆盖所有内容。我在线看过一些代码,但不适合我的代码。谢谢你的帮助。

Public Sub Test()

Dim row_number As Long
Dim count_of_Dog As Long
Dim count_of_Cat As Long
Dim count_of_others As Long
Dim count_of_all As Long
Dim items As Variant
Dim ws As Worksheet: Set ws = ActiveSheet
'declare and set your worksheet, amend as required
LastRow = ws.Cells(ws.Rows.Count, "N").End(xlUp).Row

count_of_Dog = 0
count_of_Cat = 0
count_of_others = 0
count_of_all = 0

For i = 1 To LastRow
    items = ws.Range("N" & i)
    If InStr(items, "dog") > 0 Then
        count_of_Dog = count_of_Dog + 1
    ElseIf InStr(items, "cat") > 0 Then
        count_of_Cat = count_of_Cat + 1
    ElseIf items <> "" Then
        count_of_others = count_of_others + 1

    End If
Next i

count_of_all = count_of_Dog + count_of_Cat + count_of_others

Range("N" & LastRow).Offset(3, 0).Value = "Count"
Range("N" & LastRow).Offset(4, 0).Value = count_of_Dog
Range("N" & LastRow).Offset(5, 0).Value = count_of_Cat
Range("N" & LastRow).Offset(6, 0).Value = count_of_others
Range("N" & LastRow).Offset(7, 0).Value = count_of_all
Range("N" & LastRow).Offset(3, 1).Value = "Keywords"
Range("N" & LastRow).Offset(4, 1).Value = "DOGS"
Range("N" & LastRow).Offset(5, 1).Value = "CATS"
Range("N" & LastRow).Offset(6, 1).Value = "Others"
Range("N" & LastRow).Offset(7, 1).Value = "Total"
End Sub

3 个答案:

答案 0 :(得分:0)

以下内容如何,​​我已经用For循环替换了Do循环,并且在N列中找到了包含数据的LastRow,然后使用与写入总值相同的Offset,我还删除了任何Select或者激活方法,因为这些会降低代码的速度:

<head>
    <meta charset="utf-8" />
    <title>Bazaar Ceramics</title>

<style>

body {
    margin: 0px;
    padding: 0px;
}

.mainwrapper {
    width: 1024px;
    margin: 0 auto;
}

.pagebanner {
    height: 250px;
    margin: 0;
}

.pagebanner img {
    width: 100%;
    height: 100%;
}

.main {
    margin: 0;
}

.navbar {
    background-color: brown;
    min-height: 500px;
    width: 20%;
    float: left;
}

.navbar li {
    list-style: none;
    margin-bottom: 15px;
}

.navbar a {
    color: #fff;
    font-size: 20px;
}

.headersd {
    width: 80%;
    float: left;
    padding: 0;
    min-height: 500px;
}

.header {
    background-color: #000;
    text-align: center;
}

.header h1 {
    color: #fff;
    margin: 0;
}

.subheading {
    background-color: #f8d631;
    text-align: center;
}

.subheading h2 {
    background-color: #f8d631;
    margin: 0;
}

.table {
    align: center;
    padding-left:50px
    border: 20px;
    }
.footer {
    background-color: darkgreen;
    height:20px;
    margin-bottom:0px;
}   
</style>

</head>

<body>

    <div class="mainwrapper">
        <div class="pagebanner">
          <img src="https://cdn-images-1.medium.com/max/1400/1*278tqw9zNPe2WCAz29Wzdw.jpeg" alt="PageBanner">
        </div><!--PageBanner-->

    <div class="main">
        <div class="navbar">
            <ul>
                <li>
                    <a href="#">Information</a>
                </li>
                <li>
                    <a href="#">Home</a>
                </li>
            </ul>
        </div><!--Navbar-->

    <div class="headers">
    <div class="header">
            <h1>The Club Site</h1>
    </div><!--Main page heading section-->

    <div class="subheading">
            <h2>Members Prices</h2>
    </div><!--Sub heading section-->
    </div>

    </div>
    <div class="content">
        <div class="table"> 
            <table style="margin:0 auto;" >
            <h3 style="text-align:center;">Discount Prices</h3>
            <tr><td><img src="HTML Pract A/images/smaller/bcpot002_smaller.jpg"</td><td><img src="HTML Pract A/images/smaller/bcpot006_smaller.jpg"</td><td><img src="HTML Pract A/images/smaller/bcpot013_smaller.jpg"</td></tr>
            <tr><td><img src="HTML Pract A/images/smaller/bcpot002_smaller.jpg"</td><td><img src="HTML Pract A/images/smaller/bcpot006_smaller.jpg"</td><td><img src="HTML Pract A/images/smaller/bcpot013_smaller.jpg"</td></tr>
            </table>
        </div><!--Table Section-->  

    </div><!--Content section-->

    <div class="footer">
        <footer>wsrgferg</footer>
    </div><!--Footer section-->
    </div><!--Endwrapper-->

</body>



</html>

答案 1 :(得分:0)

要查找最后一行,您必须滚动到工作表底部然后向上跳。要以编程方式执行此操作:

With ActiveSheet
    LastRow = .Range("A" & .Rows.Count).End(xlUp).Offset(1).Row
End With

但我认为这不是最佳方法。最好先定义你的范围,然后寻找那些狗和猫。用对象循环替换你的Do ... Until循环:

Dim rngToSearch As Range
Dim cl As Range

    With ActiveSheet
        rngToSearch = Range(.Range("N1"), .Range("N" & .Rows.Count).End(xlUp))


        For Each cl In rngToSearch
            If InStr(cl.Value, "dog") Then
                count_of_dog = count_of_dog + 1
            ElseIf InStr(cl.Value, "cat") Then
                count_of_cat = count_of_cat + 1
            ElseIf cl.Value <> "" Then count_of_others 1
        End If

    End With

我还重写了您的代码以进行汇总。如果你愿意的话,把它放在End If和End With之间:

    With .Range("N" & .Rows.Count).End(xlUp)
        Range(.Offset(5, 0), .Offset(8, 0)).Value = Array( _
            "Count", "Keywords", "DOGS", "CATS", "Others", "Total")
        Range(.Offset(4, 1), .Offset(8, 1)).Values = Array( _
            count_of_Dog, count_of_Cat, count_of_others, count_of_all)
    End With

这应该会使您的摘要只比您的数据低三行。

答案 2 :(得分:0)

如果列已固定,则使用正确的方法,即

LastRow = ws.Cells(ws.Rows.Count, "N").End(xlUp).Row

然而,我很惊讶地知道它并没有获取真正的最后一个空白行。我可以想象这种情况发生的唯一情况是你在电子表格的最后几行有一些数据。

以下代码返回什么值?

MsgBox ws.Range("N1").EntireColumn.Find("*", ws.Range("N1"), , , xlByRows, xlPrevious).Row