Output Variant to Range

delaneyjm

Well-known Member
Joined
Apr 22, 2009
Messages
624
Afternoon all,

Having a bit of an issue with the following VBA code.

Code:
Sub NetGroups()
    Dim ado As Object
    Dim servername As String
    Dim filter As String
    Dim ol As Object
    Dim RetVal As String
    Dim full As Variant
    Dim counter As Long
    Dim rng As Range

    Set ado = CreateObject("ADODB.Connection")
    ado.Provider = "ADSDSOObject"
    ado.Open "NameSearch"
    
    servername = "FS-V005/o=GS"
    filter = "(fullName=John Doe)"
    
    Set ol = ado.Execute("<ldap: "="" &="" servername="">;" & filter & ";groupMembership;SubTree")
    
    If ol.EOF Then
        MsgBox "No person found with that name"
    ElseIf ol.RecordCount > 1 Then
        MsgBox "Found more than 1 instance of this name.  Please specify additional criteria"
    Else
        full = ol.Fields("groupMembership").Value
        For counter = LBound(full) To UBound(full) Step 1
            full(counter) = Replace(Replace(Right(full(counter), Len(full(counter)) - 3), ",ou=", "."), ",o=", ".")
        Next counter
        
        Set rng = ActiveSheet.Range("A4:A" & UBound(full) + 4)
        rng.Value = full
    End If
End Sub
My LDAP query is working fine, the ol object has information, and the variant full has data.

My problem is when the macro reaches the line rng.Value = full, it does output information to the range but only the first item in the variant full to the specified range.

When I was troubleshooting the code myself, I went into the Locals windo and looked at the variant full. It appears to be of Variant/String type.

Does the variant full need to be a 2 dimensional "array" in order to output to a range?</ldap:>
 
Try:
Set rng = ActiveSheet.Range("A4:A" & UBound(full) + 4 - 1 )

or
Set rng = ActiveSheet.Range("A4").Resize(UBound(full))
 
Last edited:
Upvote 0
Norie, ol has OLE objects in it and .CopyFromRecordset fails hence why I was trying to output to a variant and then a range. I did use your suggestion and loop through ol to get the necessary info though. Typically, it's only returning 10 ±5 records so while it may not be the fastest method, it does work.

ZVI, both of those generate type mismatch errors when I run them in the code.
 
Upvote 0
I wasn't sure if CopyFromRecordset would work and I couldn't find any examples that used it.

I did see some examples looping though.

Didn't fully understand them though

They weren't as straightforward as I'm used to - probably because of the OLE objects you mention.
 
Upvote 0

Forum statistics

Threads
1,226,871
Messages
6,193,443
Members
453,799
Latest member
shanley ducker

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