像计数一样计数,然后放在单元格中

我试图循环访问第A-列“域”,并获得每个域的总页数。 对于每一行,如果域是相同的,则统计总数。 一旦你到达一个新的领域,把最后一页的数量放在C栏的右上angular

在这里输入图像说明

我是VBA的新手 – 我正在尝试这样的事情。 任何指导将不胜感激。

Sub TestScript() iMaxRow = 11000 Range("B1").Select pagesCounter = 0 'loop counter for each page in site countEntryCell = 1 'where you put the total # pages for that site For iRow = 1 To iMaxRow 'loop through column B, while domain name is the same... count rows 'then put final count in count column If ActiveCell = ActiveCell.Offset(-1, 0) Then pagesCounter = pagesCounter + 1 Else 'Copy pages count to column c within the box End If ActiveCell.Offset(1, 0).Select 'select next row Next iRow End Sub 

UPD:

试试这个:

 Sub TestScript() Dim lastrow As Long Dim rng As Range, c As Range 'change Sheet1 to suit With ThisWorkbook.Worksheets("Sheet1") lastrow = .Cells(.Rows.Count, "A").End(xlUp).Row Set rng = .Range("A2:A" & lastrow) For Each c In rng If c.Value <> c.Offset(-1).Value Then 'c.offset(,2) gives you column C c.Offset(, 2).Value = WorksheetFunction.CountIf(rng, c.Value) 'aplly border With c.Offset(-1).Resize(, 3).Borders(xlEdgeBottom) .LineStyle = xlContinuous .ColorIndex = 0 .TintAndShade = 0 .Weight = xlMedium End With End If Next c End With End Sub 

结果:

在这里输入图像说明