0

I'm trying to code an analysis that will count certain cells in a data set and color code them based on their relative values. I have the counter code finished, but am now trying to get a subprocedure for the coloring to work. I've been able to achieve this in a stand alone sub procedure that has a manually entered range (ie "b2:e44") however, this isn't super useful as I will be applying this code to many datasets that vary in size.

I was able to code into the function a variable range called "datarange" that changes based on the entered datarange, but when I try to call the datarange and max variables into the subprocedure, it doesn't work.

How do I continue using the function variables in the subprocedure?

This is what I have so far:

Function breadthreport(datarange As Range, max As Variant, increase As String)

'counters
Dim cf1 As Variant
    cf1 = 0
Dim cf2 As Variant
    cf2 = 0
Dim cf3 As Variant
    cf3 = 0
Dim cf4 As Variant
    cf4 = 0
    
'cell variables
Dim x As Variant
Dim y As Variant

'no room for improvement
For x = 1 To datarange.Rows.Count Step 2
    For y = 1 To datarange.Columns.Count
        If datarange(x, y) = datarange(x + 1, y) And datarange(x, y) = max Then
            cf1 = cf1 + 1
        Else
        End If
    Next y
Next x


'stagnant cohort
For x = 1 To datarange.Rows.Count Step 2
    For y = 1 To datarange.Columns.Count
        If datarange(x, y) = datarange(x + 1, y) Then
            cf2 = cf2 + 1
        Else
        End If
    Next y
Next x

'worsening cohorts
For x = 1 To datarange.Rows.Count Step 2
    For y = 1 To datarange.Columns.Count
        If increase = "improvement" Then
            If datarange(x, y) > datarange(x + 1, y) Then
                cf3 = cf3 + 1
            Else
            End If
        ElseIf increase = "worsening" Then
            If datarange(x, y) < datarange(x + 1, y) Then
                cf3 = cf3 + 1
            Else
            End If
        Else
        End If
    Next y
Next x

'success cohorts
For x = 1 To datarange.Rows.Count Step 2
    For y = 1 To datarange.Columns.Count
        If increase = "improvement" Then
            If datarange(x, y) < datarange(x + 1, y) Then
                cf4 = cf4 + 1
            Else
            End If
        ElseIf increase = "worsening" Then
            If datarange(x, y) > datarange(x + 1, y) Then
                cf4 = cf4 + 1
            Else
            End If
        Else
        End If
    Next y
Next x

'define your array
Dim ret(3, 1) As Variant
Dim labels(3, 0) As String

'insert a sub to color the labels
breadthcolor datarange, max, increase

'labels
ret(0, 0) = "Stagnant Max Cohort"
ret(1, 0) = "Stagnant Cohort"
ret(2, 0) = "Worsening Cohort"
ret(3, 0) = "Success Cohort"

'assign values
ret(0, 1) = cf1
ret(1, 1) = cf2
ret(2, 1) = cf3
ret(3, 1) = cf4

breadthreport = ret
            
End Function

Sub breadthcolor(subrange As Range, submax As Variant, subincrease As String)

MsgBox "youre in the sub"

'cell variables
Dim x As Variant
Dim y As Variant

'no room for improvement
For x = 1 To subrange.Rows.Count Step 2
    For y = 1 To subrange.Columns.Count
        For Each cell In subrange
            If cell(x, y).Value = cell(x + 1, y).Value Then
                cell.Interior.color = vbGreen
            Else
            End If
        Next
    Next y
Next x
'the rest of the sub will mirror the counter function but with color changes rather than counters
End Sub

Ideally this can be done using the datarange defined in the function, I'm sharing this code with my team who have no background in coding and will not be able to manually edit the range in the procedure.

  • 1
    What does doesn't work mean here? Are you getting an error? Is it coloring the wrong cells? Is nothing happening at all? – Warcupine Apr 20 '21 at 19:21
  • 3
    how is the function called? If it is through the worksheet, then it will not work. – Scott Craner Apr 20 '21 at 19:22
  • UDF(User Defined Functions) called from the worksheet will not change the format of cells. There are some workarounds, see: https://stackoverflow.com/questions/23433096/using-a-udf-in-excel-to-update-the-worksheet – Scott Craner Apr 20 '21 at 19:25
  • Right now I have the user defined function and sub procedure on the worksheet code. When I put the function and sub into a module/function in module sub in worksheet, the sub procedure isn't recognized in the function. – Isabela Artola Apr 20 '21 at 19:59
  • I don't think the code is going into the sub procedure at all because I don't get the message box. The counter still seems to work no matter were I put that code – Isabela Artola Apr 20 '21 at 20:01
  • Just thought I'd ask the questions again......What does doesn't work mean here? Are you getting an error? Is it coloring the wrong cells? Is nothing happening at all? How is the function called? If it is through the worksheet, then it will not work. – Darren Bartrup-Cook Apr 21 '21 at 10:56
  • The counter will work (ie return the array of strings and values) however the sub procedure is skipped (no interior color changes and the message box never appears, meaning the sub is skipped for some reason) – Isabela Artola Apr 21 '21 at 14:11

0 Answers0