我正在尝试遍历A列“域名”,并获得每个域的总页数。对于每一行,如果域相同,则计算总数。进入新域后,将最终页数计入C列中域的右上角。
我是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
答案 0 :(得分:1)
<强> 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
<强>结果:强>