Range not passing to function

trough

Board Regular
Joined
Oct 26, 2010
Messages
55
I don't normally work with Range variables but I now have a need. I wrote a function to basically rearrange a list of user names. The function works fine, however, referring to the screenshot, using the function in column E works great but trying to use it as shown in column F does not. I've searched on this to no avail and cannot figure out why the IF statement, which returns an array, won't pass to the function. It's as if the function isn't even getting called. My goal is to eliminate columns E & F so that I can generate the final list I desire in column D using a single formula that calls the function. Please advise. Thanks.



Excel 2010
ABCDEF
1Smith, JohnJSmithNo-JDoe#VALUE!
2Doe, JohnJDoeYesJDoeSLincoln#VALUE!
3Jones, MaryMJonesNo--#VALUE!
4Lincoln, SamSLincolnYesSLincoln-#VALUE!
5Carver, TimTCarverNo--#VALUE!
Sheet1
Cell Formulas
RangeFormula
D1:D5{=IF(A1:A5=", ","-",IF(C1:C5="No","-",B1:B5))}
E1:E5{=UNameList(D1:D5)}
F1:F5{=UNameList(IF(A1:A5=", ","-",IF(C1:C5="No","-",B1:B5)))}
Press CTRL+SHIFT+ENTER to enter array formulas.



Code:
Option Explicit
Option Base 1

Function UNameList(UName As Range)
    
' declare variables
Dim MyList() As String
Dim nList As Long
Dim i As Long
Dim j As Long
    
' get list size and dimension dynamic variable
nList = UName.Count
ReDim MyList(nList)

' populate dynamic list with names
j = 0
For i = 1 To nList
 If UName.Cells(i, 1) <> "-" Then
  j = j + 1
  MyList(j) = UName.Cells(i, 1)
 End If
Next i

' populate empty variable positions
If j < nList Then
 For i = j + 1 To nList
  MyList(i) = "-"
 Next i
End If
        
' write back to workbook
UNameList = Application.Transpose(MyList)

End Function
 
Last edited by a moderator:

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
You aren't passing a range, you're passing an array in column F (since you pass "-" for some rows and cell values for other rows), which is why the function fails.
 
Upvote 0
Okay, that makes sense. However, after modifying the code, reading more on the topic, and trying several different things it's still not working so I guess I'm still clueless. Could you provide anymore hints?

And then am I passing the functions final result back to the workbook properly. I just never pass arrays back and forth so I am out of my element now. Sorry.

Excel 2010
ABCDE

<colgroup><col style="width: 25pxpx"><col><col><col><col><col></colgroup><thead>
</thead><tbody>
[TD="align: center"]1[/TD]
[TD="bgcolor: <a href="]Smith, John
[/TD]
[TD="bgcolor: <a href=, align: center"]JSmith
[/TD]
[TD="bgcolor: <a href=, align: center"]No
[/TD]
[TD="align: right"][/TD]
[TD="bgcolor: [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=C5D9F1]#C5D9F1[/URL] , align: center"]#VALUE![/TD]

[TD="align: center"]2[/TD]
[TD="bgcolor: <a href="]Doe, John
[/TD]
[TD="bgcolor: <a href=, align: center"]JDoe
[/TD]
[TD="bgcolor: <a href=, align: center"]Yes
[/TD]
[TD="align: right"][/TD]
[TD="bgcolor: [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=C5D9F1]#C5D9F1[/URL] , align: center"]#VALUE![/TD]

[TD="align: center"]3[/TD]
[TD="bgcolor: <a href="]Jones, Mary
[/TD]
[TD="bgcolor: <a href=, align: center"]MJones
[/TD]
[TD="bgcolor: <a href=, align: center"]No
[/TD]
[TD="align: right"][/TD]
[TD="bgcolor: [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=C5D9F1]#C5D9F1[/URL] , align: center"]#VALUE![/TD]

