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

BradleyS

Active Member
Joined
Oct 28, 2006
Messages
347
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 Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
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,101
Messages
6,170,116
Members
452,302
Latest member
TaMere

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