是否可以给出特定行使用的最大列数

时间:2012-12-31 14:00:10

标签: vba excel-vba vbscript excel

我有一个Excel矩阵如下:

PID#      T1    T2   T3    T4    T5   T6    T7

11        1                1      
14        1     1          1     
21                   1     1     
41        1          1     1     1
71                   1     
88        1          1           1

PID#只是流程,所有流程都由多个任务组成。但并非所有进程都必须使用所有T1 - T5任务。在这种情况下,可以获得使用最大任务的PID#1用于表示已使用或未使用任务。 PID#41和88使用的最大任务是5。我只需要使用的最大列数以及使用该列数的任何行#。

注意

这里我使用1来告诉数据,但实际上有不同类型的数据。我需要找出哪一行使用了最大列。但是如果一行的任何单元格是blank并且它在左边,则应该在计数中。比方说 -

<> 1< 1> 1给出计数为4

<> <> 1< 1>将计数为3

1 1&lt; 1&gt;我会将<>用来代表no values

,将计数设为2'

修改

Option Explicit

Dim ArrayListTaskDetails : Set ArrayListTaskDetails = CreateObject("System.Collections.ArrayList")
Dim i,colcount

i=2
Do while i < = objExcel1.Application.WorksheetFunction.CountA(ob.Rows(1))

colcount=objExcel1.Application.WorksheetFunction.CountA(ob.Rows(i))
ArrayListTaskDetails.Add(colcount)

i=i+1
Loop

ArrayListTaskDetails.Sort()
i=ArrayListTaskDetails.Count
MsgBox("HighestColumnNumner:" & ArrayListTaskDetails(i-1))

问题:

我无法计算没有连续值的行的空白列。因此,我无法正确计算数量。

EDIT1

这里的问题仍然是我无法计算左边的空白单元格,因为那些也被认为是用过的列,其他行可以有值。那么需要找出最右边的列哪个已被行使用,之后任何行都没有使用任何列。希望我能够清楚我要找的东西:

Option Explicit

Dim objExcel1
Dim strPathExcel1
Dim objSheet1,objWB,ColCount
Dim ArrayListTaskDetails : Set ArrayListTaskDetails = CreateObject("System.Collections.ArrayList")

Set objExcel1 = CreateObject("Excel.Application")
strPathExcel1 = "D:\AravoVB\.xlsx"
Set objWB = objExcel1.Workbooks.open(strPathExcel1)

Set objSheet1 = objExcel1.ActiveWorkbook.Worksheets(1)

Do Untill count > objExcel1.Application.WorksheetFunction.CountA(objSheet1.Rows(1)) 

Range = objSheet1.("count:count")
ColCount=objExcel1.Application.WorksheetFunction.CountIf(Range,<> "")
ArrayListTaskDetails.Add(ColCount)

Loop

ArrayListTaskDetails.Sort()
MsgBox(ArrayListTaskDetails(ArrayListTaskDetails.Count - 1))

谢谢,

2 个答案:

答案 0 :(得分:1)

Excel在计算Matrix方面非常强大。我会使用Excel公式而不是代码来计算它。我会在右边添加一列,这将添加进程使用的任务总数,如下面的矩阵所示。

    A   B   C   D   E   F   G
1   PID T1  T2  T3  T4  T5  Total
2   #11 1                   1
3   #14     1   1   1       3
4   #21 1   1   1   1   1   5
5   #41     1   1           2

然后我将编写两个Array Formulas来计算进程使用的最大任务数以及该进程的名称。

计算示例中使用的最大任务的公式:= SUM(IF($ G $ 2:$ G $ 5 = MAX($ G $ 2:$ G $ 5),G2:G5,0))

查找使用最大任务的价格的公式: = OFFSET(A1,SUM(IF($ G $ 2:$ G $ 5 = MAX($ G $ 2:$ G $ 5),ROW(G2:G5)-1,0)),0,1,1)

请注意,我曾提到我使用了数组公式。要在Excel中添加数组公式,您需要输入公式然后按&#34; Ctrl + Shift + Enter&#34;使该公式成为一个数组公式。

希望这会有所帮助。 Vikas B

  

----------------- EDIT ----------------------------- ------------------------

在此处添加代码。我只是使用了样本,如矩阵所示,并产生了正确的结果。

    Sub FindMax()

'assuming column 1 is the task ID and Row one has the headings.

Const LastColumn As Integer = 7 ' you can use xl end to get the last used column in the range
Const LastRow As Integer = 5


Dim rowCounter As Integer
Dim prevValue As Integer
Dim rngToTotal As Range
Dim sht As Worksheet
Dim maxRowName As String
Dim value As Integer
Dim maxValue As Integer

Set sht = ActiveSheet 

For rowCounter = 2 To LastRow
    Set rngToTotal = sht.Range(sht.Cells(rowCounter, 2), sht.Cells(rowCounter, LastColumn))
    value = WorksheetFunction.Sum(rngToTotal)

    If value > prevValue Then
        maxRowName = sht.Cells(rowCounter, 1).value
        maxValue = value
    End If
    prevValue = value
Next rowCounter

MsgBox "Process name " & maxRowName & "  =  " & maxValue

End Sub

答案 1 :(得分:1)

仍然不相信为什么Vikas回答不适合你。请试试这个代码。它突出显示最后的最大值。唯一的缺陷是它不会跟踪具有相同最大值的所有PID。如果你也需要,我可以改进代码。

代码:

Option Explicit

Sub getRealUsedColumns()
Dim rngInput As Range
Dim arrInput As Variant, arrRowTotal As Variant
Dim i As Integer, j As Integer, counter As Integer, iTemp  As Integer
Dim iPID As Integer, maxRowNum As Integer

    arrInput = Application.WorksheetFunction.Transpose(Sheets(3).Range("B3:I8").Value2)
    ReDim arrRowTotal(LBound(arrInput, 2) To UBound(arrInput, 2))

    For i = LBound(arrInput, 2) To UBound(arrInput, 2)
        counter = 0
        For j = LBound(arrInput) + 1 To UBound(arrInput)
            If arrInput(j, i) <> "" Or Not IsEmpty(arrInput(j, i)) Then
                counter = counter + 1
            End If
        Next j

        '-- most recent max value (if you have two of the same, this doens't catch)
        '-- you need to save in a proper array to catch multiple PIDs with same max value
        If iTemp <= counter Then
            iTemp = counter
            iPID = arrInput(1, i)
            maxRowNum = i
        End If

        arrRowTotal(i) = counter
    Next i

    '-- Row total into the sheet output
    Sheets(3).Range("J3").Resize(UBound(arrRowTotal)) = _
                     Application.WorksheetFunction.Transpose(arrRowTotal)

    '-- highlight the max total row.
    With Sheets(3).Range("B3").Offset(maxRowNum - 1, 0).Resize(1, UBound(arrInput, 1) + 1)
        .Interior.Color = 200
    End With

End Sub

结果:

enter image description here

相关问题