如何在被调用的子过程中使用函数参数

问题描述

我正在尝试编写一个分析代码,该分析将对数据集中的某些单元格进行计数,并根据它们的相对值对它们进行颜色编码。我已经完成了计数器代码,但现在我正在尝试获得一个子过程来使着色工作。我已经能够在具有手动输入范围(即“b2:e44”)的独立子过程中实现这一点,但是,这不是非常有用,因为我将将此代码应用于许多大小不同的数据集.

我能够将一个名为“datarange”的变量范围编码到函数中,该范围会根据输入的数据范围而变化,但是当我尝试将 datarange 和 max 变量调用到子过程中时,它不起作用。

如何继续使用子过程中的函数变量?

这是我目前所拥有的:

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) 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) Then
                cf4 = cf4 + 1
            Else
            End If
        ElseIf increase = "worsening" Then
            If datarange(x,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

理想情况下,这可以使用函数中定义的数据范围来完成,我正在与我的团队共享此代码,他们没有编码背景并且无法在过程中手动编辑范围。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)