[TD="align: center"]4[/TD]
[TD="bgcolor: <a href="],
[/TD]
[TD="bgcolor: <a href=, align: center"]-
[/TD]
[TD="bgcolor: <a href=, align: center"]No
[/TD]
[TD="align: right"][/TD]
[TD="bgcolor: [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=C5D9F1]#C5D9F1[/URL] , align: center"]#VALUE![/TD]

[TD="align: center"]5[/TD]
[TD="bgcolor: <a href="]Carver, Tim
[/TD]
[TD="bgcolor: <a href=, align: center"]TCarver
[/TD]
[TD="bgcolor: <a href=, align: center"]Yes
[/TD]
[TD="align: right"][/TD]
[TD="bgcolor: [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=C5D9F1]#C5D9F1[/URL] , align: center"]#VALUE![/TD]

</tbody>
Sheet1

[TABLE="width: 85%"]
<tbody>[TR]
[TD]Array Formulas[TABLE="width: 100%"]
<thead>[TR="bgcolor: [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=DAE7F5]#DAE7F5[/URL] "]
[TH="width: 10px"]Cell[/TH]
[TH="align: left"]Formula[/TH]
[/TR]
</thead><tbody>[TR]
[TH="width: 10px, bgcolor: [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=DAE7F5]#DAE7F5[/URL] "]E1:E5[/TH]
[TD="align: left"]{=UNameList(IF(A1:A5=", ","-",IF(C1:C5="No","-",B1:B5)))}[/TD]
[/TR]
</tbody>[/TABLE]
Entered with Ctrl+Shift+Enter. If entered correctly, Excel will surround with curly braces {}.
Note: Do not try and enter the {} manually yourself[/TD]
[/TR]
</tbody>[/TABLE]


Code:
Option Explicit
Option Base 1

Function UNameList(UName() As String)

' declare variables
Dim MyList() As String
Dim nList As Long
Dim i As Long
Dim j As Long

' get list size and dimension dynamic variable
nList = UBound(UName)
ReDim MyList(nList)

' populate dynamic list with names
j = 0
For i = 1 To nList
 If UName.Cells(i, 1) <> "-" Then
  j = j + 1
  MyList(j) = UName.Cells(i, 1)
 End If
Next i

' populate empty variable positions
If j < nList Then
 For i = j + 1 To nList
  MyList(i) = "-"
 Next i
End If

' write back to workbook
UserList = MyList()

End Function
 
Last edited by a moderator:
Upvote 0
Please use code tags. Thanks.

Uname is an array so it doesn’t have cells - remove the .Cells parts. Also your function isn’t returning anything since you assigned the return array to UserList rather than UNameList.

I’m on an iPad so can’t do more than hint just now. ;)
 
Upvote 0
Duh! My bad. I didn't remove the .Cell when I converted to the String array instead of Range. Also, I missed the function rename I did when I was trying different things. However, I still can't get this thing to work. I put a break at the ReDim line and it doesn't stop there. It's like the function isn't even getting ran. I'm just stuck.

The array function still looks the same: {=UserList(IF(A1:A5=", ","-",IF(C1:C5="No","-",B1:B5)))}

Code:
Option Explicit
Option Base 1

Function UserList(UName() As String)

' declare variables
Dim MyList() As String
Dim nList As Long
Dim i As Long
Dim j As Long

' get list size and dimension dynamic variable
nList = UBound(UName)
ReDim MyList(nList)

' populate dynamic list with names
j = 0
For i = 1 To nList
 If UName(i) <> "-" Then
  j = j + 1
  MyList(j) = UName(i)
 End If
Next i

' populate empty variable positions
If j < nList Then
 For i = j + 1 To nList
  MyList(i) = "-"
 Next i
End If

' write back to workbook
UserList = MyList()

End Function
 
Upvote 0
In the original post all it seems to see is 'rearrange a list of user names' and I'm afraid, for me anyway, the table/formulas/code don't explain things further.

I think you want to produce a delimited list of some sort but I may be way off.:)
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,337
Members
452,636
Latest member
laura12345

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