Convert Sub to Function

STA

New Member
Joined
May 30, 2022
Messages
6
Office Version
  1. 365
Hi All,

New member here and hope you can help.

I am entry lvl in VBA (started learning 2 months ago) and trying to convert a sub procedure into a function but i have no idea how. Whatever i have done i keep having a #value! as answer in my formula.

Below is the code :

VBA Code:
Option Explicit

Sub Group_Constraint()

Dim sh1 As Worksheet
Dim arr1 As Variant
Dim Check2 As New Collection
Dim i As Integer, k As Integer
Dim j As Integer, y As Integer
Dim rng As Range

Set sh1 = Sheets(3)

Set rng = sh1.Range("A1", sh1.Range("A1").End(xlToRight).End(xlDown))

arr1 = rng

For j = 1 To 2

    For i = LBound(arr1, 1) To UBound(arr1, 1)

        sh1.Range("N1").Offset(j - 1, i - 1) = arr1(j, i)

    Next i

Next j

For j = 2 To 2

    For i = 2 To UBound(arr1, 1)

        If Application.WorksheetFunction.IsNumber(arr1(j, i)) = True Then
       
            If arr1(j, i) >= 0.5 Then

                 For k = 1 To UBound(arr1, 1)

                    sh1.Range("N1").Offset(0, k - 1).End(xlDown).Offset(1) = arr1(i, k)

                Next k
       
            End If
       
        End If

    Next i

Next j

i = 1

For i = LBound(arr1, 1) To UBound(arr1, 1)

    If Application.WorksheetFunction.CountIf(sh1.Range("N1", sh1.Range("N1").End(xlDown)), sh1.Range("N1").Offset(0, i - 1)) = 0 Then
       
        If Application.WorksheetFunction.Sum(sh1.Range(sh1.Range("N1").Offset(0, i - 1), sh1.Range("N1").Offset(0, i - 1).End(xlDown))) >= 0.5 Then
   
            For k = 1 To UBound(arr1, 1)
       
                sh1.Range("N1").Offset(0, k - 1).End(xlDown).Offset(1) = arr1(i, k)
       
            Next k
           
        Else
           
            Check2.Add i, sh1.Range("N1").Offset(0, i - 1)
            GoTo NextIteration
       
        End If
       
    Else
   
        GoTo NextIteration
       
    End If

NextIteration:

Next i

k = 1
j = 1
i = 1

If IsEmpty(Check2) Then

Else

    Do While Check2.Count <> y

     y = Check2.Count
    
Above:
    
        For i = 1 To Check2.Count

                If i > Check2.Count Then
               
                  GoTo Above
               
                Else
           
                        If Application.WorksheetFunction.Sum(sh1.Range(sh1.Range("N1").Offset(0, Check2(i) - 1), sh1.Range("N1").Offset(0, Check2(i) - 1).End(xlDown))) >= 0.5 Then
                   
                                For k = 1 To UBound(arr1, 1)
                   
                                    sh1.Range("N1").Offset(0, k - 1).End(xlDown).Offset(1) = arr1(Check2(i), k)
                   
                                Next k
                               
                                Check2.Remove (sh1.Range("N1").Offset(0, Check2(i) - 1))
                               
                        End If
               
                End If
       
        Next i
   
    Loop
   
End If

If sh1.Range("N2", sh1.Range("N2").End(xlDown)).Count = sh1.Range("O1", sh1.Range("O1").End(xlToRight)).Count Then

        Cells(14, 1) = "TRUE"
       
        sh1.Range("N1", sh1.Range("N1").End(xlToRight)).EntireColumn.Delete

Else


        Cells(14, 1) = "FALSE"
        sh1.Range("N1", sh1.Range("N1").End(xlToRight)).EntireColumn.Delete

End If

End Sub

What i want to do is being able to have a formula , put inside the range i need and then return a true or false as an answer. As a sub is working fine but as a fuction everything goes wrong.

Happy to hear your thoughts

Regards,
 
Glad you sorted it & thanks for the feedback.
 
Upvote 0

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.

Forum statistics

Threads
1,214,649
Messages
6,120,732
Members
448,987
Latest member
marion_davis

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top