Frage

I thought I fixed a problem, but now I am facing another problem... VBA is so hard.

   For i = 2 To LastRow
    For j = 2 To LastCol
        '
        '*There is lots of code here....*
        '
        salk = Sheets("SheetS").Cells(i, j).Value
        presence = WorksheetFunction.CountIf(Range("B1:D19"), salk)


                Dim arrOutput(2)

                Select Case presence
                    Case 1
                        Sheets("SheetX").Cells(i, j).Value = salk & " (" & GroupCol(salk) & ")"
                    Case 2
                        arrOutput(0) = Int(GroupCol(salk) / presence)
                        arrOutput(1) = GroupCol(salk) - arrOutput(0)
                        arrOutput(2) = 0

                        Sheets("SheetX").Cells(i, j).Value = salk & " (" & arrOutput(0) & " - " & arrOutput(1) & ")"
                    Case Else
                        MsgBox "Error"
                End Select
    Next j
Next i

With the code above I managed to get this outcome: enter image description here

I am very satisfied with the code that breaks an odd-number into two different numbers. So I don't have to cut people into half. When a group has an even number as an outcome then it can be devided into two same numbers. When its an odd number then it becomes much difficult. Dave helped me on how to fix the odd-number. I have implemented the code of Dave in my code. You see all the in brackets are total numbers of persons in a group belonging to a team. You see 4a4 has total of 39. Because this group shows up twice I want to split this group into two.When I do this I get 18.5, the code of Dave (above implemented)gives me the ability to get the following two numbers 19 and 20. My question is to get these two separate numbers over the two groups the group with the team name C gets 19 (see B2 in picture) and the other number 20 goes to B6. When this is adjusted I get the following image:

enter image description here

Because the select case is in a loop I can not reserve them into separate variable :(... Is there a way to get what I want.

War es hilfreich?

Lösung

I have a potential solution (took me some time and the algo quality is not that good)

Instead of doing:

Sheets("SheetX").Cells(i, j).Value = salk & " (" & arrOutput(0) & " - " & arrOutput(1) & ")"

in Case 2 you can try (you will have to adapt my code, I tried it on a personnal example):

' ====== ALGO START
' The following calculate the number of times salk appears before the current cell  
Select Case i    ' Depends on the ROWS since you CAN'T use a range which is not continuous with COUNTIF!
    Case 1
        presenceCurr = WorksheetFunction.CountIf(Range("B1" & ":" & Cells(i, j).Address), salk)
    Case 2
        presenceCurr = WorksheetFunction.CountIf(Range("B2" & ":" & Cells(i, j).Address), salk) _
                 + WorksheetFunction.CountIf(Range("B1:D1"), salk)
    Case 3
        presenceCurr = WorksheetFunction.CountIf(Range("B3" & ":" & Cells(i, j).Address), salk) _
                 + WorksheetFunction.CountIf(Range("B1:D2"), salk)
    Case 5
        ' etc ...

    ' All the other Cases

    Case 19
        presenceCurr = WorksheetFunction.CountIf(Range("B19" & ":" & Cells(i, j).Address), salk) _
                 + WorksheetFunction.CountIf(Range("B1:D18"), salk)
    Case Else       ' Should not happen
        MsgBox "Error, out of range"
End Select
presenceCurr = presenceCurr - 1    ' Minus 1 because the current cell would be counted
' ===== ALGO END

Sheets("SheetX").Cells(i, j).Value = salk & " (" & arrOutput(presenceCurr) & ")"    

I'm not perfectly sure this is going to work, because I suppose "B1:D19" is associated (in terms of range) with your nested for loop.

As already said, you'll probably have to adapt this code. What is important is the idea: I find how many times salk was found before the current cell and (it will be 0 or 1 in your example) and associate that with a position in your array (:

The Select Case could have been really shorter I you had looped on Columns then rows but ...

NOTE: This is a quick and dirty code, I can be clearly optimized and factorised (eg using a for loop instead of select case)

Anyway, I hope this will help and you can use it to do what you want

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top