计算特定细胞

时间:2018-01-19 10:28:23

标签: vba excel-vba excel

我知道这将是一个简单的调整,但是我已经尝试了很多变化,并且在谷歌和这个论坛上运气不佳。

我到目前为止的代码给了我一列B的行数,返回值为225.但是我希望计数从单元格“B17”开始返回值209.这是因为B17是我的电子表格中的表格的开头,它将包含可变数量的数据。

目前的代码是:

With ActiveSheet
    LastRow = .Cells(.Rows.Count, "B").END(xlUp).row
End With

提前致谢

[Results when using the code

Ultimate Result

Sub Macro1()

Dim startRow As Double
Dim lastRow As Double
Dim procCol As Double
Dim i As Double

'I will preface this with you should not use activesheet
'you should declare:
'Dim thisWS As Worksheet
'Set thisWS = Thisworkbook.Worksheets("yourworksheetname")
'and use thisWS where you see Activesheet

startRow = ActiveSheet.Range("K16").row
procCol = ActiveSheet.Range("K16").Column

lastRow = ActiveSheet.Cells(Rows.Count, procCol).END(xlUp).row

'for RGB use a tool like
'https://www.w3schools.com/colors/colors_picker.asp


  For i = startRow To lastRow 'this will run through the rows, it's dynamic
   If ActiveSheet.Cells(i, procCol).Value = ActiveSheet.Cells(i + 1, procCol) Then

            Range(ActiveSheet.Cells(i, 2), ActiveSheet.Cells(i, 20)).Interior.Color = RGB(220, 230, 241) 'this is light blue
            Range(ActiveSheet.Cells(i + 1, 2), ActiveSheet.Cells(i + 1, 20)).Interior.Color = RGB(255, 255, 255) 'this is light blue

   Else 'Do nothing

   End If
Next i

End Sub

我唯一改变的是从B到K的参考单元格,扩展了范围,使其填充行而不是单元格并更新颜色?

4 个答案:

答案 0 :(得分:2)

如果你需要计算17(包括)和当前单元格之间的行,这很简单:

LastRow = Range(ActiveSheet.Range("A17"), Range("A" & ActiveSheet.Rows.Count).End(xlUp)).Rows.Count

答案 1 :(得分:1)

你试过这样的事吗:

With ActiveSheet
    LastRow = .Cells(.Rows.Count, "B").End(xlUp).Row - .Range("B17").Row
End With

这实际上取决于你想要的东西。但在上面的情况下,您只需从结果中删除17即可。考虑将+1添加到LastRow以获得您真正想要的内容。

喜欢这个LastRow = LastRow +1

答案 2 :(得分:1)

“谢谢这实际上似乎有效。最终我正在尝试找到我表格中的最后一行(因为它有所不同)。下一步要解决的问题是尝试格式化数据以便我可以每当某个值与下面的行匹配时,就会使用不同的填充颜色。通过查看按颜色分隔的行,可以看出数据更具可读性。问题是有时2行可能匹配,有时10行等等。 - Carlos80 8小时前“

此代码将执行您想要的操作,如果您希望每个选择使用不同的颜色,则必须稍微修改代码。它会动态调整您硬编码的列,也可以轻松更改列的列或起始行。检查一下,它很简单,但会根据B17到你想要的数据到列的末尾。

**如果新的匹配在彼此之上,则更新为交错颜色****

Option Explicit

Sub Macro1()


Dim startRow As Double
Dim lastRow As Double
Dim procCol As Double
Dim i As Double

'I will preface this with you should not use activesheet
'you should declare:
'Dim thisWS As Worksheet
'Set thisWS = Thisworkbook.Worksheets("yourworksheetname")
'and use thisWS where you see Activesheet

startRow = ActiveSheet.Range("B17").Row
procCol = ActiveSheet.Range("B17").Column

lastRow = ActiveSheet.Cells(Rows.Count, procCol).End(xlUp).Row

'for RGB use a tool like
'https://www.w3schools.com/colors/colors_picker.asp


  For i = startRow To lastRow 'this will run through the rows, it's dynamic
   If ActiveSheet.Cells(i, procCol).Value = ActiveSheet.Cells(i + 1, procCol)  Then

            ActiveSheet.Cells(i, procCol).Interior.Color = RGB(0, 204, 255) 'this is light blue
            ActiveSheet.Cells(i + 1, procCol).Interior.Color = RGB(0, 204, 255) 'this is light blue

   Else 'Do nothing

   End If
Next i

End Sub

干杯,Wookie

答案 3 :(得分:0)

如果您的表格是Excel数据表(插入>表格),您可以尝试计算这样的行:

Set tbl = Sheets("Your_Sheet").ListObjects("Your_table")
rows_no = tbl.DataBodyRange.Rows.Count
Debug.Print rows_no