需要我的vba代码才能在某些列

时间:2017-07-19 19:32:03

标签: excel vba excel-vba

我的问题是这个,不确定是否可以实现:(需要用vba做) 我有一个范围包含5组,每组3年(每组中有2012年,2013年和2014年的列),这些列有等级。我需要在所有小组中找到最高分,但仅限于2013年。我知道如何找到更高的等级,但它在所有专栏中搜索。 我可以找到这个等级,但只能在属于2013年的列中进行搜索吗?

example of the data range

1 个答案:

答案 0 :(得分:0)

此代码可以帮助您。这是一个非常通用的代码,可以满足您的要求。您可以通过创建UserForm并向用户询问您希望识别的列标题而不是仅查找2013来扩展此功能。

Sub 
   Dim lastColumn As Long
   Dim max_value As Double

   max_value = 0

   lastColumn = Worksheets("Sheet1").Cells(1, Columns.Count).End(xlToLeft).Column
   For i = 1 To lastColumn
      ' Seek out the column you desire!
      If Worksheets("Sheet1").Cells(2,i).Value = "2013" Then

         Dim lastRow As Long
         lastRow = Worksheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row

         ' Go through the entire column of the desired type to read the values
         For j = 3 To lastRow
            If Worksheets("Sheet1").Cells(j, i).Value > max_val Then
               max_value = Worksheets("Sheet1").Cells(j, i).Value
            End If
         Next j
      End If
   Next
End Sub