需要我的vba代码只在特定列之间搜索一个值

问题描述:

我的问题是这样的,不确定是否可以实现:(需要与vba一起做) 我有一个范围5个组,每组3年(a每个组中的2012,2013和2014年的专栏)这些专栏有分数。我需要在所有小组中找到最高成绩,但仅限于2013年。我知道如何找到更高的成绩,但它在所有专栏中进行搜索。 我可以找到这个年级,但只在属于2013年的列中搜索吗?需要我的vba代码只在特定列之间搜索一个值

example of the data range

+1

您的数据是什么样的?一个公式不可接受?您尝试过什么,请发布您当前的代码/公式。 – BruceWayne

+0

添加了我的数据的示例图像,它必须在vba上,这太糟糕了,因为我认为我可以修复它的一个公式:(至于我现在的代码,我老实说无能为力,不知道怎么可以我将搜索范围限制为仅限于同一年的专栏 –

+0

您是否可以发布您的代码? – BruceWayne

此代码应帮助你。这是一个非常通用的代码来完成你所要求的。您可以通过制作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 
+1

非常感谢!!看起来这样会起作用!我想我会将其修改为颜色与结果的单元格,但shouldn现在没有太大问题! –