Issue passing two parameters from sub to function to get back result

BradleyS

Active Member
Joined
Oct 28, 2006
Messages
351
Office Version
  1. 2010
Platform
  1. Windows
I'm trying to pass two parameters (sr and colPos) from a sub to a function and get back the result (Names).
The function checks for a value in column H and if found gets the name in column B of the adjacent row
The Names variable will have a list of ; separate names

However, I keep getting a compile error "Expected array" in the sub with regards to "Names"
What am I doing wrong? How do I accomplish this?

VBA Code:
Option Explicit
Sub Macro1()

    Dim sr As String, colPos As Integer, Names As String, sn As String

        sr = "H7:H"
        colPos = 6
        
        sn = Names(sr, colPos)


End Sub

Function Names(ByVal sr As String, ByVal colPos As Integer) As String
        
        Dim output
        Dim entry As Variant
        Dim Lr As Long
    
        Lr = ThisWorkbook.ActiveSheet.Range("B" & Rows.Count).End(xlUp).Row
            On Error Resume Next    'if only 1 row
    
        For Each entry In ThisWorkbook.ActiveSheet.Range(sr & Lr)
            If Not IsEmpty(entry.Value) Then
                output = output & entry.Offset(, -colPos).Value & ";"
            End If
        Next
        
        Names = Left(output, Len(output) - 1)

End Function
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
I'm trying to pass two parameters (sr and colPos) from a sub to a function and get back the result (Names).
The function checks for a value in column H and if found gets the name in column B of the adjacent row
The Names variable will have a list of ; separate names

However, I keep getting a compile error "Expected array" in the sub with regards to "Names"
What am I doing wrong? How do I accomplish this?

VBA Code:
Option Explicit
Sub Macro1()

    Dim sr As String, colPos As Integer, Names As String, sn As String

        sr = "H7:H"
        colPos = 6
       
        sn = Names(sr, colPos)


End Sub

Function Names(ByVal sr As String, ByVal colPos As Integer) As String
       
        Dim output
        Dim entry As Variant
        Dim Lr As Long
   
        Lr = ThisWorkbook.ActiveSheet.Range("B" & Rows.Count).End(xlUp).Row
            On Error Resume Next    'if only 1 row
   
        For Each entry In ThisWorkbook.ActiveSheet.Range(sr & Lr)
            If Not IsEmpty(entry.Value) Then
                output = output & entry.Offset(, -colPos).Value & ";"
            End If
        Next
       
        Names = Left(output, Len(output) - 1)

End Function
Change the function name from 'Names' which is reserved.
 
Upvote 0
I have changed it to GetNames and it still shows the same error

In all three places?

VBA Code:
Option Explicit
Sub Macro1()

    Dim sr As String, colPos As Integer, Names As String, sn As String

        sr = "H7:H"
        colPos = 6
        
        sn = GetNames(sr, colPos)

End Sub

Function GetNames(ByVal sr As String, ByVal colPos As Integer) As String
        
        Dim output
        Dim entry As Variant
        Dim Lr As Long
    
        Lr = ThisWorkbook.ActiveSheet.Range("B" & Rows.Count).End(xlUp).Row
            On Error Resume Next    'if only 1 row
    
        For Each entry In ThisWorkbook.ActiveSheet.Range(sr & Lr)
            If Not IsEmpty(entry.Value) Then
                output = output & entry.Offset(, -colPos).Value & ";"
            End If
        Next
        
        GetNames = Left(output, Len(output) - 1)

End Function
 
Upvote 0
Solution
In all three places?

VBA Code:
Option Explicit
Sub Macro1()

    Dim sr As String, colPos As Integer, Names As String, sn As String

        sr = "H7:H"
        colPos = 6
       
        sn = GetNames(sr, colPos)

End Sub

Function GetNames(ByVal sr As String, ByVal colPos As Integer) As String
       
        Dim output
        Dim entry As Variant
        Dim Lr As Long
   
        Lr = ThisWorkbook.ActiveSheet.Range("B" & Rows.Count).End(xlUp).Row
            On Error Resume Next    'if only 1 row
   
        For Each entry In ThisWorkbook.ActiveSheet.Range(sr & Lr)
            If Not IsEmpty(entry.Value) Then
                output = output & entry.Offset(, -colPos).Value & ";"
            End If
        Next
       
        GetNames = Left(output, Len(output) - 1)

End Function

Ok this works and I can see that the only thing you have changed is remove(ignores) the dim Names as string reference in the sub.
It works so I'm happy thank you
 
Upvote 0

Forum statistics

Threads
1,223,885
Messages
6,175,182
Members
452,615
Latest member
bogeys2birdies

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